Seite - 8 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 8 -
Text der Seite - 8 -
8 1 TheFirstFewSteps
y/x as input parameter or argument. The atan function takes one argument,
and the computed value is returned from atan. This means that where we see
atan(y/x),a computation isperformed(tan 1.y=x/)and theresult “replaces” the
textatan(y/x). This is actually nomoremagic than ifwe hadwritten just y/x:
then thecomputationofy/xwould takeplace, and the resultof thatdivisionwould
replace the text y/x. Thereafter, the result is assigned to the name angle on the
left-handsideof=.
Note that the trigonometricfunctions, suchasatan,workwithanglesinradians.
The return value ofatanmust hence be converted to degrees, and that iswhywe
perform the computation (angle/pi)*180. Two things happen in the print
statement: first, the computation of (angle/pi)*180 is performed, resulting in
arealnumber,andsecond,printprints that realnumber.Again,wemaythinkthat
the arithmetic expression is replaced by its results and then print starts working
with that result.
If we next execute ball_angle_first_try.py, we get an errormessage on
thescreensaying
NameError: name ’atan’ is not defined
WARNING: Failure executing file: <ball_angle_first_try.py>
Wehavedefinitely run into trouble,butwhy?Weare told that
name ’atan’ is not defined
so apparently Python does not recognize this part of the code as anything famil-
iar. On a pocket calculator the inverse tangent function is straightforward to use
in a similarway aswehavewritten in the code. InPython, however, this function
has not yet been imported into the program. A lot of functionality is available to
us in aprogram,butmuchmore functionality exists inPython libraries, and to ac-
tivate this functionality,wemust explicitly import it. InPython, theatan function
is grouped togetherwithmany othermathematical functions in the library called
math. Such a library is referred to as amodule in correctPython language. Toget
access toatan inourprogramwehave towrite
from math import atan
Inserting this statement at the top of the programand rerunning it, leads to a new
problem:pi isnotdefined. Thevariablepi, representing , is alsoavailable in the
mathmodule,but it has tobe imported too:
from math import atan, pi
It is tedious if youneedquite somemath functionsandvariables inyourprogram,
e.g., alsosin,cos,log,exp, and so on. Aquickway of importing everything in
mathatonce, is
from math import *
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2016
- Sprache
- englisch
- Lizenz
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 248
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik