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

Page - 13 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 13 -

Image of the Page - 13 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 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.
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
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
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python