Page - 116 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 116 -
Text of the Page - 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, Volume Second Edition
- Title
- Programming for Computations – Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Volume
- Second Edition
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2020
- Language
- English
- License
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 356
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik