Python Variables

Python Variables

Python supports two types of variables –

पायथन दो प्रकार के वैरियेबल का समर्थन करता है

1. Local Variables – लोकल वेरिएबल
2. Global Variables – ग्लोबल वेरिएबल

Local Variables – लोकल वेरिएबल  

Local variables which are declared inside a function’s body or in the local scope are called local variables. Local variables are available only for the function in which we declared it i.e., from outside of the function we cannot access.

लोकल वैरियेबल जो किसी फंक्शन के बॉडी के भीतर या स्थानीय दायरे में घोषित किए जाते हैं, लोकल वैरियेबल कहलाते हैं। लोकल वैरियेबल केवल उस फंक्शन के लिए उपलब्ध होते हैं, जिसमें हमने इसे घोषित किया था, अर्थात् फंक्शन के बाहर से हम पहुंच नहीं सकते हैं।
Example 1:

def fun () :
    a = "local"
    print(a)
fun ()

Output:
local

Example 2: Local variables are available only for the function, from outside of the function we cannot access.
लोकल वैरियेबल केवल फंक्शन के लिए उपलब्ध हैं, फंक्शन के बाहर से हम पहुंच नहीं सकते हैं।

def fun () :
    a = "local" 
print(a) 

Output:
NameError: name ‘a’ is not defined

The output shows an error, because we are trying to access a local variable ‘a’ in a global scope whereas the local variable only works inside fun().

आउटपुट में एरर दिखाई देता है, क्योंकि हम लोकल वैरियेबल ‘a’ को ग्लोबल दायरे में एक्सेस करने की कोशिश कर रहे हैं, जबकि लोकल वैरियेबल केवल fun) के लिए काम करता है।

Global Variables – ग्लोबल वैरियेबल

In Python, variables declared outside of the function or in global scope are known as global variable. This means, global variables can be accessed inside or outside of the functions of that module.

पायथन में, फंक्शन के बाहर या ग्लोबल दायरे में घोषित वैरियेबल को ग्लोबल वैरियेबल के रूप में जाना जाता है। इसका मतलब है, ग्लोबल वैरियेबल उस मॉड्यूल के कार्यों के अंदर या बाहर पहुँचा जा सकता है।
Example 1:

a = "global " 
def fun1 () :
    print (a)
def fun2():
    print (a)
    print ("calling fun1 () and fun2 () ") 
fun1 () 
fun2 () 
print ("----------------------------")

Output:
calling fun1 () and fun2 ()
global
global
—————–
In above code, we created ‘a’ as a global variable and defined two functions fun1() and fun2() to print the global variable ‘a’. Finally, we call the fun1() and fun2() which will print the value of ‘a’.

उपरोक्त कोड में, हमनें ‘a’ को एक ग्लोबल वैरियेबल के रूप में बनाया और ग्लोबल वैरियेबल ‘a’ को प्रिंट करने के लिए fun1() और fun2) को दो फंक्शन परिभाषित किया। अंत में, हम fun1() और fun2() कहते हैं, जो ‘a’ के वैल्यू को प्रिंट करेगा।

Example2: If you want to change value of ‘a’ inside a function.
यदि आप किसी फंक्शन के अंदर ‘a’ का मान बदलना चाहते हैं।

a = 10 
def fun () :
    a = a * 2
print (a) 
fun ()

Error message: UnboundLocalError: local variable ‘a’ referenced before assignment.

The output shows an error because Python treats ‘a’ as a local variable and ‘a’ is also not defined inside fun().

आउटपुट में एरर दिखाई देती है क्योंकि पायथन ‘a’ को लोकल वैरियेबल के रूप में मानता है, और ‘a’ को fun() के अंदर भी परिभाषित नहीं किया जाता है।

In Python, to make this work we use global keyword.

पायथन में, इस काम को करने के लिए हम ग्लोबल कीवर्ड का उपयोग करते हैं।

‘global’ keyword: ‘global’ keyword allows a user to modify a variable outside of the current scope. It is used to create global variables from a non-global scope i.e., inside a function.

‘global’ कीवर्ड: ‘global’ कीवर्ड यूजर को मौजूदा दायरे से बाहर एक वैरियेबल को संशोधित करने की अनुमति देता है। इसका उपयोग गैर-वैश्विक क्षेत्र से ग्लोबल वैरियेबल बनाने के लिए किया जाता है, अर्थात् एक फंक्शन के अंदर ।

‘globaľ keyword is used inside a function only when we want to do assignments or when we want to change a variable. Global is not needed for printing and accessing.

‘global’ कीवर्ड का उपयोग किसी फंक्शन के अंदर. तभी किया जाता है जब हम असाइनमेंट करना चाहते हैं या जब हम एक परिवर्तनशील परिवर्तन करना चाहते हैं। प्रिंटिंग और एक्सेस के लिए ग्लोबल की जरूरत नहीं है।

Rules:

1. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.

यदि किसी वैरियेबल को फंक्शन के बॉडी के भीतर कहीं भी वैल्यू दिया जाता है, तो इसे स्थानीय माना जाता है जब तक कि स्पष्ट रूप से ग्लोबल घोषित नहीं किया जाता है।

2. Variables that are only referenced inside a function are implicitly global.

वैरियेबल जो केवल एक फंक्शन के अंदर संदर्भित होते हैं, वे अंतर्निहित रूप से ग्लोबल हैं।

3. We Use ‘global’ keyword to use a global variable inside a function.

हम एक फंक्शन के अंदर एक वैश्विक वैरियेबल का उपयोग करने के लिए ‘global’ कीवर्ड का उपयोग करते

4. There is no need to use global keyword outside a function.

किसी फंक्शन के बाहरं ग्लोबल कीवर्ड का उपयोग करने की आवश्यकता नहीं है।

Note: To access a global variable inside a function there is no need to use global keyword.

किसी फंक्शन के अंदर एक ग्लोबल वैरियेबल का उपयोग करने के लिए ग्लोबल कीवर्ड का उपयोग करने की कोई आवश्यकता नहीं है।

Example 1: Python program to modify’ a global value inside a function.
एक फंक्शन के अंदर एक ग्लोबल वैल्यू को संशोधित करने के लिए पायथन प्रोग्राम।

c = 100 
print(" Before calling modify function 'c' value is : ",c) 
def modify () :
    global c 
    c = 1000
print(" Inside function 'c' value is :", c)
print(" End modify() ")
#calling function modify ()
modify () 
print(" Outside function 'c' value is :")

Output:
Before calling modify function ‘c’ value is :  100
 Inside function ‘c’ value is : 100
 End modify()
 Outside function ‘c’ value is :

Chapter wise Model Paper Link

About Me