Page - 12 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 12 -
Text of the Page - 12 -
12 1 TheFirstFewSteps
1.5 MoreBasicConcepts
Sofarwehaveseena fewbasicexamplesonhowtoapplyPythonprogrammingto
solvemathematical problems. Beforewe can go onwith other andmore realistic
examples, we need to briefly treat some topics thatwill be frequently required in
later chapters. These topics include computer science concepts like variables, ob-
jects, errormessages, andwarnings;morenumericalconcepts like roundingerrors,
arithmetic operator precedence, and integer division; in addition to more Python
functionalitywhenworkingwitharrays,plotting, andprinting.
1.5.1 UsingPythonInteractively
Python can also be used interactively. That is, we do not first write a program in
a file and execute it, butwe give statements and expressions towhat is known as
a Python shell. We recommend to use IPython as shell (because it is superior to
alternativePythonshells).WithSpyder,Ipython is availableat startup, appearing
as the lower right window. Following the IPython prompt In [1]: ( a prompt
meansa“readysign”, i.e. theprogramallowsyoutoenteracommand,anddifferent
programsoftenhavedifferent lookingprompts),youmaydocalculations:
In [1]: 2+2
Out [1]: 4
In [2]: 2*3
Out [2]: 6
In [3]: 10/2
Out [3]: 5
In [4]: 2**3
Out [4]: 8
The response fromIPython is precededbyOut [q]:,whereqequalspwhen the
response is to input “number”p.
Note that, as in a program, youmay have to use import before using pi or
functions likesin,cos, etc. That is, at the prompt, do the commandfrom math
import *beforeyouusepiorsin, etc. Importingothermodules thanmathmay
berelevant,dependingonwhatyouraim iswith thecomputations.
Youmayalsodefinevariablesanduse 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
------> print(y)
1.2342
back to the
book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python
- Title
- Programming for Computations – Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2016
- Language
- English
- License
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 248
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik