Editor settings

General settings

Save settings

Learn to program with Python

What is Python module #

In Python, a module is a file that contains Python definitions and statements. The file name is the module name with the suffix .py. A module can define functions, classes, and variables, and can also include runnable code. By using modules, we can organize code and make it more reusable.

Exercise #

Creating a module #

Since modules are simply files containing Python definitions and statements that can be imported and used in other Python code, creating a module is very simple.

In this lesson, we have already created one file: main.py. This is our main file, which is the entrypoint to the program.

Create a new Python file with a .py extension in the same directory as your main Python file. Click on the + on top of the editor to add a new file. Let's name this file greetings.py.

Here's what our directory looks like after a new file is created. You can see that both files are in the same level.

.
├── greetings.py
└── main.py

Now, go ahead and copy the function below to the greetings.py file:

def greet(name):
    print(f"Hello {name}!")

Tests #

  • Create a new file named greetings.py
  • Create a new function named greet in the file greetings.py
Output will be displayed here