This Website for Those Students who are searching for a platform to study online in their own Language Hindi, on this website they will learn all courses like IAS, programming, web designing, data science, and competitive exam preparation, etc.


Python Variables

Python Variables

Variables



एक variable computer की memory में किसी location का नाम होता है। यह नाम उस location पर value store करने और वँहा से value प्राप्त करने के लिए use किया जाता है।

किसी भी variable में value मुख्यतः इसलिए store की जाती है ताकि उसके साथ कोई operations perform किया जा सके या उसे process किया जा सके।

Variable जब create किया जाता है तब interpreter द्वारा value को store करने के लिए memory location आरक्षित की जाती है |

Variable पर कोई भी data type की value store की जा सकती है | जैसे कि, Number, string, list, tuple, dictionary

Python में variables की values को print करने के लिए print() function use किया जाता है।

किसी text को variable के साथ combine करके print करने के लिए python में + character को use किया जाता है। इसके अलावा आप इस character से एक variable को दूसरे variable से भी जोड़ सकते है।

Python में हर variable एक object है। असल में python में आप किसी variable को एक value नहीं assign करते है बल्कि आप एक object का reference assign करते है जिसमे वह value store की गयी है।

इसका मतलब यह हुआ की python में जब आप कोई variable की value access करने का प्रयास करते है तो असल में आप उस object को point करते जिसमे वह value store की गयी है।

उदाहरण के लिए निचे दिए गए code को देखिये।

 
  
  
num = 50
  



ऊपर दिए गए code में 50 के integer object है और num एक नाम है जो उस object को point करता है।

Python में आप एक string और number को directly concatenate नहीं कर सकते है। पहले आप दोनों को separate variables के रूप में declare करते है इसके बाद उन variables को आपस में concatenate किया जा सकता है।



Variable Rules in Python

 

Python में कुछ variable सम्बधित rules होते है जिन्हें follow करना आवश्यक होता है।



  • एक variable का नाम या तो किसी letter या underscore (_) character से शुरू होना चाहिए। 


  • एक variable के नाम में सिर्फ alphanumeric characters (a-z, 0-9 और _) ही हो सकते है। 


  • Python के reserved words को आप variable names के रूप में नहीं use कर सकते है।


  • एक बात आपको हमेशा ध्यान रखनी चाहिए की python एक case sensitive language है। यानी की python में sum, Sum और SUM तीन अलग अलग variables माने जायेंगे। 


Declaring Variables in Python



Variables को एक single alphabet से लेकर किसी भी नाम से declare किया जा सकता है।

Python में variables को use करने के लिए पहले से define करने की आवश्यकता नहीं होती है जैसा की कई programming languages में होता है। Python में जैसे ही आप variable को कोई value assign करते है वह declare हो जाता है। यही कारण है की python को dynamically typed language कहा जाता है।

Python में variables को declare करने के लिए data type define करने की भी आवश्यकता नहीं होती है। Python किसी भी value का data type automatically judge कर लेती है। किसी भी variable को value assign करने के लिए assignment operator (=) use किया जाता है।

Python में variable create करने का general syntax निचे दिया जा रहा है।



 

    variable-name = value
   



Python interpreter variable की value के आधार पर ही उसे memory assign करता है। Python में variable create करना निचे उदाहरण द्वारा समझाया जा रहा है।

num = 100

Python में variables को एक बार declare करने के बाद दुबारा किसी दूसरे type की value से redeclare किया जा सकता है। उदाहरण के लिए ऊपर declare किये गए num variable को दुबारा एक string value से इस प्रकार redeclare किया जा सकता है।

 

   num = "Hundred
   



एक single variable declare करने के अलावा आप एक साथ कई variables भी declare कर सकते है और उन्हें एक ही value assign कर सकते है।

उदाहरण के लिए निचे  दिए गए code को देखिये।

 

   num1=num2=num3=10
   



ऊपर दिए गए code में 3 variables create किये गए है और उन सभी को 10 value assign की गयी है। 

Assigning Value to Variable



Python में declaration की जरुरत नहीं होती है | जब variable पर value assign होती है तब automatically declaration होता है |

declaration न होने के कारण Python में variable की default value नहीं होती है |

For Example,

 

  a = 5 #Number 

  b = "Hello" #string 

  c = [2, 5, 9] #list 

  print(a, b, c) 


Output :
 

  5 

  Hello 

  [2, 5, 9] 
  

Changing Variable's Value



Python में variable की value change या re-assign की जा सकती है

Source Code :

 
  a = 5
 
  print(a)
  
  a = "Hello" 
 
  print(a) 
 
  a = [4, 5, 8] 
 
  print(a)
   

Output :
 

  5 

  Hello 

  [4, 5, 8]
  



Deleting Variables in Python



Python आपको किसी variable को delete करने की facility भी provide करती है। इसके लिए del command का प्रयोग किया जाता है। Python में variable delete करने का general syntax निचे दिया जा रहा है।



 

  del variable-name
  



उदाहरण के लिए मान लीजिये आप num variable को delete करना चाहते है तो इसके लिए आप इस प्रकार statement लिखते है।
 

  del num
  



Swapping Variable Values in Python 



