In addition to the commands and function available normally in Python, you can access many more by importing modules. Python has many built in modules, and others that can be imported after installing them.
To import a built-in module or installed module, use the import keyword followed by the name of all the modules you wish to import. For now, we'll just use the math and random modules. This import statement should come at the beginning of your code, and must come before you use the given module.
import math imports math
import math, random imports math and random
Having only certain modules load when our program runs can decrease the amount of time our program takes to load. It also frees up names of different functions and variables that may already be in use in a different module.
Also, sometimes we will want to import modules that aren't a part of standard Python. This is much better than having to rewrite all of these from scratch or having to copy and paste.
Sometimes we want to import a specific method or variable from a module.
from math import sqrt imports sqrt() from the math class
from math import pi imports pi from the math class
from math import * imports everything from the math class
If you import a specific method or variable, you no longer need to reference the module to call the method or refer to the variable.
Instead of math.sqrt(3), we can write sqrt(3).
Sometimes modules or methods have long clunky names, and we want to give a nickname, or alias, that refers to the same thing. We can do this with the as keyword.
For example string.ascii_lowercase will give you a list with all of the lower case letters a to z, but it's a bit long and hard to remember.
from string import ascii_lowercase as lc_letters
Now, instead of string.ascii_lowercase or ascii_lowercase, we can use lc_letters.