Python Modules & Packages क्या है? import, from, as, math, random in Hindi | RBSE Class 12

इस लेख में: Python Module और Package क्या होते हैं, import, from और as statements, Built-in Modules, User-defined Modules, math, random और datetime modules को आसान Hindi में examples के साथ समझेंगे।

Python Module क्या है?

Module Python की एक file होती है जिसमें Python code, जैसे functions, classes और variables, को organize करके रखा जा सकता है। Python module की file का extension सामान्यतः .py होता है।

Modules का मुख्य फायदा यह है कि code को अलग-अलग files में व्यवस्थित करके आवश्यकता के अनुसार reuse किया जा सकता है।

Example

# calculator.py

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

ऊपर calculator.py एक user-defined module का उदाहरण है।

Module का उपयोग क्यों करते हैं?

  • Code को व्यवस्थित रखने के लिए।
  • Code को reuse करने के लिए।
  • बड़े programs को छोटे parts में divide करने के लिए।
  • Functions और classes को अलग-अलग files में रखने के लिए।
  • Existing Python libraries का उपयोग करने के लिए।
Exam Tip: Python में module एक Python file होती है जिसमें reusable code रखा जा सकता है।

Python में Module Import करना

किसी module के code को अपने program में उपयोग करने के लिए import statement का उपयोग किया जा सकता है।

Syntax

import module_name

Example

import math

print(math.sqrt(25))

Output:

5.0

import Statement

import statement module को program में उपलब्ध कराने के लिए उपयोग किया जाता है।

import math

print(math.pi)
print(math.sqrt(16))

from Statement

किसी module से specific function या object import करने के लिए from ... import syntax का उपयोग किया जा सकता है।

from math import sqrt

print(sqrt(36))

Output:

6.0

एक से अधिक Functions Import करना

from math import sqrt, factorial

print(sqrt(25))
print(factorial(5))

Output:

5.0
120

as Keyword — Alias

Module या imported object को छोटा या दूसरा नाम देने के लिए as keyword का उपयोग किया जा सकता है।

import math as m

print(m.sqrt(49))
print(m.pi)

Output:

7.0
3.141592653589793
Exam Tip: import math as m में m math module का alias है।

Python Built-in Modules

Python के साथ कई useful modules उपलब्ध होते हैं। इनमें से कुछ commonly used modules हैं:

  • math
  • random
  • datetime
  • os
  • statistics
  • sys

math Module

math module mathematical functions और constants प्रदान करता है।

import math

print(math.pi)
print(math.sqrt(64))
print(math.factorial(5))

Output:

3.141592653589793
8.0
120

math.sqrt()

sqrt() function किसी number का square root निकालने के लिए उपयोग किया जाता है।

import math

result = math.sqrt(81)

print(result)

Output:

9.0

math.factorial()

factorial() function किसी non-negative integer का factorial निकालने के लिए उपयोग किया जाता है।

import math

print(math.factorial(5))

Output:

120

math.pow()

pow() function power calculation के लिए उपयोग किया जा सकता है।

import math

print(math.pow(2, 3))

Output:

8.0

math.ceil() और math.floor()

import math

print(math.ceil(4.2))
print(math.floor(4.8))

Output:

5
4

ceil() number को ऊपर की ओर nearest integer की ओर ले जाता है, जबकि floor() नीचे की ओर nearest integer की ओर ले जाता है।

random Module

random module pseudo-random values generate करने के लिए useful functions प्रदान करता है।

random.random()

import random

print(random.random())

यह सामान्यतः 0 और 1 के बीच एक floating-point value देता है।

random.randint()

randint(a, b) दिए गए range में integer generate करता है, जहाँ दोनों endpoints शामिल होते हैं।

import random

number = random.randint(1, 10)

print(number)

इस program में output हर execution में अलग हो सकता है।

random.choice()

choice() किसी sequence से एक random element चुनने के लिए उपयोग किया जाता है।

import random

colors = ["Red", "Green", "Blue"]

print(random.choice(colors))

random.shuffle()

shuffle() list के elements का order randomly बदलने के लिए उपयोग किया जाता है।

import random

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

random.shuffle(numbers)

print(numbers)
Note: shuffle() list को in-place modify करता है।

datetime Module

datetime module date और time से संबंधित operations के लिए उपयोगी है।

import datetime

today = datetime.date.today()

print(today)

Current Date और Time

import datetime

now = datetime.datetime.now()

print(now)

datetime के साथ Date बनाना

import datetime

date = datetime.date(2026, 8, 23)

print(date)

os Module

os module operating system से संबंधित कई operations के लिए functions उपलब्ध कराता है।

import os

print(os.getcwd())

getcwd() current working directory की जानकारी देता है।

statistics Module

statistics module statistical calculations के लिए useful functions प्रदान करता है।

import statistics

numbers = [10, 20, 30, 40, 50]

