Python Revision Tour
Tokens, variables और data types
Class XII, Class XI की हर चीज़ पर टिका है। Python program tokens से बनता है: keywords (if, def, return), identifiers (total), literals (25, 'CS', True, None), operators (+ == and) और punctuators (( ) [ ] : ,)। Variables objects से जुड़े नाम हैं; type value से आता है (dynamic typing)।
Data 📁 types/ ├── 📁 Numbers/ int, float, complex, bool ├── 📁 Sequences/ str, list, tuple ├── 📁 Mapping/ dict ├── 📁 Set/ set └── None
Mutable vs immutable
Immutable
- int, float, bool, str, tuple
- बदलने पर नया object बनता है
- Dictionary keys बन सकते हैं
Mutable
- list, dict, set
- उसी जगह बदलते हैं — share करने वाले हर नाम को बदलाव दिखता है
- Dictionary keys नहीं बन सकते
a = [1, 2, 3] b = a c = a[:] b.append(4) c.append(5) print(a, b, c) s = 'cs' s2 = s s2 += '12' print(s, s2)
b = a alias बनाता है; c = a[:] copy बनाता है। Strings immutable हैं, इसलिए s2 += '12' नई string बनाता है और s नहीं बदलती।
Operators और precedence
ऊँचे से नीचे क्रम: () → ** (दाएँ से बाएँ) → unary + - ~ → * / // % → + - → comparisons, in, is → not → and → or।
print(5 // 2 * 2, 2 ** 3 ** 0, not 0 or 5, 7 and 0, 'a' < 'B')
7 and 0 0 return करता है (पहली false value); 'a' < 'B' False है क्योंकि 'a' 97 है और 'B' 66।
Flow of control
x = 10
while x > 0:
x -= 3
print(x)
for i in range(3):
pass
else:
print('loop finished, i =', i)Strings
s = 'Board Exam 2026'
print(s[6:10], s[-4:], s.lower().count('a'))
print(s.split(), s.replace(' ', '-'))ज़रूरी methods: len, upper, lower, title, capitalize, count, find, index, replace, split, join, strip, startswith, endswith, isalpha, isdigit, isalnum, isupper, islower, isspace।
Lists, tuples और dictionaries
L = [5, 'two', 3.0, [7, 8]]
print(len(L), L[3][1], L[-2:])
t = (1, 2, 3) * 2
print(t, t.index(3))
d = {'a': 1}
d.setdefault('b', 2)
print(d, list(d.items()))| Type | मुख्य methods |
|---|---|
| list | append, extend, insert, pop, remove, index, count, sort, reverse, copy, clear |
| tuple | count, index (और len, max, min, sum, sorted) |
| dict | keys, values, items, get, update, pop, popitem, setdefault, copy, clear, fromkeys |
Comprehensions
print([i * i for i in range(6) if i % 2])
print({k: len(k) for k in ['cs', 'ip', 'maths']})Board-style output questions
Output बताइए (शब्द-दर-शब्द trace करें):
text = 'Python Is Easy To Learn'
words = text.split()
new = ''
for w in words:
if len(w) > 2:
new += w[0].lower() + w[1:].upper() + ' '
else:
new += w + ' '
print(new.strip())Output बताइए: L = [2, 4, 6]; L[1:2] = [9, 9]; print(L, len(L)) और print('-'.join('CS'.lower() * 2))।
Answers: [2, 9, 9, 6] 4 और c-s-c-s।
Quick revision table
| Concept | याद रखें |
|---|---|
| Integer division | -7 // 2 = -4 (floor), -7 % 2 = 1 |
| Slicing | s[a:b] में b शामिल नहीं; s[::-1] उल्टा |
| Single-item tuple | (5,) न कि (5) |
| Dictionary keys | Unique, immutable |
| Alias vs copy | b = a share करता है; a[:], list(a), a.copy() copy करते हैं |
| Loop else | break न हो तभी चलता है |
