Global Statement in Python in Hindi

Arpit Nageshwar
⏰ 5 min read

Global Statement in Python in Hindi

Table of Contents

Introduction to Global Variables in Hindi – Global Variable Kya Hai?

  • चलिए आज एक ऐसा topic समझते हैं जो शुरुआत में थोड़ा confusing लगता है, लेकिन एक बार clear हो जाए तो बहुत आसान है – Global Variables और Global Statement।

  • जब भी आप Python में कोई function बनाते हैं, तो उस function के अंदर बनाई गई हर variable सिर्फ उसी function तक सीमित रहती है, यानी उसे बाहर से access नहीं किया जा सकता। इसे हम Local Variable कहते हैं।

  • लेकिन इसके उलट, जो variable किसी function के बाहर, यानी main program के level पर बनाई जाती है, उसे पूरे program में कहीं से भी access किया जा सकता है, इसी को हम Global Variable कहते हैं।

  • आप इसे अपने घर की तरह सोच सकते हैं – घर के हर कमरे (function) का अपना सामान (local variable) होता है, जो सिर्फ उसी कमरे में रहता है, लेकिन घर का main gate या backyard (global scope) ऐसी जगह है जहां से हर कमरे का व्यक्ति आ-जा सकता है।

  • Global Variables का सबसे बड़ा फायदा यह है कि इन्हें एक बार define करने के बाद, आप उन्हें program के किसी भी function के अंदर इस्तेमाल कर सकते हैं, बिना उन्हें बार-बार pass किए।

  • हालांकि, अगर आप किसी function के अंदर उसी Global Variable की value को बदलना चाहें, तो सिर्फ उसे access करना काफी नहीं होता, इसके लिए Python में एक खास keyword इस्तेमाल किया जाता है – global

  • Exam की नज़र से देखा जाए तो, Global Variable का मतलब है – "wo variable jo kisi function ke bahar define ki jaati hai, aur program ke kisi bhi hisse se access ki ja sakti hai."

Global aur Local Scope Global Scope (Poore Program mein Accessible) x = 10 Function A y = 5 (Local Variable) Function B z = 8 (Local Variable)

यह diagram दिखाता है कि Global Variable x दोनों functions (Function A और Function B) से access किया जा सकता है, लेकिन Function A का Local Variable y और Function B का Local Variable z, सिर्फ अपने-अपने function तक ही सीमित रहते हैं।

Global Statement in Python in Hindi – global Keyword Kaise Use Karein?

  • अब यहां एक बहुत जरूरी बात समझनी है – किसी Global Variable की value को सिर्फ पढ़ने (read करने) के लिए आपको किसी special keyword की जरूरत नहीं पड़ती, आप उसे function के अंदर सीधे इस्तेमाल कर सकते हैं।

x = 10

def show_value():
    print("x ki value hai:", x)   # Yaha sirf padha ja raha hai, koi problem nahi

show_value()
  • लेकिन अगर आप उसी function के अंदर उस Global Variable की value को बदलना (modify करना) चाहें, तो Python आपको एक नई Local Variable बना देगा, बजाय Global Variable को बदलने के, और इससे एक error भी आ सकती है।

x = 10

def change_value():
    x = x + 5   # Yeh error dega!
    print(x)

change_value()

ऊपर वाला code UnboundLocalError देगा, क्योंकि Python यह मान लेता है कि x function के अंदर एक नई Local Variable है (क्योंकि उसे assign किया जा रहा है), और उसे function के अंदर इस्तेमाल करने से पहले define नहीं किया गया।

इसी problem को solve करने के लिए Python में global keyword इस्तेमाल किया जाता है, यह Python को साफ-साफ बता देता है कि "मैं यहां जिस variable की बात कर रहा हूं, वो function के अंदर की नई variable नहीं, बल्कि बाहर वाली Global Variable ही है।"

x = 10

def change_value():
    global x
    x = x + 5
    print("Function ke andar x hai:", x)

change_value()
print("Function ke bahar x hai:", x)
Function ke andar x hai: 15
Function ke bahar x hai: 15

देखिए, इस बार function के अंदर की गई value की बदलाव, function के बाहर भी दिखाई दे रहा है, यही global keyword की असली ताकत है।

Local vs Global Variables in Hindi

  • चलिए अब इन दोनों को थोड़ा और गहराई से समझते हैं, क्योंकि exam में अक्सर इन दोनों के बीच फर्क पूछा जाता है।

  • Local Variable वह variable होती है जो किसी function के अंदर बनाई जाती है, और उसका जीवन (lifetime) सिर्फ उस function के execution तक ही सीमित रहता है, function खत्म होते ही वह variable भी खत्म हो जाती है।

  • Global Variable वह variable होती है जो function के बाहर, main program के level पर बनाई जाती है, और यह पूरे program के execution तक जिंदा रहती है, इसे कहीं से भी access किया जा सकता है।

