Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🐍 Unit 2 – Python Basics  ·  Lesson 12

Variables, Data Types और Mutability

Variable क्या है?

Variable एक नाम है जो memory में रखी value को refer करता है। इसे डिब्बे पर चिपका label समझिए। Variable = operator से value assign करके बनता है — कोई declaration नहीं चाहिए।

Python
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)
Ananya 16 5.4 True Next year Ananya will be 17
namevariable (label)refers to'Ananya'memory में str object
Variable का नाम एक object की ओर point करता है
⚠️ = का मतलब "बराबर" नहीं

Python में = का मतलब है "दाईं ओर की value बाईं ओर के नाम को दो"। इसलिए age = age + 1 बिल्कुल सही है: पुरानी age लो, 1 जोड़ो, वापस रखो। दो values compare करने के लिए == use होता है।

Assignment, multiple assignment और swap

Python
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)
5 six 7.0 0 0 0 9 3

x, y = y, x से swap में तीसरा variable नहीं चाहिए — viva का पसंदीदा प्रश्न।

सब कुछ object है: id() और type()

Python में हर value — number, string, list, यहाँ तक कि function भी — एक object है। हर object की एक identity (id()), एक type (type()) और एक value होती है।

Python
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))
True 15 10 False

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 है
Integers immutable हैं: x बदलने से नया object बनता है

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 नहीं)
Python data types का family tree
Python
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}))
<class 'int'> <class 'float'> <class 'complex'> <class 'bool'> <class 'str'> <class 'NoneType'> <class 'list'> <class 'tuple'> <class 'set'> <class 'dict'>

Numbers: int, float, complex, bool

Typeक्या रखता हैExamples
intकिसी भी size के पूर्ण numbers0, 45, -300, 2**100
floatDecimal point वाले numbers3.14, -0.5, 1.5e3 (=1500.0)
complexReal + imaginary part (j)3+4j
boolTrue या False (int का ही type: True = 1)marks >= 33
Python
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([]))
3.5 3 1267650600228229401496703205376 0.30000000000000004 3.0 4.0 5.0 2 0 False False True False

ध्यान दें: 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 नहीं"।
marks →900851772643
List values को positions 0, 1, 2 … पर क्रम से रखती है

Mutable और immutable types

Immutable (उसी जगह नहीं बदलते)

  • int, float, complex, bool
  • str
  • tuple
  • बदलने का मतलब नया object बनाना

Mutable (उसी जगह बदलते हैं)

  • list
  • dict
  • set
  • Same object बदलता है; सभी names को बदलाव दिखता है
Exams के लिए बहुत ज़रूरी
Python
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)
[1, 2, 3, 4] [1, 2, 3, 4] True
Python
s = 'Hello'
s[0] = 'J'
TypeError: 'str' object does not support item assignment

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।
✅ Practice

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 मिला)।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

क्या Python में variable का type declare करना पड़ता है?
नहीं। Python dynamically typed है: type उस value से तय होता है जो आप assign करते हैं। वही नाम बाद में दूसरे type की value को refer कर सकता है।
Python में कौन से data types mutable हैं?
List, dictionary और set mutable हैं — उन्हें उसी जगह बदला जा सकता है। int, float, complex, bool, str और tuple immutable हैं।
id() क्या return करता है?
Object की lifetime में उसका unique number (CPython में उसका memory address)। दो names का id same है तो वे एक ही object को refer करते हैं।
← Class 11 Computer Science
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicOperators, Expressions और Precedence आपकी प्रगति: 12 / 29 (41%) · Lesson 13 / 29 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें