Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Page - 8 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 8 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Image of the Page - 8 -

Image of the Page - 8 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text of the Page - 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 *
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2016
Language
English
License
CC BY-NC 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
248
Keywords
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Category
Informatik
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python