18
1 The First Few Steps
1.4.3 Imports with Name Change
Whether we import for use with or without prefix, we may change names of the imported items by minor adjustments of the import statements. Introducing such name
changes in our program and saving this final version as check_functions.py, it
reads
import numpy as np
import math as m
x = np.exp([0, 1, 2])
# do all 3 calculations
print(x)
# print all 3 results
y = m.cos(0)
print(y)
Effectively, the module names in this program now become np and m (by our
own choice) instead of numpy and math, respectively. We still enjoy the safety
of prefixing and notice that such name changes might bring computer coded
expressions closer to mathematical writing and thus ease human interpretation.
When importing library items for use without prefix, name changes can be done,
e.g., like
from math import cos as c, sin as s
print(c(0) + s(0))
1.4.4 Importing from Packages
Modules may be grouped into packages, often together with functions, variables,
and more. We may import items (modules, functions, etc.) from such packages
also, but the appearance of an import statement will then depend on the structure
of the package in question. We leave out the details 14 and just exemplify with two
packages often used in this book.
The numpy library used above is, in fact, a package and we saw how it could be
used with different import statements, just as if it had been a module. Note that the
import statement
import numpy as np
# standard way of importing numpy
is the standard way of importing numpy, i.e., also the “nickname” np is standard.
This will be the standard import technique for numpy also in our book, meaning that
we will generally use numpy items with the np prefix. We will deviate from this at
times, typically during brief interactive sessions (see Sect. 2.1), in which case we
will import items explicitly specified by name.
Another popular package you will meet often in this book, is the plotting library
matplotlib (Sect. 1.5), used for generating and handling plots. The standard
import statement, including the “nickname”, is then
import matplotlib.pyplot as plt
# standard way of importing pyplot
14 If you are curious, check for more details at https://docs.python.org/3.6/tutorial/modules.html.
1 The First Few Steps
1.4.3 Imports with Name Change
Whether we import for use with or without prefix, we may change names of the imported items by minor adjustments of the import statements. Introducing such name
changes in our program and saving this final version as check_functions.py, it
reads
import numpy as np
import math as m
x = np.exp([0, 1, 2])
# do all 3 calculations
print(x)
# print all 3 results
y = m.cos(0)
print(y)
Effectively, the module names in this program now become np and m (by our
own choice) instead of numpy and math, respectively. We still enjoy the safety
of prefixing and notice that such name changes might bring computer coded
expressions closer to mathematical writing and thus ease human interpretation.
When importing library items for use without prefix, name changes can be done,
e.g., like
from math import cos as c, sin as s
print(c(0) + s(0))
1.4.4 Importing from Packages
Modules may be grouped into packages, often together with functions, variables,
and more. We may import items (modules, functions, etc.) from such packages
also, but the appearance of an import statement will then depend on the structure
of the package in question. We leave out the details 14 and just exemplify with two
packages often used in this book.
The numpy library used above is, in fact, a package and we saw how it could be
used with different import statements, just as if it had been a module. Note that the
import statement
import numpy as np
# standard way of importing numpy
is the standard way of importing numpy, i.e., also the “nickname” np is standard.
This will be the standard import technique for numpy also in our book, meaning that
we will generally use numpy items with the np prefix. We will deviate from this at
times, typically during brief interactive sessions (see Sect. 2.1), in which case we
will import items explicitly specified by name.
Another popular package you will meet often in this book, is the plotting library
matplotlib (Sect. 1.5), used for generating and handling plots. The standard
import statement, including the “nickname”, is then
import matplotlib.pyplot as plt
# standard way of importing pyplot
14 If you are curious, check for more details at https://docs.python.org/3.6/tutorial/modules.html.
