Labs ICT
โญ Pro Login

Modules

Modules

A module is a .py file containing functions, classes, and variables. Use import to bring its contents into your program.

Importing a Module

import math

print(math.sqrt(16))
print(math.pi)

from random import randint
print(randint(1, 10))
Try it Yourself โ†’

Creating Your Own Module

Save this as mymodule.py:

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

PI = 3.14159

Then use it in another script:

import mymodule

print(mymodule.greet("Alice"))
print(mymodule.PI)
print(dir(mymodule))
Try it Yourself โ†’

๐Ÿงช Quick Quiz

How do you import only a specific function from a module?