Python Revision Tour
Tokens, variables and data types
Class XII builds on everything from Class XI. A Python program is made of tokens: keywords (if, def, return), identifiers (total), literals (25, 'CS', True, None), operators (+ == and) and punctuators (( ) [ ] : ,). Variables are names bound to objects; the type comes from the 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
- Changing creates a new object
- Can be dictionary keys
Mutable
- list, dict, set
- Changed in place — every name sharing it sees the change
- Cannot be 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 creates an alias; c = a[:] creates a copy. Strings are immutable, so s2 += '12' makes a new string and s is unchanged.
Operators and precedence
Order from high to low: () → ** (right to left) → 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 returns 0 (the first false value); 'a' < 'B' is False because 'a' is 97 and 'B' is 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(' ', '-'))Must-know methods: len, upper, lower, title, capitalize, count, find, index, replace, split, join, strip, startswith, endswith, isalpha, isdigit, isalnum, isupper, islower, isspace.
Lists, tuples and 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 | Key methods |
|---|---|
| list | append, extend, insert, pop, remove, index, count, sort, reverse, copy, clear |
| tuple | count, index (and 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
Find the output (trace word by word):
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())Predict: L = [2, 4, 6]; L[1:2] = [9, 9]; print(L, len(L)) and print('-'.join('CS'.lower() * 2)).
Answers: [2, 9, 9, 6] 4 and c-s-c-s.
Quick revision table
| Concept | Remember |
|---|---|
| Integer division | -7 // 2 = -4 (floor), -7 % 2 = 1 |
| Slicing | s[a:b] excludes b; s[::-1] reverses |
| Single-item tuple | (5,) not (5) |
| Dictionary keys | Unique, immutable |
| Alias vs copy | b = a shares; a[:], list(a), a.copy() copy |
| Loop else | Runs only if no break |
