Scope और Modules (math, random)
Scope क्या है?
Variable का scope program का वह हिस्सा है जहाँ वह variable use हो सकता है। Function के अंदर बना variable सिर्फ़ उसी function में रहता है — जैसे class की announcement सिर्फ़ उसी classroom में सुनाई देती है, जबकि पूरे school की announcement (global) हर जगह।
Local और global variables
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 herepercent show() के अंदर बना था, इसलिए function ख़त्म होते ही मिट गया। Function के parameters भी local variables होते हैं।
अंदर और बाहर एक ही नाम
Function किसी नाम को assign करे तो Python नया local variable बना देता है, चाहे उसी नाम का global हो। Global वैसा ही रहता है।
x = 5
def change():
x = 50 # creates a NEW local x
print('Local x =', x)
change()
print('Global x =', x)global keyword
Function के अंदर global variable बदलने के लिए उसे global से declare करें।
count = 0
def visit():
global count # use the global variable
count += 1
visit(); visit(); visit()
print('Visitors =', count)बहुत सारे 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 बनाते हैं।
Module import करने के तरीके
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))
| Statement | कैसे call करें |
|---|---|
import math | math.sqrt(9) |
import math as m | m.sqrt(9) |
from math import sqrt | sqrt(9) |
from math import * | sqrt(9) (सब import – बड़े programs में बचें) |
math module
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))
| 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 से बड़ा या बराबर सबसे छोटा integer | ceil(4.1) → 5 |
| floor(x) | x से छोटा या बराबर सबसे बड़ा integer | floor(-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, tan | Trigonometry (radians) | sin(radians(90)) → 1.0 |
random module
Random values हर बार बदलती हैं, इसलिए आपका output नीचे के sample से अलग होगा।
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']))
import random
# 4-digit OTP
otp = random.randint(1000, 9999)
print('Your OTP is', otp)"random.randint(2, 4) * 10 के possible outputs क्या हैं?" → 20, 30 या 40। random.randrange(2, 4) के लिए सिर्फ़ 2 या 3 (4 शामिल नहीं)।
statistics module
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 = जोड़ ÷ संख्या = 483 ÷ 6 = 80.5। Median = sorted list [66, 70, 78, 85, 92, 92] की बीच वाली value = (78 + 85) ÷ 2 = 81.5। Mode = सबसे ज़्यादा बार आने वाली value = 92।
अपना module बनाना
नीचे के functions को shapes.py नाम से save करें। उसी folder की दूसरी file में import shapes लिखकर shapes.circle_area(7) call करें।
# file: shapes.py (your own module)
def circle_area(r):
return 3.14159 * r * r
def square_area(s):
return s * simport shapes print(round(shapes.circle_area(7), 2)) print(shapes.square_area(9))
सारांश और 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)।
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।
