Labs ICT
๐ŸŽ“ SIWES/IT Login

Modules

1 min read | Python Tutorial
๐ŸŽ“

Doing SIWES / IT attachment?

Get a structured training programme, a supervised digital logbook, and a completion certificate with LabsICT SIWES / IT.

Explore SIWES / IT Training

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))

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))

๐Ÿงช Quick Quiz

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