print(statistics.mean(numbers))

Output:

30

User-defined Module क्या है?

जब programmer स्वयं एक Python file बनाकर उसमें functions, classes या variables define करता है और उसे दूसरे Python program में import करता है, तो उसे user-defined module कहा जा सकता है।

Step 1 — Module बनाना

# mymodule.py

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

def add(a, b):
    return a + b

Step 2 — Module Import करना

import mymodule

print(mymodule.greet("Rahul"))
print(mymodule.add(10, 20))

Output:

Hello Rahul
30

User-defined Module से Specific Function Import करना

from mymodule import add

print(add(5, 10))

Output:

15

Module को Alias देना

import mymodule as mm

print(mm.add(10, 20))

Python Package क्या है?

Package related Python modules को एक organized structure में रखने का तरीका है। इससे बड़े projects में code को व्यवस्थित करना आसान हो सकता है।

एक simple package structure इस प्रकार हो सकता है:

myproject/
│
├── main.py
│
└── tools/
    ├── __init__.py
    ├── calculator.py
    └── message.py

Package से Module Import करना

from tools import calculator

print(calculator.add(10, 20))

Module और Package में अंतर

Module Package
आमतौर पर एक Python file होती है। Related modules को organize करने वाला structure होता है।
File extension सामान्यतः .py होती है। एक directory/package structure हो सकता है।
Example: calculator.py Example: tools package

__name__ और __main__

Python में __name__ एक special variable है। जब Python file directly run की जाती है, तो उस file में __name__ की value आमतौर पर "__main__" होती है।

def greet():
    print("Hello Python")


if __name__ == "__main__":
    greet()

इस pattern का उपयोग यह निर्धारित करने के लिए किया जा सकता है कि कोई code file direct execution के दौरान चले या import किए जाने पर नहीं।

Common Module Import Examples

Statement उपयोग
import math पूरा math module import करना
from math import sqrt केवल sqrt import करना
import math as m math को alias m देना
from math import sqrt, pi Specific objects import करना

Important Python Modules

Module मुख्य उपयोग
math Mathematical operations
random Random values और selections
datetime Date और time operations
os Operating system related operations
statistics Statistical calculations
sys Python runtime/system related functionality

Module के फायदे

  • Code reuse किया जा सकता है।
  • Program को छोटे modules में divide किया जा सकता है।
  • Code maintenance आसान हो सकती है।
  • Related functionality को एक जगह रखा जा सकता है।
  • Python की standard library का लाभ लिया जा सकता है।

RBSE Class 12 Important Questions

  1. Python Module क्या है?
  2. Module का उपयोग क्यों किया जाता है?
  3. Python में module import करने के लिए कौन-सा keyword उपयोग होता है?
  4. import statement का syntax लिखिए।
  5. from statement का उपयोग क्या है?
  6. as keyword का उपयोग क्या है?
  7. Built-in Module क्या है?
  8. User-defined Module क्या है?
  9. math module का उपयोग क्या है?
  10. math.sqrt() का उपयोग क्या है?
  11. math.factorial() क्या करता है?
  12. random module क्या है?
  13. random.randint() का उपयोग बताइए।
  14. random.choice() क्या करता है?
  15. datetime module का उपयोग क्या है?
  16. os module का उपयोग क्या है?
  17. Python Package क्या है?
  18. Module और Package में अंतर लिखिए।
  19. __name__ और __main__ का उपयोग क्या है?
  20. User-defined module का example लिखिए।

Frequently Asked Questions (FAQs)

Python Module क्या है?

Python module एक Python file होती है जिसमें reusable Python code जैसे functions, classes और variables रखे जा सकते हैं।

Python में module import कैसे करते हैं?

Module import करने के लिए import statement का उपयोग किया जा सकता है, जैसे import math

from और import में क्या अंतर है?

import से module import किया जा सकता है, जबकि from ... import से module के specific functions या objects import किए जा सकते हैं।

as keyword क्या करता है?

as keyword module या imported object को दूसरा नाम यानी alias देने के लिए उपयोग किया जाता है।

math module का उपयोग क्या है?

math module mathematical functions और constants जैसे sqrt(), factorial() और pi आदि उपलब्ध कराता है।

random module क्या है?

random module pseudo-random values generate करने और random selection जैसे कामों के लिए उपयोग किया जाता है।

Module और Package में क्या अंतर है?

Module सामान्यतः एक Python file होती है, जबकि package related modules को organized structure में रखने का तरीका है।

अब अगला Topic पढ़ें

RBSE Python Study Tip: Modules topic में सबसे पहले import, from और as statements को समझें। इसके बाद math, random और datetime modules के common functions की practice करें। Exam में Module और Package का अंतर तथा import statements पर विशेष ध्यान दें।
Official Reference:

Python Modules की विस्तृत जानकारी के लिए Python की official documentation देखें।

Python Modules Documentation