Seite - 116 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 116 -
Text der Seite - 116 -
116 5 SomeMorePythonEssentials
ball_function.py, while the other two, time_of_flight and max_height,
compute the time of flight and maximumheight attained, respectively (consult any
introductorybookonmechanics regardingthe implementedformulas).
Inlinewithgoodprogrammingpractice,wealsoequipourmodulefilewithadoc
string on top. Generally, that doc string should give the purpose of the module and
explainhowthemoduleisused.Morecomprehensivedocstringsareoftenrequired
for largerandmore“complicated”modules (here,ourdocstring isverysimple,but
professionalprogrammerswrite theirdocstringswithgreatcare10).Themodulefile
reads:
"""
Module for computing vertical motion
characteristics for a projectile.
"""
def y(v0, t):
"""
Compute vertical position at time t, given the initial vertical
velocity v0. Assume negligible air resistance.
"""
g = 9.81
return v0*t - 0.5*g*t**2
def time_of_flight(v0):
"""
Compute time in the air, given the initial vertical
velocity v0. Assume negligible air resistance.
"""
g = 9.81
return 2*v0/g
def max_height(v0):
"""
Compute maximum height reached, given the initial vertical
velocity v0. Assume negligible air resistance.
"""
g = 9.81
return v0**2/(2*g)
# Other function definitions could be added here...
As with built-in modules, the built-in help function can be used to retrieve
documentationfromuser-definedmodules:
In [1]: import vertical_motion
In [2]: help(vertical_motion)
Help on module vertical_motion:
NAME
vertical_motion
FILE
/.../.../.../vertical_motion.py
10 https://www.python.org/dev/peps/pep-0257/.
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