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