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 เคเคฐเคจा เคจिเคे เคเคฆाเคนเคฐเคฃ เคฆ्เคตाเคฐा เคธเคฎเคाเคฏा เคा เคฐเคนा เคนै।
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 เคे เคฆो เคช्เคฐเคाเคฐ เคนै |
- Local Variables
- 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 เคเคฐ เคธเคเคคे เคนो เค เคเคฐ เคเคชเคो เคฏे เคชเคธंเคฆ เคเคฏा เคคो เคถेเคฏเคฐ เคเคฐूเคฐ เคीเคिเคฏे เคงเคจ्เคฏเคตाเคฆ।
