🟢 Beginner  ·  Lesson 17

Modules and Packages

Modules और 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.

Module क्या है?

Module एक Python file है जो ready-made code (functions, variables) से भरी होती है जिसे आप reuse कर सकते हैं। सब खुद लिखने के बजाय, आप module import करके उसके tools use करते हैं। package related modules का folder है।

Import के तरीके

Python – imports.py
import math               # पूरा module import
print(math.sqrt(16))

from math import pi       # एक नाम import
print(pi)

import math as m          # छोटा alias
print(m.factorial(5))
4.0 3.141592653589793 120
  • import mathmath.sqrt के रूप में।
  • from math import pi → सीधे pi
  • import math as m → छोटा alias m

उपयोगी Built-in Modules

Moduleकिसके लिए
mathsqrt, pi, factorial, pow
randomrandom numbers, choice, shuffle
datetimecurrent date और time
osfiles और folders
statisticsmean, median, mode

Program 1: math का Use

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: random का Use

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 range में पूर्ण संख्या देता है; choice एक item चुनता है; shuffle list मिलाता है। (हर बार output बदलता है।)

Program 3: अपना Module

कोई भी .py file आपका module हो सकती है। mytools.py बनाएं:

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

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

फिर दूसरी file में use करें:

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

Packages और pip

Package modules का folder है। कई packages built-in नहीं होते — आप उन्हें pip (Python के package installer) से install करते हैं।

Terminal
pip install numpy
pip install pandas

Install के बाद import numpy करके किसी भी module की तरह use करें।

सामान्य गलतियाँ

  • अपनी file का नाम असली module जैसा रखना (जैसे random.py) — imports confuse होते हैं।
  • import से पहले pip से package install करना भूलना।
  • random output हर run में same होने की उम्मीद करना।

Practice Tasks

  1. math से circle का area निकालें (pi r squared)।
  2. random से दो dice roll simulate करके sum print करें।
  3. दो functions वाला module बनाकर दूसरी file में import करें।
  4. statistics module से list का mean निकालें।

सारांश

  • Module reusable Python file है; package modules का folder।
  • Import: import, from ... import, या import ... as
  • Built-in: math, random, datetime, os, statistics।
  • Extra packages pip install से install करें।
← Back to Python Tutorial
🔗

Share this topic with a friend

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

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

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

WhatsApp