Python Modules in Python – Complete Beginner Guide (2026)

 What is a Module in Python?

Python Modules in Python – Complete Beginner Guide (2026)

Introduction

As your Python programs become larger, writing all the code in a single file becomes difficult to manage. Python solves this problem using modules and packages.

A module is a Python file (.py) that contains reusable code such as variables, functions, and classes. Instead of writing the same code repeatedly, you can create a module once and import it whenever needed.

Python also provides thousands of built-in modules that help developers perform mathematical calculations, generate random numbers, work with dates and times, manage files and folders, and much more.

In this tutorial, you will learn:

  • What is a Module?
  • How to Import a Module
  • How to Create Your Own Module
  • What are Packages?
  • Built-in Python Modules
  • Math Module
  • Random Module
  • Datetime Module
  • OS Module

What is a Module in Python?

A module is simply a Python file that contains reusable code.

It helps organize programs into separate files, making the code cleaner, reusable, and easier to maintain.

For example, instead of writing a calculator function in every program, you can create it once inside a module and import it whenever needed.

Example

calculator.py 

def add(a, b):

    return a + b

Now this file becomes a module.


Why Do We Use Modules?

Modules make programming easier because they:

  • Reduce code duplication
  • Improve code readability
  • Make programs easier to maintain
  • Help organize large projects
  • Allow code reuse
  • Support teamwork in software development

Import Module in Python

Python uses the import keyword to load modules.

Syntax

import module_name

Example

math.py 

import math 

print(math.sqrt(25))

Output

5.0

Python loads the module and allows access using the dot (.) operator.


Import Specific Function

Instead of importing the whole module, you can import only the required function.

from math import sqrt

 print(sqrt(64))

Output

8.0


Import Multiple Functions

from math import sqrt, factorial

 print(sqrt(49))

print(factorial(5))

Output

7.0

120


Import with Alias

Sometimes module names are long.

Python allows giving them shorter names.

import math as m 

print(m.sqrt(36))

Output

6.0


Create Your Own Module

Creating a module is very simple.

Step 1

Create a file named

greetings.py

Write the following code.

def welcome(name):

    print("Welcome", name)


Step 2

Create another file

main.py

Import the module.

import greetings

 greetings.welcome("Rahul")

Output

Welcome Rahul

Now your function can be used in multiple programs.


What are Packages in Python?

A package is a collection of related modules stored inside a folder.

A package helps organize large Python projects.

Example Folder Structure

project/

 calculator/

     add.py

     subtract.py

     multiply.py

 main.py

Each file is a module.

The folder calculator becomes a package (typically with an __init__.py file, though modern Python also supports namespace packages in some cases).

Example

from calculator import add

Packages make projects easier to understand and maintain.


Built-in Modules in Python

Python already provides many useful modules.

These are called built-in modules.

Some popular built-in modules are:

  • math
  • random
  • datetime
  • os
  • statistics
  • time
  • calendar
  • json
  • csv
  • sys

These modules save time because developers do not need to write common functionality from scratch.


Math Module in Python

The math module provides mathematical functions.

Import the module

import math


Square Root

import math

 print(math.sqrt(64))

Output

8.0


Power

import math

 print(math.pow(2,5))

Output

32.0


Pi Value

import math

 print(math.pi)

Output

3.141592653589793


Ceiling

Rounds upward.

import math

 print(math.ceil(8.2))

Output

9


Floor

Rounds downward.

import math

 print(math.floor(8.9))

Output

8


Factorial

import math

 print(math.factorial(5))

Output

120


Common Math Functions

Function

Description

sqrt()

Square root

pow()               

Power

factorial()

Factorial

ceil()

Round up

floor()

Round down

pi

Pi constant


Random Module in Python

The random module generates random numbers and random selections.

Import module

import random


Random Integer

import random 

print(random.randint(1,10))

Output

7

(Output changes each time.)


Random Choice

import random

 fruits = ["Apple","Mango","Banana"]

 print(random.choice(fruits))

Possible 

Output

Mango


Shuffle List

import random

 numbers=[1,2,3,4,5]

 random.shuffle(numbers)

 print(numbers)

Output

[3,5,1,4,2]

(Random order.)


Datetime Module in Python

The datetime module helps work with dates and times.

Import

import datetime


Current Date and Time

import datetime

 today = datetime.datetime.now()

 print(today)

Output

2026-07-21 10:30:15

(Example output.)


Current Date

import datetime

 print(datetime.date.today())

Output

2026-07-21


