Seite - 13 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 13 -
Text der Seite - 13 -
1.3 APythonProgramwithaLibraryFunction 13
y/xwould take place, and the result of that division would replace the text y/x.
Thereafter, the result is assigned toangleon the left-handside of=.
Note that the trigonometric functions, such as atan, work with angles in
radians. Thus, if we want the answer in degrees, the return value of atan
must be converted accordingly. This conversion is performed by the computation
(angle/pi)*180.Two things happen in theprint command: first, the computa-
tion of(angle/pi)*180 is performed, resulting in a number, and second,print
prints that number. Again, we may think that the arithmetic expression is replaced
by its result and thenprint startsworkingwith that result.
Running the Program If we next executeball_angle_first_try.py, we get
anerrormessageon thescreensaying
NameError: name ’atan’ is not defined
WARNING: Failure executing file: <ball_angle_first_try.py>
We havedefinitely run into trouble,butwhy?We are told that
name ’atan’ is not defined
so apparently Python does not recognize this part of the code as anything familiar.
On a pocket calculator the inverse tangent function is straightforward to use in a
similarwayaswehavewritten in thecode. InPython,however, this function isone
of many that must be imported before use. A lot of functionality9 is immediately
available to us (from the Python standard library) as we start a new programming
session, but much more functionality exists in additional Python libraries. To
activate functionality from these libraries, we must explicitly import it. In Python,
theatan function is groupedtogetherwith manyothermathematical functions ina
librarymodulecalledmath.Togetaccess toatan inourprogram,wemaywritean
import statement:
from math import atan
Inserting this statement at the top of the program and rerunning it, leads to a new
problem:pi isnot defined.Theconstantpi, representingπ, is also available in the
mathmodule, but it has to be imported too. We can achieve this by including both
itemsatanandpi in the import statement,
from math import atan, pi
With this latter statement inplace,we save thecodeasball_angle.py:
from math import atan, pi
x = 10.0 # Horizontal position
y = 10.0 # Vertical position
angle = atan(y/x)
print((angle/pi)*180)
Thisscript correctlyproduces45.0asoutputwhenexecuted.
9 https://docs.python.org/3/library/functions.html.
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