Lesson 1 of 0
In Progress

Lesson #15: Modules

A module is a file containing Python code that can be reused in other Python code files.

Create The First Module

To create a module just save the code you want in a file with the file extension .py.

In this example, we will create a file named adder.py with the following code.

def add(x, y):
  return x + y

Import The Module

Now we can use the module we just created, by using the import statement.

import adder

results = adder.add(10, 20)
print(results)

Renaming a Module

You can create an alias when you import a module, by using the as keyword.

In this example, we will create an alias for the module adder.py.

import adder as a

results = a.add(10, 20)
print(results)