Seite - 8 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 8 -
Text der Seite - 8 -
8 1 TheFirstFewSteps
This is a print function call, which makes Python print the value of y on the
screen.4 Simply stated, the value ofy is sent to a ready-madepiece of code named
print (being a function—see Chap.4, here called with a single argument named
y), which then takes care of the printing. Thus, whenball.py is run, the number
1.2342appearson thescreen.
Readabilityand Coding Style In the codeabove,yousee several blank lines too.
ThesearesimplyskippedbyPythonandyoumayuseasmanyasyouwanttomakea
niceandreadablelayoutofthecode.Similarly,younoticethatspacesareintroduced
to each side of− in the “formula” and to each side of= in the assignments. These
spaces are not required, i.e., Python will understand perfectly well without them.
However, they contribute to readability and it is recommended to use them5 as part
of good coding style.6 Had there been a+ sign in there, it too should have a space
toeachside.To the contrary,noextra spacesare recommendedfor/,*and**.
SeveralStatementsonOneLine Note that it’sallowed tohaveseveral statements
on the same line if they are separated by a semi-colon. So, with our program here,
wecouldhavewritten, e.g.,
# Program for computing the height of a ball in vertical motion
# v0 is the intial velocity, g is the acceleration of gravity, t is time
v0 = 5; g = 9.81; t = 0.6
y = v0*t - 0.5*g*t**2 # vertical position
print(y)
Ingeneral,however, readability is easilydegradedthisway,e.g.,makingcomment-
ingmoredifficult, so it shouldbedonewithcare.
Assignments like a=2*a
Frequently,you will meet assignment statements in which the variable name
on the left hand side (of=) also appears in the expression on the right hand
side. Take, e.g., a = 2*a. Python would then, according to its rules, first
compute the expression on the right hand side with the current value of a
and then let the result become the updatedvalue ofa through the assignment
(theupdatedvalueofa isplaced in anew“box” in computermemory).
4 In Python 2.7, thiswould have been a print command readingprint y.
5 Beaware that incertainsituationsprogrammersdoskipsuchspaces, e.g.,whenlistingarguments
in function calls, asyou will learn more about inChap.4.
6 You might like to check out the style guide for Python coding at https://www.python.org/dev/
peps/pep-0008/.
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