Using Modules

Importing modules is very intuitive.

import random

The code from above will generate a random number between 1 and 9, including 9. There can be generated 9 different numbers. We imported the module random and we called the randint method.

We can use the as keyword associated with import to rename the module, to be easyer to call it.

import random as r

If you do not want to remember this, you can use an alternative:

import random
x = random

There is another way to import modules. In the previous examples, when we called the function randint we had to specify the module where the function can be found.

We can use from keyword to avoid this by importing all the methods of the module in the current namespace.

from random import randint
print(randint(1,9)) # as you can see, there is no need to call this method on
	# an object

If we wanted to import more methods/functions from a module we can enumerate them separated with commas. For example:

from module_name import f1, f2, f3

If we want to import all functions from a module:

from module_name import *