DevNet Associate DEVASC 200-901 Official Certification Guide by Adrian Iliesiu (z-lib.org)

(andrew) #1

those included as part of the standard library in Python
are built this way.


Importing a Module


All modules are accessed the same way in Python: by
using the import command. Within a program—by
convention at the very beginning of the code—you type
import followed by the module name you want to use.
The following example uses the math module from the
standard library:


Click here to view code image


>>> import math
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__',
'__package__',
'__spec__', 'acos', 'acosh', 'asin', 'asinh',
'atan', 'atan2',
'atanh', 'ceil', 'comb', 'copysign', 'cos',
'cosh', 'degrees',
'dist', 'e', 'erf', 'erfc', 'exp', 'expm1',
'fabs', 'factorial',
'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd',
'hypot', 'inf',
'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt',
'ldexp', 'lgam-
ma', 'log', 'log10', 'log1p', 'log2', 'modf',
'nan', 'perm', 'pi',
'pow', 'prod', 'radians', 'remainder', 'sin',
'sinh', 'sqrt',
'tan', 'tanh', 'tau', 'trunc']

After you import a module, you can use the dir()
function to get a list of all the methods available as part
of the module. The ones in the beginning with the __ are
internal to Python and are not generally useful in your
programs. All the others, however, are functions that are
now available for your program to access. As shown in
Example 4-4, you can use the help() function to get
more details and read the documentation on the math
module.


Example 4-4 math Module Help

Click here to view code image

Free download pdf