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
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))
import math→ use asmath.sqrt.from math import pi→ usepidirectly.import math as m→ short aliasm.
Useful Built-in Modules
| Module | Used For |
|---|---|
| math | sqrt, pi, factorial, pow |
| random | random numbers, choice, shuffle |
| datetime | current date and time |
| os | files and folders |
| statistics | mean, median, mode |
Program 1: Using math
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))Program 2: Using random
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)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:
def add(a, b):
return a + b
def greet(name):
return "Hello " + nameThen use it in another file:
import mytools
print(mytools.add(5, 3))
print(mytools.greet("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.
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
randomoutput to be the same every run.
Practice Tasks
- Use
mathto find the area of a circle (pi r squared). - Use
randomto simulate rolling two dice and print the sum. - Make a module with two functions and import it into another file.
- Use the
statisticsmodule 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, orimport ... 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 के तरीके
import math # पूरा module import print(math.sqrt(16)) from math import pi # एक नाम import print(pi) import math as m # छोटा alias print(m.factorial(5))
import math→math.sqrtके रूप में।from math import pi→ सीधेpi।import math as m→ छोटा aliasm।
उपयोगी Built-in Modules
| Module | किसके लिए |
|---|---|
| math | sqrt, pi, factorial, pow |
| random | random numbers, choice, shuffle |
| datetime | current date और time |
| os | files और folders |
| statistics | mean, median, mode |
Program 1: math का Use
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))Program 2: random का Use
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)randint range में पूर्ण संख्या देता है; choice एक item चुनता है; shuffle list मिलाता है। (हर बार output बदलता है।)
Program 3: अपना Module
कोई भी .py file आपका module हो सकती है। mytools.py बनाएं:
def add(a, b):
return a + b
def greet(name):
return "Hello " + nameफिर दूसरी file में use करें:
import mytools
print(mytools.add(5, 3))
print(mytools.greet("Aman"))Packages और pip
Package modules का folder है। कई packages built-in नहीं होते — आप उन्हें pip (Python के package installer) से install करते हैं।
pip install numpy pip install pandas
Install के बाद import numpy करके किसी भी module की तरह use करें।
सामान्य गलतियाँ
- अपनी file का नाम असली module जैसा रखना (जैसे
random.py) — imports confuse होते हैं। - import से पहले pip से package install करना भूलना।
randomoutput हर run में same होने की उम्मीद करना।
Practice Tasks
mathसे circle का area निकालें (pi r squared)।randomसे दो dice roll simulate करके sum print करें।- दो functions वाला module बनाकर दूसरी file में import करें।
statisticsmodule से 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 करें।