2
A Few More Steps
2.1 Using Python Interactively
2.1.1 The IPython Shell
Python can also be used interactively, in which case we do not first write a program,
store it in a file and then execute it. Rather, we give statements and expressions
directly to what is known as a Python shell. This means that we communicate with
the Python interpreter via a particular interface. Interactive use of Python is a great
way to quickly demonstrate different aspects of Python and we will use it repeatedly
for this purpose. It is also very useful for testing out things like function behavior,
syntax issues, etc., before finalizing related code in a program you are writing.
We recommend to use IPython as shell (because it is superior to alternative
Python shells). With Spyder, Ipython is available at startup, appearing in the lower
right pane (the console). An interactive session appears as a succession of pairwise
corresponding input (to Python) and output (from Python). The user writes input
commands after the IPython prompt 1 In [p]:, where p = 1, 2, . . ., and gets the
response back (if any) after Out [p]:. Thus, p serves as a counter for each pair of
input and output (when there is no output, this “pair” will consist of only input).
To exemplify, we may write:
In [1]: 2+2
Out[1]: 4
In [2]: 2*3
Out[2]: 6
In [3]: 10/2
# note: gives float
Out[3]: 5.0
In [4]: 2**3
Out[4]: 8
1 A prompt means a “ready sign”, i.e. the program allows you to enter a command, and different
programs often have different looking prompts.
© The Author(s) 2020
S. Linge, H. P. Langtangen, Programming for Computations - Python,
Texts in Computational Science and Engineering 15,
https://doi.org/10.1007/978-3-030-16877-3_2
39
A Few More Steps
2.1 Using Python Interactively
2.1.1 The IPython Shell
Python can also be used interactively, in which case we do not first write a program,
store it in a file and then execute it. Rather, we give statements and expressions
directly to what is known as a Python shell. This means that we communicate with
the Python interpreter via a particular interface. Interactive use of Python is a great
way to quickly demonstrate different aspects of Python and we will use it repeatedly
for this purpose. It is also very useful for testing out things like function behavior,
syntax issues, etc., before finalizing related code in a program you are writing.
We recommend to use IPython as shell (because it is superior to alternative
Python shells). With Spyder, Ipython is available at startup, appearing in the lower
right pane (the console). An interactive session appears as a succession of pairwise
corresponding input (to Python) and output (from Python). The user writes input
commands after the IPython prompt 1 In [p]:, where p = 1, 2, . . ., and gets the
response back (if any) after Out [p]:. Thus, p serves as a counter for each pair of
input and output (when there is no output, this “pair” will consist of only input).
To exemplify, we may write:
In [1]: 2+2
Out[1]: 4
In [2]: 2*3
Out[2]: 6
In [3]: 10/2
# note: gives float
Out[3]: 5.0
In [4]: 2**3
Out[4]: 8
1 A prompt means a “ready sign”, i.e. the program allows you to enter a command, and different
programs often have different looking prompts.
© The Author(s) 2020
S. Linge, H. P. Langtangen, Programming for Computations - Python,
Texts in Computational Science and Engineering 15,
https://doi.org/10.1007/978-3-030-16877-3_2
39
