14
1 The First Few Steps
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 required by the latter. Both techniques are commonly used and represent the
two basic ways of importing library code in Python. Importing code is an evident
part of Python programming, so we better shed some more light on it.
1.4 Importing from Modules and Packages
At first, it may seem cumbersome to have code 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
handful of libraries used in this book is listed below (Sect. 1.4.5). Having everything
available at any time would be convenient, but this would also mean that computer
memory would be filled with a lot of unused information, causing less memory to be
available for computations on big data. Python has so many libraries, with so much
functionality, that importing what is needed is indeed a very sensible strategy.
Where to Place Import Statements? The general recommendation is to place
import statements at the top of a program, making them easy to spot.
1.4.1 Importing for Use Without Prefix
The need to prefix item names is avoided when import statements are on 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
become easier to read, since code generally comes “closer” to mathematical writing.
On the other hand, it is less evident where imported items “come from” and this may
complicate matters, particularly when trying to understand more comprehensive
code written by others.
10 There is a built-in function called dir, 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 by dir(math) or help(math).
1 The First Few Steps
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 required by the latter. Both techniques are commonly used and represent the
two basic ways of importing library code in Python. Importing code is an evident
part of Python programming, so we better shed some more light on it.
1.4 Importing from Modules and Packages
At first, it may seem cumbersome to have code 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
handful of libraries used in this book is listed below (Sect. 1.4.5). Having everything
available at any time would be convenient, but this would also mean that computer
memory would be filled with a lot of unused information, causing less memory to be
available for computations on big data. Python has so many libraries, with so much
functionality, that importing what is needed is indeed a very sensible strategy.
Where to Place Import Statements? The general recommendation is to place
import statements at the top of a program, making them easy to spot.
1.4.1 Importing for Use Without Prefix
The need to prefix item names is avoided when import statements are on 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
become easier to read, since code generally comes “closer” to mathematical writing.
On the other hand, it is less evident where imported items “come from” and this may
complicate matters, particularly when trying to understand more comprehensive
code written by others.
10 There is a built-in function called dir, 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 by dir(math) or help(math).