Python आपको single statement द्वारा variables की values आपस में swap करने की ability provide करती है। इसके लिए आप निचे दिया गया syntax follow करते है। 

 

  var1, var2 = var2, var1
  



Python में variables की values swap करना निचे उदाहरण द्वारा समझाया जा रहा है। 

 

  num1 = 5
 
  num2 = 7

 

  num1, num2 = num2, num1

  print(num1)

  print(num2)
  
  

Assigning Single Value to Multiple Variables



Python में एक ही value एक से ज्यादा variables पर assign की जा सकती है

Source Code :

 

  a = b = c = d = "Hello"

   print(a)

   print(b)

   print(c) 

   print(d)
    



Output :

 

Hello Hello Hello Hello




Assigning Value to Variable according to order



Python में क्रमनुसार variable पर value store की जाती है |

Example पर एक ही memory location multiple variables और उनकी values assign की गयी है |

Source Code :

 

  a, b, c = 1, 'H', [1, 2]

  print(a)

  print(b) 

  print(c) 




Output :

 

  1

  H 

  [1, 2]
  



Variables Concatenation



Python में एक ही data types के variables concatenate किय जा सकते है |

Example पर str() function का इस्तेमाल object को integer से string में convert करने के लिए किया गया है |



Source Code :

 

  a = 1 
  b = 2 
  print(a + b)

  print(str(a) + str(b))
 
  c = "Hello" print(str(a) + c) 




Output :

 

  3 

 
  12 1Hello
  
  



Types of Variables



Python मे variable के दो प्रकार है |

  1. Local Variables


  2. Global Variables


Local Variables



Local Variables; functions के अन्दर होते है | उनकी visibility सिर्फ function के अन्दर होती है, जब वो function के बाहर आते है तब destroy हो जाते है |



Source Code :

 

def func():

  a = 5 #local variable print(a)  func()
 
   print(a)
   



Output :

 

 5 Traceback (most recent call  last):
 
  print(a)
 
  NameError: name 'a' is not  defined
  



Global Variables



Global Variables; function के बाहर होते है | उनकी visibility function के अन्दर और बाहर होती है | उनका scope पूरे program पर होता है |



Source Code :

 

 a=10 #global variable

  def func(): print(a)
   func()
  
    print(a)
    
    

Output :
 

 10
  
 10 




Example पर local और global ये दोनों variables declared किये गए है | function के बाहर का variable lobal है और अन्दर का variable local है | global variable का scope function के अन्दर और बाहर होता है लेकिन function के अन्दर अलग से variable declaration होने के कारण func() call करते ही variable की value change हो जाती है |

Source Code :

 

  a = 10 #global variable

   def func(): a = 5 #local
   variable
   print(a) func() #print local 
  
   print(a) #print global 
   
   



Output:

 

 5

 10




With 'global' and Without 'global' Keyword



function में variable के लिए 'global' keyword का भी इस्तेमाल किया जाता है |

Without global



Example पर 'a' variable के declaration से पहले ही 'a' variable को print किया गया है, इसके कारण 'UnboundLocalError' ये exception occur हुआ है |



Source Code :

 

  def func():

   print(a) a="local" 
 
   print(a) a="global"
 
   func() print (a)




Output :

 

  in func print(a)   UnboundLocalError: local variable 'a' referenced before assignment 




With global



Example पर global keyword का इस्तेमाल शुरुआत में ही किया गया है और उसके बाद variable a को print किया गया है | program में जहा पर भी 'a' नामक global variable होगा वो func() call करते ही पहले print हो जायेगा |



Source Code :

 

  def func():
   global a
 
    print(a) #print global
  
    a="local" print(a)#print local
   
    a="global"
      
      func() print(a) #print  
      local
       




Output :

 

global local local




No-Local or No-Global Variable



'nonlocal' variable का इस्तेमाल nested funtion के लिए किया जाता है | 'nonlocal' variables ये global भी नहीं होते और global भी नहीं होते है | अगर inner function में उनको लिया जाता है तो outer functions में उनकी values change नहीं होती है |



Without nonlocal



Source Code :

 

var = 2
  def outer():
   var = 5
  
    def inner():
     var = 10 
     
     print("inner : ", var)    
     inner()
    
      print("outer : ", var)
      outer()
     
      print("global : ", var) 





Output :

 

 inner :10 
 outer :5 
 global:2 
 
 



With Non-local



Source Code :

 

 var = 2
 
  def outer(): 
 
  var = 5 def inner():
 
  nonlocal var
  
   var = 10
    print("inner : ", var)   
    
    inner() 
   
    print("outer : ", var) 
    
    outer() 
    
    print("global : ", var) 





Output :

 


inner : 10 

outer : 10

global: 2




हेलो दोस्तो आशा करता हु की ये पोस्ट आपको पसंद आया होगा , अगर आपको इसमे समझने में या कुछ दिकत आती है तो आप मुजे comment box में जाके comment कर सकते हो अगर आपको ये पसंद आया तो शेयर जरूर कीजिये धन्यवाद।

Previous
Next Post »

Quote

"Educationists should build the capacities of the spirit of inquiry, creativity, entrepreneurial and moral leadership among students and become their role model."

Dr. Abdual Kalam