Seite - 6 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 6 -
Text der Seite - 6 -
6 1 TheFirstFewSteps
t = 0.6 # Time
y = v0*t - 0.5*g*t**2 # Vertical position
print(y)
Letusnowexplain thisprogramin fulldetail.
Typesettingof Code Computerprograms,andpartsofprograms,are typeset with
a blue background in this book. When a complete program is shown, the blue
backgroundhasaslightlydarker topandbottombar(asforball.pyhere).Without
the bars, the code is just a snippet and will normally need additional lines to run
properly.
We also use the blue background, without bars, for interactive sessions
(Sect.2.1).
1.2.2 DissectingtheProgram
A computerprogramlikeball.pycontains instructions to thecomputerwrittenas
plain text. Humans can read the code and understand what the program is capable
of doing, but the program itself does not trigger any actions on a computer before
another program, the Python interpreter, reads the program text and translates this
text into specificactions.
Youmust learntoplay theroleofacomputer
AlthoughPython is responsible for readingandunderstandingyourprogram,
it is of fundamental importance that you fully understand the program
yourself.Youhavetoknowtheimplicationofeveryinstructionintheprogram
andbeable tofigureout theconsequencesof the instructions. Inotherwords,
youmustbeable to play the roleofa computer.
Oneimportantreasonforthisstrongdemandis thaterrorsunavoidably,and
quite often, will be committed in the program text, and to track down these
errors,you have to simulate what the computerdoeswith the program.Also,
youwilloftenneedtounderstandcodewrittenbyotherpeople.Ifyouareable
tounderstandtheir codeproperly,youmaymodifyanduse it as it suitsyou.
When you run your program in Python, it will interpret the text in your file 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 isnotmeant forPythontoread
andexecute,but rather forahumanthat reads thecodeand tries tounderstandwhat
isgoingon.Therefore,onerule inPythonsays thatwheneverPythonencountersthe
sign# it takes the rest of the line as a comment. Python then simply skips reading
the rest of the line and jumps to the next line. In the code, you see several such
comments and probably realize that they make it easier for you to understand (or
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Band
- Second Edition
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2020
- Sprache
- englisch
- Lizenz
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 356
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik