Variables, Data Types और Mutability
Variable क्या है?
Variable एक नाम है जो memory में रखी value को refer करता है। इसे डिब्बे पर चिपका label समझिए। Variable = operator से value assign करके बनता है — कोई declaration नहीं चाहिए।
name = 'Ananya' # str
age = 16 # int
height = 5.4 # float
is_monitor = True # bool
print(name, age, height, is_monitor)
age = age + 1 # value of a variable can change
print('Next year', name, 'will be', age)Python में = का मतलब है "दाईं ओर की value बाईं ओर के नाम को दो"। इसलिए age = age + 1 बिल्कुल सही है: पुरानी age लो, 1 जोड़ो, वापस रखो। दो values compare करने के लिए == use होता है।
Assignment, multiple assignment और swap
a, b, c = 5, 'six', 7.0 # many values at once print(a, b, c) p = q = r = 0 # same value to many print(p, q, r) x, y = 3, 9 x, y = y, x # swap in one line print(x, y)
x, y = y, x से swap में तीसरा variable नहीं चाहिए — viva का पसंदीदा प्रश्न।
सब कुछ object है: id() और type()
Python में हर value — number, string, list, यहाँ तक कि function भी — एक object है। हर object की एक identity (id()), एक type (type()) और एक value होती है।
x = 10 y = x print(id(x) == id(y)) # both names point to the same object x = x + 5 # a NEW object 15 is created for x print(x, y, id(x) == id(y))
y = x के बाद
- x → 10
- y → 10 (same object)
- id(x) == id(y) True है
x = x + 5 के बाद
- x → 15 (नया object)
- y → 10 (वैसा ही)
- id(x) == id(y) False है
Data types एक नज़र में
Python data 📁 types/ ├── 📁 Numbers/ │ ├── int 25, -7, 10**20 │ ├── float 3.14, -0.5, 2.0e3 │ ├── complex 3+4j │ └── bool True, False ├── 📁 Sequences/ │ ├── str 'Hello', "नमस्ते" │ ├── list [10, 'a', 2.5] │ └── tuple (10, 20, 30) ├── 📁 Set/ │ └── set {1, 2, 3} ├── 📁 Mapping/ │ └── dict {'roll': 1, 'name': 'Riya'} └── None None (कोई value नहीं)
print(type(25), type(3.5), type(2 + 3j))
print(type(True), type('Hi'), type(None))
print(type([1, 2]), type((1, 2)), type({1, 2}), type({'a': 1}))Numbers: int, float, complex, bool
| Type | क्या रखता है | Examples |
|---|---|---|
int | किसी भी size के पूर्ण numbers | 0, 45, -300, 2**100 |
float | Decimal point वाले numbers | 3.14, -0.5, 1.5e3 (=1500.0) |
complex | Real + imaginary part (j) | 3+4j |
bool | True या False (int का ही type: True = 1) | marks >= 33 |
print(7 / 2, 7 // 2) # float division, integer division
print(2 ** 100) # int has no size limit
print(0.1 + 0.2) # floats are approximate
z = 3 + 4j
print(z.real, z.imag, abs(z))
print(True + True, int(False))
print(bool(0), bool(''), bool('0'), bool([]))ध्यान दें: 0.1 + 0.2 ठीक 0.3 नहीं है क्योंकि floats binary में लगभग (approximately) store होते हैं। पैसे दिखाने के लिए round(x, 2) use करें। ख़ाली values (0, '', []) False हैं; लगभग बाकी सब True — string '0' भी।
Sequences, sets, mappings और None
- str — quotes में characters का ordered sequence:
'Python'। - list — square brackets में ordered, बदलने वाला collection:
[90, 85, 77]। - tuple — round brackets में ordered, न बदलने वाला collection: location के लिए
(28.6, 77.2)। - set — unique items का unordered collection:
{'red', 'green'}। - dict — key: value pairs:
{'name': 'Riya', 'class': 11}। - None — ख़ास value जिसका मतलब "कुछ नहीं / अभी कोई value नहीं"।
Mutable और immutable types
Immutable (उसी जगह नहीं बदलते)
- int, float, complex, bool
- str
- tuple
- बदलने का मतलब नया object बनाना
Mutable (उसी जगह बदलते हैं)
- list
- dict
- set
- Same object बदलता है; सभी names को बदलाव दिखता है
L = [1, 2, 3] M = L # M is another name for the SAME list M.append(4) # list changes in place print(L, M, L is M)
s = 'Hello' s[0] = 'J'
List example दिखाता है कि M = L list की copy नहीं बनाता; दोनों names एक ही list share करते हैं। सही copy करना आप list lesson में सीखेंगे।
सारांश और practice
- Variable = object को refer करने वाला नाम; assignment से बनता है।
- हर object की id, type और value होती है।
- Types: int, float, complex, bool, str, list, tuple, set, dict, None।
- Mutable: list, dict, set। Immutable: numbers, str, tuple।
Output बताइए: a = [5]; b = a; b.append(6); print(a) और s = 'ab'; t = s; t = t + 'c'; print(s)।
Answers: [5, 6] (list mutable और shared है) और ab (strings immutable हैं; t को नया object मिला)।
