🟢 Beginner  ·  Lesson 17

Modules and Packages

What is a Module?

A module is a Python file full of ready-made code (functions, variables) that you can reuse. Instead of writing everything yourself, you import a module and use its tools. A package is a folder of related modules.

Ways to Import

Python – imports.py
import math               # import the whole module
print(math.sqrt(16))

from math import pi       # import one name
print(pi)

import math as m          # give it a short alias
print(m.factorial(5))
4.0 3.141592653589793 120
  • import math → use as math.sqrt.
  • from math import pi → use pi directly.
  • import math as m → short alias m.

Useful Built-in Modules

ModuleUsed For
mathsqrt, pi, factorial, pow
randomrandom numbers, choice, shuffle
datetimecurrent date and time
osfiles and folders
statisticsmean, median, mode

Program 1: Using math

Python – usemath.py
import math
print("Square root of 25 :", math.sqrt(25))
print("5 factorial       :", math.factorial(5))
print("Value of pi       :", round(math.pi, 4))
print("2 to power 10     :", math.pow(2, 10))
Square root of 25 : 5.0 5 factorial : 120 Value of pi : 3.1416 2 to power 10 : 1024.0

Program 2: Using random

Python – userandom.py
import random
print("Random 1-6  :", random.randint(1, 6))   # dice roll
fruits = ["apple", "mango", "kiwi"]
print("Random fruit:", random.choice(fruits))
random.shuffle(fruits)
print("Shuffled    :", fruits)
Random 1-6 : 4 Random fruit: mango Shuffled : ['kiwi', 'apple', 'mango']

randint gives a whole number in a range; choice picks one item; shuffle mixes a list. (Output changes each run.)

Program 3: Your Own Module

Any .py file can be your own module. Create mytools.py:

Python – mytools.py
def add(a, b):
    return a + b

def greet(name):
    return "Hello " + name

Then use it in another file:

Python – main.py
import mytools
print(mytools.add(5, 3))
print(mytools.greet("Aman"))
8 Hello Aman

Packages & pip

A package is a folder of modules. Many packages are not built in — you install them with pip, Python's package installer.

Terminal
pip install numpy
pip install pandas

After installing, import numpy and use it like any module.

Common Mistakes

  • Naming your file the same as a real module (e.g. random.py) — it confuses imports.
  • Forgetting to install a package with pip before importing.
  • Expecting random output to be the same every run.

Practice Tasks

  1. Use math to find the area of a circle (pi r squared).
  2. Use random to simulate rolling two dice and print the sum.
  3. Make a module with two functions and import it into another file.
  4. Use the statistics module to find the mean of a list.

Summary

  • A module is a reusable Python file; a package is a folder of modules.
  • Import with import, from ... import, or import ... as.
  • Built-in: math, random, datetime, os, statistics.
  • Install extra packages with pip install.
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.