Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🔁 Unit 2 – Flow of Control और Functions  ·  Lesson 19

Scope और Modules (math, random)

Scope क्या है?

Variable का scope program का वह हिस्सा है जहाँ वह variable use हो सकता है। Function के अंदर बना variable सिर्फ़ उसी function में रहता है — जैसे class की announcement सिर्फ़ उसी classroom में सुनाई देती है, जबकि पूरे school की announcement (global) हर जगह।

Global scopeसभी functions के बाहर बने variables – हर जगह दिखते हैंFunction का local scopefunction के अंदर बने variables – सिर्फ़ अंदर दिखते हैं
Scope के दो levels

Local और global variables

Python
school = 'KV No. 1'           # global variable

def show():
    percent = 88              # local variable
    print('Inside:', school, percent)

show()
print('Outside:', school)
print(percent)                # percent does not exist here
Inside: KV No. 1 88 Outside: KV No. 1 NameError: name 'percent' is not defined

percent show() के अंदर बना था, इसलिए function ख़त्म होते ही मिट गया। Function के parameters भी local variables होते हैं।

अंदर और बाहर एक ही नाम

Function किसी नाम को assign करे तो Python नया local variable बना देता है, चाहे उसी नाम का global हो। Global वैसा ही रहता है।

Python
x = 5

def change():
    x = 50                    # creates a NEW local x
    print('Local x =', x)

change()
print('Global x =', x)
Local x = 50 Global x = 5

global keyword

Function के अंदर global variable बदलने के लिए उसे global से declare करें।

Python
count = 0

def visit():
    global count              # use the global variable
    count += 1

visit(); visit(); visit()
print('Visitors =', count)
Visitors = 3
⚠️ कम use करें

बहुत सारे global variables से programs debug करना मुश्किल हो जाता है। Values arguments के रूप में भेजें और results return करें।

Python standard library

Python के साथ सैकड़ों ready-made modules आते हैं जिन्हें standard library कहते हैं। Module बस useful functions वाली एक .py file है। एक folder में रखे related modules package बनाते हैं।

📐
mathsqrt, pow, ceil, floor, factorial, pi …
🎲
randomgames, quizzes, OTPs के लिए random numbers
📊
statisticsmean, median, mode
📅
datetimedates और times
CBSE syllabus और उससे आगे के modules

Module import करने के तरीके

Python
from math import sqrt, pi        # import only what you need
print(sqrt(81), round(pi, 2))
import math as m                 # short alias
print(m.floor(9.99))
9.0 3.14 9
Statementकैसे call करें
import mathmath.sqrt(9)
import math as mm.sqrt(9)
from math import sqrtsqrt(9)
from math import *sqrt(9) (सब import – बड़े programs में बचें)

math module

Python
import math
print(math.sqrt(144), math.pow(2, 5))
print(math.ceil(4.2), math.floor(4.8), math.fabs(-7.5))
print(math.factorial(5), math.gcd(36, 60))
print(round(math.pi, 4), round(math.e, 4))
print(round(math.sin(math.radians(30)), 2), math.log10(1000))
12.0 32.0 5 4 7.5 120 12 3.1416 2.7183 0.5 3.0
FunctionमतलबExample
sqrt(x)Square root (float)sqrt(49) → 7.0
pow(x, y)x की घात y (float)pow(3, 2) → 9.0
ceil(x)x से बड़ा या बराबर सबसे छोटा integerceil(4.1) → 5
floor(x)x से छोटा या बराबर सबसे बड़ा integerfloor(-4.1) → -5
fabs(x)Absolute value (float)fabs(-3) → 3.0
factorial(n)n!factorial(4) → 24
gcd(a, b)महत्तम समापवर्तक (HCF)gcd(12, 18) → 6
sin, cos, tanTrigonometry (radians)sin(radians(90)) → 1.0

random module

Random values हर बार बदलती हैं, इसलिए आपका output नीचे के sample से अलग होगा।

Python
import random
print(random.random())          # float 0.0 <= x < 1.0
print(random.randint(1, 6))     # dice: 1 to 6 (both included)
print(random.randrange(10, 50, 5))  # 10, 15, ... 45
print(random.choice(['Red', 'Green', 'Blue']))
0.6394267984578837 4 35 Green
Python
import random
# 4-digit OTP
otp = random.randint(1000, 9999)
print('Your OTP is', otp)
Your OTP is 5832
🧮 Exam का पसंदीदा

"random.randint(2, 4) * 10 के possible outputs क्या हैं?" → 20, 30 या 40। random.randrange(2, 4) के लिए सिर्फ़ 2 या 3 (4 शामिल नहीं)।

statistics module

Python
import statistics
marks = [85, 92, 78, 92, 66, 70]
print('Mean  :', statistics.mean(marks))
print('Median:', statistics.median(marks))
print('Mode  :', statistics.mode(marks))
Mean : 80.5 Median: 81.5 Mode : 92

Mean = जोड़ ÷ संख्या = 483 ÷ 6 = 80.5। Median = sorted list [66, 70, 78, 85, 92, 92] की बीच वाली value = (78 + 85) ÷ 2 = 81.5। Mode = सबसे ज़्यादा बार आने वाली value = 92।

हर mark की frequency – 92 सबसे ज़्यादा (mode)0123667078859211112
Mode सबसे ऊँचा bar है

अपना module बनाना

नीचे के functions को shapes.py नाम से save करें। उसी folder की दूसरी file में import shapes लिखकर shapes.circle_area(7) call करें।

Python
# file: shapes.py  (your own module)
def circle_area(r):
    return 3.14159 * r * r

def square_area(s):
    return s * s
Python
import shapes
print(round(shapes.circle_area(7), 2))
print(shapes.square_area(9))
153.94 81

सारांश और practice

  • Local variables function के अंदर रहते हैं; global बाहर।
  • Function के अंदर global बदलने के लिए global name।
  • Import के तरीके: import m, import m as x, from m import f।
  • math, random (random, randint, randrange, choice), statistics (mean, median, mode)।
✅ Practice

1) random.randrange(1, 10, 3) क्या return कर सकता है? 2) Dice game: दो dice फेंकें और दोनों बराबर हों तो "Double!" print करें। 3) क्या print होगा? a = 10 → def f(): a = 20 → f(); print(a)

Answers: 1) 1, 4 या 7। 3) 10।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Local और global variable में क्या फ़र्क है?
Local variable function के अंदर बनता है और सिर्फ़ उसी के अंदर use होता है; function ख़त्म होते ही मिट जाता है। Global variable सभी functions के बाहर बनता है और program में कहीं भी पढ़ा जा सकता है।
random.random(), randint() और randrange() में क्या फ़र्क है?
random() 0.0 से 1.0 (1.0 शामिल नहीं) तक float देता है। randint(a, b) a से b तक integer देता है, दोनों शामिल। randrange(start, stop, step) range की तरह काम करता है और stop शामिल नहीं करता।
Python में module क्या है?
Module एक .py file है जिसमें functions, variables और classes होते हैं जिन्हें दूसरे programs में import करके reuse किया जा सकता है, जैसे math या random।
← Class 11 Computer Science
🔗

Share this topic with a friend

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

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

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

अगला topicStrings: Slicing और Methods आपकी प्रगति: 19 / 29 (66%) · Lesson 20 / 29 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें