Page - 40 - in Programming for Computations โ Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 40 -
Text of the Page - 40 -
40 2 AFewMoreSteps
Observe that, as in a program, certain items must be imported before use, e.g.,pi,
sin,cos, etc.Forexample, to interactivelycomputesin(ฯ2), youcouldwrite
In [1]: from math import sin, pi
In [2]: sin(pi/2)
Out[2]: 1.0
Observe that the import statement here, i.e. from math import sin, pi, is an
exampleof input thatdoesnotproduceanycorrespondingoutput.
Youmayalso definevariablesanduse formulas interactivelyas
In [1]: v0 = 5
In [2]: g = 9.81
In [3]: t = 0.6
In [4]: y = v0*t - 0.5*g*t**2
In [5]: print(y)
1.2342
2.1.2 CommandHistory
IPythonstores thedialogue,whichallowsyoutoeasily repeatapreviouscommand,
with or without modifications. Using the up-arrow key, brings you โbackwardsโ in
commandhistory.Pressing thisone timegivesyou thepreviouscommand,pressing
two timesgivesyou the commandbefore that, andso on.With the down-arrowkey
you can go โforwardโ again. When you have the relevant command at the prompt,
youmayedit it beforepressingenter (which letsPythonread it and take action).
2.1.3 TABCompletion
When typing in IPython, you may get assistance from the TAB key to finalize a
variablenameorcommandyouare typing.
To illustrate, assume youhavewritten
In [1]: import numpy as np
In [2]: x = np.lins # before pressing TAB key
and then press the TAB key. IPython will then assume you intend to write
np.linspaceand thereforefill out the rest foryou, so thatyousuddenlyhave
In [2]: x = np.linspace # after pressing TAB key
Youmay thengoonandfill out the rest, perhaps like
In [2]: x = np.linspace(0, 1, 11) # after filling out the rest
Programming for Computations โ Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
- Title
- Programming for Computations โ Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Volume
- Second Edition
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2020
- Language
- English
- License
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 356
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik