Variables in Python
Python variables are like containers that store values in a program. When you create a variable, Python sets aside space in memory to hold the value. The type of value you store decides how much space is needed.
Python में variables ऐसे container होते हैं जो values को स्टोर करते हैं। जब आप कोई variable बनाते हैं, तो Python मेमोरी में उसके लिए जगह बना देता है। आप जो type की value उसमें डालते हैं, उसी के हिसाब से मेमोरी दी जाती है।
Example 1: Basic Variable Assignment
x = 5
y = 'Hello'
print(x)
print(y)
Output:
5 Hello
Assigns integer and string values to variables and prints them.
यह प्रोग्राम एक integer और एक string वैल्यू को variable में store करता है और print करता है।
Example 2: Multiple Assignment
a, b, c = 1, 2, 3
print(a, b, c)
Output:
1 2 3
Assigns multiple variables in one line.
यह प्रोग्राम एक ही लाइन में कई variables को value assign करता है।
Example 3: Type Casting
x = int(2.5)
y = float(5)
z = str(10)
print(x, y, z)
Output:
2 5.0 10
Casts values into integer, float, and string types.
यह प्रोग्राम values को integer, float और string में बदलता है।
Example 4: Case Sensitivity
a = 10
A = 20
print(a)
print(A)
Output:
10 20
Shows that Python variable names are case-sensitive.
यह दिखाता है कि Python में variable names case-sensitive होते हैं।
Example 5: Variable Naming Rules
my_var = 5
_var2 = 10
print(my_var + _var2)
Output:
15
Demonstrates valid variable naming rules.
यह प्रोग्राम सही variable naming rules को दिखाता है।
Example 6: Swapping Variables
x, y = 5, 10
x, y = y, x
print(x, y)
Output:
10 5
Swaps values of two variables in one line.
यह एक लाइन में दो variables की values को आपस में बदल देता है।
Example 7: Global and Local Variables
x = 'global'
def test():
x = 'local'
print(x)
test()
print(x)
Output:
local global
Shows difference between local and global variables.
यह local और global variables के बीच का अंतर दिखाता है।
Example 8: Constants Convention
PI = 3.1416
print(PI)
Output:
3.1416
Shows how constants are usually named in uppercase.
यह दिखाता है कि constants को सामान्यतः uppercase में लिखा जाता है।
Example 9: Assigning Same Value to Multiple Variables
a = b = c = 100
print(a, b, c)
Output:
100 100 100
Assigns the same value to multiple variables.
यह एक ही value को कई variables में assign करता है।
Example 10: Unpacking a List
fruits = ['apple', 'banana', 'cherry']
x, y, z = fruits
print(x, y, z)
Output:
apple banana cherry
Unpacks list elements into separate variables.
यह list के elements को अलग-अलग variables में store करता है।