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 - 14 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

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

Image of the Page - 14 -

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

Text of the Page - 14 -

14 1 TheFirstFewSteps Alternatively, we could use the import statement import math. This would require atan and pi to be prefixed with math, however, as shown in ball_angle_prefix.py: import math x = 10.0 # Horizontal position y = 10.0 # Vertical position angle = math.atan(y/x) print (angle/math.pi)*180 The essential difference between the two import techniques shown here, is the prefix requiredby the latter. Both techniquesare commonlyused and represent the two basic ways of importing library code in Python. Importing code is an evident partofPythonprogramming,sowe better shedsomemore lighton it. 1.4 ImportingfromModulesandPackages At first, it may seem cumbersometo havecode in different libraries, since it means you have to know (or find out) what resides in which library.10 Also, there are many libraries around in addition to the Python standard library itself. To your comfort, you come a long way with just a few libraries, and for easy reference, the handfulof librariesusedinthisbookis listedbelow(Sect.1.4.5).Havingeverything available at any time would be convenient,but this would also mean that computer memorywouldbefilledwithalotofunusedinformation,causinglessmemorytobe available forcomputationsonbigdata.Pythonhasso manylibraries,with so much functionality, that importingwhat isneeded is indeeda verysensible strategy. Where to Place Import Statements? The general recommendation is to place import statementsat the topofaprogram,making themeasy tospot. 1.4.1 ImportingforUseWithoutPrefix Theneed toprefix itemnames is avoidedwhen import statementsareon the form from some_library import ... # i.e., items will be used without prefix as we saw in ball_angle.py above. Without prefixing, coded formulas often becomeeasier toread,sincecodegenerallycomes“closer”tomathematicalwriting. Ontheotherhand,it is lessevidentwhereimporteditems“comefrom”andthismay complicate matters, particularly when trying to understand more comprehensive codewrittenbyothers. 10 There is a built-in function calleddir, which gives all the names defined in a library module. Another built-in function called help prints documentation. To see how they work, write (in Spyder, pane down to the right)import math followed bydir(math)orhelp(math).
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