Current Time

import datetime 

print(datetime.datetime.now().time())

Example 

Output

10:30:15


Create Custom Date

import datetime 

d = datetime.date(2026,12,25)

 print(d)

Output

2026-12-25


OS Module in Python

The os module allows Python to interact with the operating system.

Import

import os


Current Working Directory

import os

 print(os.getcwd())

Output

C:\PythonProjects


List Files

import os

 print(os.listdir())

Output

['main.py', 'test.py', 'images']


Create Folder

import os

 os.mkdir("PythonNotes")


Rename File

import os

 os.rename("old.txt","new.txt")


Remove File

import os

 os.remove("sample.txt")


Advantages of Modules

  • Reuse code easily
  • Reduce duplicate code
  • Organize projects efficiently
  • Improve readability
  • Simplify maintenance
  • Speed up development
  • Encourage modular programming

Difference Between Module and Package


 

Module

           Package

Single .py file

      Folder containing related modules

Stores functions, classes, variables 

      Organizes multiple modules

Smaller unit

      Larger organizational unit

Imported directly

     Can contain many modules and subpackages


Real-Life Example

Imagine you are building an Online Shopping Website.

Instead of writing everything in one file, you organize your project like this:

  • payment.py – Handles payments.
  • login.py – Manages user authentication.
  • products.py – Displays products.
  • orders.py – Processes customer orders.

These individual files are modules. You place them inside a folder named shopping, making it a package. This structure keeps the project organized, easier to maintain, and allows different developers to work on separate modules simultaneously.


Frequently Asked Questions (FAQs) 

1. What is a module in Python?

A module is a Python file (.py) that contains reusable code such as functions, classes, and variables. It helps organize programs and allows the same code to be used in multiple projects.


2. Why are modules used in Python?

Modules are used to:

  • Reuse code
  • Reduce duplication
  • Improve readability
  • Organize large projects
  • Simplify maintenance
  • Make teamwork easier

3. How do you import a module in Python?

You can import a module using the import keyword.

Example:

import math

 print(math.sqrt(25))


4. How can you import a specific function from a module?

Use the from keyword.

Example:

from math import sqrt

 print(sqrt(49))


5. What is the difference between import and from...import?

  • import module imports the entire module.
  • from module import function imports only the required function.

6. What is a package in Python?

A package is a folder containing one or more Python modules. It helps organize related modules into a single directory.


7. What are built-in modules?

Built-in modules are pre-installed Python libraries that provide ready-to-use functions and features.

Examples:

  • math
  • random
  • datetime
  • os
  • sys
  • json

8. What is the Math module used for?

The math module performs mathematical calculations such as:

  • Square root
  • Power
  • Factorial
  • Trigonometric functions
  • Logarithms

9. What is the Random module used for?

The random module generates random values, selects random items, and shuffles data.


10. What is the Datetime module used for?

The datetime module is used to work with:

  • Current date
  • Current time
  • Date calculations
  • Time formatting

Interview Questions 

1. What is a module in Python?

A module is a Python file that contains reusable functions, variables, and classes. It helps organize code and improves reusability.


2. What is the purpose of using modules?

Modules help reduce code duplication, improve code organization, simplify maintenance, and allow code reuse across multiple programs.


3. How do you import a module in Python?

import math


4. How do you import only one function from a module?

from math import sqrt


5. What is the difference between import math and from math import sqrt?

  • import math imports the entire module.
  • from math import sqrt imports only the sqrt() function.

6. What is a package?

A package is a directory containing multiple related Python modules. It helps organize large projects into logical groups.


7. What are built-in modules?

Built-in modules are libraries that come pre-installed with Python and provide commonly used functionality without requiring installation.


8. What is the Math module?

The math module provides mathematical functions such as sqrt(), pow(), factorial(), ceil(), and floor().


9. What is the Random module?

The random module is used to generate random numbers, randomly select items, and shuffle lists.


10. What is the Datetime module?

The datetime module helps work with dates and times, including getting the current date, current time, and performing date calculations.


Conclusion

Modules and packages are fundamental building blocks of Python programming. They help developers write clean, reusable, and organized code. By learning how to import modules, create your own modules, organize them into packages, and use powerful built-in modules like math, random, datetime, and os, you can build scalable Python applications more efficiently. Mastering these concepts early will make your Python development faster, cleaner, and more professional.

Comments

Popular posts from this blog

HTML Tag

HTML Input Type Submit Syntax and Example

CSS Text Color Explained with Syntax and HTML Examples