-
Hajipur, Bihar, 844101
Modules in Python help you organise your code into separate files so you can reuse functions, variables and classes whenever you need them. Instead of writing the same code again and again, you can place it inside a module and simply import it in any program. Modules make your projects cleaner, easier to manage and more efficient.
In this tutorial, you will learn what modules are, how to create your own module, how to import modules in different ways, how to use built-in modules, how packages work, and how modules improve code structure in real projects.
A module is a Python file that contains functions, variables and classes. Every file with a .py extension automatically becomes a module that you can import in another program.
For example, if you create a file named math_tools.py, it becomes a module called math_tools.
Modules help you:
reuse code
keep files small
organise projects
avoid repeating logic
Modules allow you to split a large project into logical parts. You can keep related code together instead of creating one huge file. This makes debugging easier and helps you work faster.
Some common reasons to use modules:
You want to use the same function in many programs
Your code is getting too long
You want to separate tasks, such as calculations, input handling or file operations
You want to share your functions with others
Python supports three main types of modules:
Built-in modules (already available in Python)
User-defined modules (modules you create)
Third-party modules (installed using PIP)
You will work with all three in real projects.
Python has many modules ready to use. You don’t need to install anything. Just import and start using them.
Examples of built-in modules:
math
random
datetime
os
json
Example:
import math
print(math.sqrt(25))
To create a module, simply create a new .py file.
Example: greeting.py
def welcome(name):
return "Hello " + name
Now you can use this module in another file.
To import a module, use the import keyword.
Example:
import greeting
print(greeting.welcome("Aarohi"))
Here, greeting is the module and welcome is the function inside it.
Instead of importing the whole module, you can import only what you need.
Example:
from greeting import welcome
print(welcome("Bhoomi"))
This saves time and makes the code cleaner.
Sometimes module names are long, so you can use an alias.
Example:
import greeting as g
print(g.welcome("Tara"))
This approach is common in Python projects.
Modules can also hold variables.
Example: data.py
title = "Python Learning"
year = 2025
Use them like this:
import data
print(data.title)
print(data.year)
The dir() function shows all functions and variables inside a module.
Example:
import math
print(dir(math))
This helps you explore what a module can do.
Python has a rich standard library. Here are a few useful examples.
math module exampleimport math
print(math.pi)
print(math.pow(2, 3))
random module exampleimport random
print(random.randint(1, 10))
datetime module exampleimport datetime
print(datetime.datetime.now())
A package is a group of modules inside a folder. If you want to organise modules even more, you can use packages.
A package must contain a file named __init__.py. This file tells Python that the folder is a package.
Example structure:
myapp/
__init__.py
tools.py
helpers.py
You can import modules like this:
from myapp import tools
from myapp.helpers import info
Packages help you build large-scale applications.
Imagine you want to build a calculator.
Folder:
calculator/
__init__.py
add.py
subtract.py
File: add.py
def add(a, b):
return a + b
File: subtract.py
def sub(a, b):
return a - b
Use it:
from calculator.add import add
from calculator.subtract import sub
print(add(10, 5))
print(sub(10, 5))
This approach is clean and easy to maintain.
Importing a full module
import math
print(math.sqrt(16))
Importing specific functions
from math import ceil
print(ceil(4.2))
Using an alias
import random as r
print(r.choice([1, 2, 3]))
User-defined module
import greeting
print(greeting.welcome("Kavya"))
Module with variables
import data
print(data.title)
Nested package import
from calculator.add import add
Getting module info
import os
print(dir(os))
Random number generation
import random
print(random.random())
Current date and time
import datetime
print(datetime.date.today())
Using math constants
import math
print(math.pi)
Modules allow you to organise Python code into separate files so you can reuse functions, variables and classes. Python includes many built-in modules, and you can create your own or install third-party modules with PIP. You learned how to import modules, use aliases, explore module contents and create packages. Modules keep your programs clean, manageable and easy to extend as your projects grow.
Q1. Write a Python program to import the math module and use the ceil() function to round up a number.
Q2. Write a Python program to use random.choice() to pick a random item from a list of your favorite fruits.
Q3. Write a Python program to import the datetime module with an alias dt and print the current date.
Q4. Write a Python program to create a module file calc.py with an add() function that adds two numbers.
Q5. Write a Python program to import add() from the calc module and use it in your main script.
Q6. Write a Python program to use from math import pi and print the value of pi.
Q7. Write a Python program to try importing a non-existing module and handle the ModuleNotFoundError using try/except.
Q8. Write a Python program to use the os module to get and print the current working directory.
Q9. Write a Python program to use dir() to list all attributes and functions of the datetime module.
Q10. Write a Python program to call a function from your own custom module stored in a separate .py file.