What is Variables Rules In Python

Variable jab create kiya jata hai tab interpreter dauvara value ko store karne ke liye memory location assign ki jati hai .

Variables In Python

Variable par koi bhi data type ki value store ki ja sakti hai | Example : Number , String, List, Tuple, Dictionary.

Assigning Value To Variable

Python me declaration ki jarurat nhi hai | Jab Variable par value assign hoti hai tab automatically declaration hota hai.

Declaration Na hone ke karan python me variable ki default value nhi hoti hai|

For Example

a = 6   # Number
b = "Hello World"  # sting
c = [3,2,4]  # list
print(a,b,c)

Output :

6 Hello World [3,2,4]

Changing Variable’s Value

Python me variable ki value change ya re-assign ki ja sakti hai |

Example :

a = 4
print(a)
a = "Test"
print(a)
a = [3,5,6]
print(a)

Output :

4
Test
[3,5,6]

Assigning Single Value to Multiple Variables

Python me ek hi value ek se jyada variables par assign ki ja sakti hai |

For Example :

a = b = c = d= "Hello World"
print(a)
print(b)
print(c)
print(d)

Output :

Hello World
Hello World
Hello World
Hello World

Variables Concatenation

python me ek hi data types ke variables concetenate kiye ja sakte hai |

See Example :

Example par str() function ka istemal object ko integer se string me convert karne ke liye kiya gya hai |

Source Code :

a = 2
b = 1
print(a+b)
print(str(a) + str(b))
c = "Hello World"
print(str(a) + c)

Output :

3
21
2Hello World

Types of Variables In Python

python me variable ke two type hote hai|

  • 1 . Local Variables
  • 2 . Global Variables

1 . Local Variables

Local Variables, functions ke under hote hai| unki visibility sirf function ke under hoti hai, jab wo function ke bahar aate hai tab destroy ho jate hai|

For Example :

def func():
    a = 7 # local variable
    print(a)

func()
print(a)

Output :

7
Traceback ( most recent Call Last):
  print(a)
NameEroor: Name 'a' is not defined

Global Variables

Global Variables , function ke bahar hote hai | unki visibility function ke under or bahar hoti hai| unka scope pure program par hota hai |

Source Code :

a = 10  # global variable
def func():
    print(a)

func()
print(a)

Output:

10
10

Example local or global ye dono variables declared kiye gye hai | function ke bahar ka variable local hai | or under ka variable local hai | global variable ka scope function ke under or bahar hota hai lekin function ke under alag se variable declaration hone ke karan func() call karte hi variable ki value change ho jati hai|

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

func() # print local
print(a) # print global

Output:

4
12
Please follow and like us:
error
Summary
Review Date
Reviewed Item
What is Variables Rules In Python
Author Rating
51star1star1star1star1star

Danish Ali

I Am a 1st Sem BCA Student & I have Skills C, C++ ,Python, HTML,CSS, Computer & Advance Hacking & Blogger & YouTuber.

Leave a Reply

Your e-mail address will not be published. Required fields are marked *