x = 100   # Global Variable

def my_function():
    y = 50   # Local Variable, sirf is function ke andar hi accessible hai
    print("Function ke andar, x hai:", x)
    print("Function ke andar, y hai:", y)

my_function()
print("Function ke bahar, x hai:", x)

# print(y)   # Yeh line error degi, kyunki y sirf function ke andar hi accessible hai

अगर आप ऊपर वाले code में last line का comment हटाकर चलाएंगे, तो आपको NameError मिलेगी, क्योंकि y function के बाहर मौजूद ही नहीं है, यही Local aur Global Variables के बीच सबसे बुनियादी फर्क है।

Examples of Global Statement in Hindi

Example 1: Ek Counter Banana Jo Har Function Call Par Badhta Rahe

counter = 0

def increment_counter():
    global counter
    counter = counter + 1
    print("Counter ki value hai:", counter)

increment_counter()
increment_counter()
increment_counter()
Counter ki value hai: 1
Counter ki value hai: 2
Counter ki value hai: 3

Example 2: Bank Balance Update Karna

balance = 1000

def deposit(amount):
    global balance
    balance = balance + amount
    print("Deposit ke baad balance hai:", balance)

def withdraw(amount):
    global balance
    balance = balance - amount
    print("Withdraw ke baad balance hai:", balance)

deposit(500)
withdraw(300)
Deposit ke baad balance hai: 1500
Withdraw ke baad balance hai: 1200

Example 3: Global Variable ko Sirf Padhna (global Keyword ke Bina)

college_name = "ABC Institute"

def display_info():
    print("College ka naam hai:", college_name)   # Sirf padhna hai, global keyword ki zaroorat nahi

display_info()

ध्यान दीजिए, इस तीसरे example में हमें global keyword की जरूरत नहीं पड़ी, क्योंकि हम सिर्फ value पढ़ रहे हैं, उसे modify नहीं कर रहे। global keyword सिर्फ तभी जरूरी होता है जब आप function के अंदर से किसी Global Variable की value को बदलना चाहते हैं।

Difference Between Local and Global Variables (Table) in Python in Hindi

Point Local Variable Global Variable
Definition Kahan Hoti HaiKisi function ke andarFunction ke bahar, main program mein
Scope (Accessibility)Sirf usi function tak seemitPoore program mein kahin se bhi accessible
LifetimeSirf function ke execution takPoore program ke execution tak
Modify Karne Ke LiyeDirectly modify kiya ja sakta haiFunction ke andar modify karne ke liye global keyword chahiye
Memory Kab Free Hoti HaiFunction khatam hote hiProgram khatam hone par
Exampley = 5 (kisi function ke andar)x = 10 (function ke bahar)

Exam में याद रखने का सबसे आसान तरीका यह है – Local Variable अपने function के "कमरे" तक सीमित रहता है, जबकि Global Variable पूरे "घर" यानी पूरे program में कहीं से भी बुलाया जा सकता है, बस उसे modify करने के लिए global keyword की इजाज़त लेनी पड़ती है।

FAQs – Global Statement in Python in Hindi

वह variable जो किसी function के बाहर, main program के level पर define की जाती है, और पूरे program में कहीं से भी access की जा सकती है, उसे Global Variable कहते हैं।

global keyword ki zaroorat sirf tab padti hai jab aap kisi function ke andar se, us function ke bahar bani hui Global Variable ki value ko modify (badalna) chahte hain, sirf padhne ke liye iski zaroorat nahi hoti।

Aisa karne par Python UnboundLocalError de dega, kyunki Python us variable ko function ke andar ki nayi Local Variable maan leta hai, na ki bahar wali Global Variable।

Local Variable sirf apne function tak seemit rehta hai aur function khatam hote hi khatam ho jaata hai, jabki Global Variable poore program mein kahin se bhi accessible hota hai aur program khatam hone tak jinda rehta hai।

Haan, ho sakti hai, aise mein function ke andar Python hamesha Local Variable ko priority dega, aur function ke bahar wali Global Variable par koi asar nahi padega, jab tak global keyword use na kiya jaaye।

Arpit Nageshwar

✍️ Arpit Nageshwar

Post-graduated | Web Developer | +3 yr Experience | IIT Kharagpur Certified