Page - 4 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 4 -
Text of the Page - 4 -
4 1 TheFirstFewSteps
1.2.1 TheProgram
Let us next look at a Pythonprogramfor evaluating this simple formula. Assume
theprogramiscontainedas text inafilenamedball.py. The text looksas follows
(fileball.py):
# Program for computing the height of a ball in vertical motion
v0 = 5 # Initial velocity
g = 9.81 # Acceleration of gravity
t = 0.6 # Time
y = v0*t - 0.5*g*t**2 # Vertical position
print y
Computer programs and parts of programs are typesetwith a blue background
in thisbook.Aslightlydarker topandbottombar,asabove, indicates that thecode
isacompleteprogramthatcanbe runas it stands.Without thebars, thecode is just
a snippetandwill (normally)needadditional lines to runproperly.
1.2.2 DissectionoftheProgram
Acomputer program is plain text, as here in the fileball.py,which contains in-
structions to the computer. Humans can read the code and understand what the
programis capableofdoing,but theprogramitself doesnot trigger anyactionson
a computer before another program, thePython interpreter, reads theprogram text
and translates this text into specificactions.
Youmustlearntoplaytheroleofacomputer
AlthoughPythonisresponsibleforreadingandunderstandingyourprogram,it is
of fundamental importance thatyoufullyunderstand theprogramyourself. You
have toknowthe implicationof every instruction in theprogramandbe able to
figureout theconsequencesof the instructions. Inotherwords,youmustbeable
toplaytheroleofacomputer. Thereasonforthisstrongdemandofknowledgeis
that errors unavoidably, andquite often,will be committed in theprogram text,
and to track down these errors, you have to simulate what the computer does
with theprogram.Next,weshall explainall the text inball.py in fulldetail.
Whenyou runyourprograminPython, itwill interpret the text inyourfile line
by line, fromthe top, readingeach line fromleft to right. Thefirst line it reads is
# Program for computing the height of a ball in vertical motion.
This line iswhatwecallacomment. That is, the line isnotmeantforPythontoread
andexecute,but rather forahumanthat reads thecodeand tries tounderstandwhat
is going on. Therefore, one rule in Python says thatwhenever Python encounters
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