40
2 A Few More Steps
Observe that, as in a program, certain items must be imported before use, e.g., pi,
sin, cos, etc. For example, to interactively compute sin(
π
2 ), you could write
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
example of input that does not produce any corresponding output.
You may also define variables and use formulas interactively as
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 Command History
IPython stores the dialogue, which allows you to easily repeat a previous command,
with or without modifications. Using the up-arrow key, brings you “backwards” in
command history. Pressing this one time gives you the previous command, pressing
two times gives you the command before that, and so on. With the down-arrow key
you can go “forward” again. When you have the relevant command at the prompt,
you may edit it before pressing enter (which lets Python read it and take action).
2.1.3 TAB Completion
When typing in IPython, you may get assistance from the TAB key to finalize a
variable name or command you are typing.
To illustrate, assume you have written
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.linspace and therefore fill out the rest for you, so that you suddenly have
In [2]: x = np.linspace
# after pressing TAB key
You may then go on and fill out the rest, perhaps like
In [2]: x = np.linspace(0, 1, 11)
# after filling out the rest
2 A Few More Steps
Observe that, as in a program, certain items must be imported before use, e.g., pi,
sin, cos, etc. For example, to interactively compute sin(
π
2 ), you could write
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
example of input that does not produce any corresponding output.
You may also define variables and use formulas interactively as
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 Command History
IPython stores the dialogue, which allows you to easily repeat a previous command,
with or without modifications. Using the up-arrow key, brings you “backwards” in
command history. Pressing this one time gives you the previous command, pressing
two times gives you the command before that, and so on. With the down-arrow key
you can go “forward” again. When you have the relevant command at the prompt,
you may edit it before pressing enter (which lets Python read it and take action).
2.1.3 TAB Completion
When typing in IPython, you may get assistance from the TAB key to finalize a
variable name or command you are typing.
To illustrate, assume you have written
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.linspace and therefore fill out the rest for you, so that you suddenly have
In [2]: x = np.linspace
# after pressing TAB key
You may then go on and fill out the rest, perhaps like
In [2]: x = np.linspace(0, 1, 11)
# after filling out the rest
