fbpx

A Python module is a file or group of files that contain function definitions and statements.

A module can be created by creating a .py file.

For instance, we can create a file hello.py with the following code:

def say_hello():
    print("Hello World!")

We can then import this file:

import hello

hello.say_hello()

This will give the output

>>> import hello
>>> hello.say_hello()
Hello World!

We can also import specific functions from the module such as:

>>> from hello import say_hello
>>> say_hello()
Hello World!

If the file has more than one function, you can simply import * such as

>>> from hello import *
>>> say_hello()
Hello World!

Great, we just created a simple Python module

If you a beginner and want to learn the basics of Python, check out our Python for Beginners course

Also, check out our other courses https://learn.doitwithcode.com/