Seite - 14 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 14 -
Text der Seite - 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).
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