A Python module is a file containing Python code. Such files can contain classes, functions and constants. They can be can be grouped together in a package. Packages also serve to create separate namespaces so that two classes of the same name can exist and be used in the same code as long as they belong to different packages.
To start using modules, you will need to import them first using "import" command. We considered already such command at the very start of this tutorial in Sect.Arithmetic Let us repeat the previous example:
We imported the entire package "math" with mathematical functions. Or you may consider to import only one function:
Alternatively, you simply import all functions (in this case you do not need to type "math." on front of each statement
How do we know what is inside the package "math"? You can list all functions and classes implemented in this package calling the build-in function dir():
Let us consider another module "random" which generates random numbers:
|Exercise: Import modules "os" and "sys" and print all the methods:|