Python | Variables, Their Rules & Types By. Shivansh Sir

PYTHON PROGRAMMING

PYTHON | VARIABLES

Variable जब create किया जाता है तब interpreter द्वारा value को store करने के लिए memory location reserved की जाती है | Variable पर कोई भी data type की value store की जा सकती है | जैसे कि, Number, string, list, tuple, dictionary, etc

PYTHON | VARIABLES | ASSIGNING VALUE TO VARIABLES

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

Example

a = 5      #Number

b = "Hello" #string

c = [2, 5, 9] #list

Source Code

print(a, b, c)

Output

5 Hello [2, 5, 9]

PYTHON | VARIABLES | 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]

PYTHON | VARIABLES | 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

PYTHON | VARIABLES | 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]

PYTHON | VARIABLES | VARIABLE 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

PYTHON | VARIABLES | TYPES OF VARIABLES

Python में variable के दो प्रकार है |
1. 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

2. 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
Note :-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

Comments

Popular posts from this blog

Python | All in One Program By. Shivansh Sir

Python | Data Types | Number & it's Conversion By. Shivansh Sir

Python | Loop | For Loop By. Shivansh Sir