Python me Variables creation

Simple language me kaha jaye to python me variables ek tarah ka storage hota hai jaha values ko store kiya jata hai.
Dusre languages jaise C, C++, Java, Dotnet, inn sabhi me variables ko pahle declare karna hota h lekin python me aisa koi concept nhi hai. Python! variable ki values ke hisab se apne aap uski properties define kar leta hai.

Example se samajhte hain:

a = 7
b = “Jenny”
print(a)
print(b)

Yaha “a” aur “b” 2 variables hain, jaha “a” ek int variable hai wahi dusri side “b” ek string variable hai. String variable ko single quotes ka use karke bhi define kiya jata hai.

Python me Variable Name

Python me variables ke naam bhut hi short ( jaise “a” or “b”) ya bhi fully descriptive( jaise profit, loss, product_name etc) hone chahiye. 

Python Variables se related kuch Rules:

  • Ek variable hamesha ek letter se ya fir (_) underscore se start hona chahiye.
  • Variables ko kabhi bhi number se shuru nhi karna chahiye jaise 2profit , 5loss etc.
  • Hamesha alpha-numeric characters (A-Z or a-z) ya fir (_) underscore ka use karna chahiye

* Python me variable naam Case Sensitive hote hain jaise TITLE, Title, title – ye tino terms alag-alag variables honge.

* Right Way
a = 7

_count = 5
var_num = 88
_my_num = “Fifty”
My_Num = 34

* Wrong Way
5a = 7
5_gems = “5 gems”

Multi-Variable Assignment

Python me hum ek sath multiple variables ki values ko define kar sakte hain, Jaise: 

a, b, c = 7, 8, 9
print (a)
print (b)
print (c)

 * Output will be
a = 7
b = 8
c = 9

Iske alawa hum ek sath 1 hi value ko multiple variables me assign kar sakte hain , jaise:
a = b = c = 10
Jiska matlab yah hai ki a, b aur c tino ki value 10 hogi.

Python Output Variable

Python me Output Variable ka concept “print” statement par lagu hota hai. Simple language me bole to “print” statement ek output variable hai.

Python Global Variable

Global Variable – jo kisi bhi function me globally accessible hota hai aur jise functions ke outside me create kiya jata hai.
Kuch Examples se samajhte hain:

x = “Focus”
def myfoo():
    print(“Just ” + x + ” in your Goal”)

myfoo()

O/P : Just Focus in your Goal

yaha ‘+’ concatenation ki tarah kam kar raha hai means variable ko sentence ke sath jod raha hai aur myfoo() ek function hai, jise hum define karne ke baad use execute kar rahen hain. 

Kisi bhi Global Variable ka Global aur Local Accessibility :

my_var = “impressed”

def myfoo():
    my_var = “a function”
    print(“I am ” + my_var)

myfoo()
print(“I am ” + my_var)  

Python me Global Keyword ka concept

def myfoo():
    global my_var
    my_var = “Amazing”
    print(“Myfoo() Function”)

myfoo()

print(“The World is ” + my_var)

Python me Global Keyword ka concept kuch khas hota hai. Samajhne ke liye hum upar diye gaye code ko dekhte hain. Jaha my_var ko ek “global” keyword ke sath define kiya gaya hai jo myfoo() function me available hai, lekin ek baat notice karne wali yah hai ki ise hum function ke outside se bhi access kar sakte hain. jaisa ki print(“The World is ” + my_var) bata raha hai.

Agar koi variable kisi code me global hai aur usse hum kisi bhi function ke andar se change karna chahte hain to hum use “Global’ keyword ke sath use karke change kar sakte hain. Niche diye gaye example se samajhiye…

my_var = “Shocked”

def myfoo():
    global my_var
    my_var = “impressed”

myfoo()

print(“I am ” + my_var)

O/P : I am impressed