Worksheets › Class 12 › Python Revision › Cheat sheet
Class 12 Python Revision: cheat sheet
CBSE Computer Science. A revision of Class 11 Python: strings and slicing, string methods, and list, tuple and dictionary operations, traced to the exact output.
Key ideas
- Indexes start at 0 (or −1 from the end); a slice [start:stop:step] stops before stop, and a negative step goes backwards.
- Strings and tuples are immutable; lists and dictionaries are mutable and change in place.
- split() breaks a string at spaces and newlines into a list of words; join() puts a separator between strings.
- append, insert, extend, remove, sort and reverse change a list and give back None; pop() gives back the item it removes.
- A dictionary keeps the order of insertion: a new key goes at the end, and an existing key keeps its place when its value changes.
- In a nested list M[i][j] is row i, column j, both counted from 0.
Remember
| s = 'python' | s[1:4] = 'yth', s[-2:] = 'on', s[::2] = 'pto', s[::-1] = 'nohtyp' |
| String methods | 'a b c'.split() = ['a', 'b', 'c']; '#'.join(['x', 'y']) = 'x#y' |
| Case | 'my BOOK'.capitalize() = 'My book'; 'my BOOK'.title() = 'My Book' |
| Lists | L.sort() gives None; sorted(L) is a new list; L.pop() gives the last item |
| Tuples | (1, 2) * 2 = (1, 2, 1, 2); (7,) has one item |
| Dictionaries | d.get('x', 0) gives 0 for a missing key |
Watch out
- Including the stop index of a slice.
- Writing L = L.append(5): L becomes None.
- Thinking > keeps the last of two equal items: it keeps the first.
- Mixing up row and column in M[i][j].
Worked example
Trace this program. Give (a) the first line it prints (b) the second (c) the third. s = 'candle kite forest river ladder' words = s.split() longest = '' initials = '' for w in words: if len(w) > len(longest): longest = w initials = initials + w[0].upper() print(len(words)) print(longest) print(initials)
Working: words: candle, kite, forest, river, ladder; the first longest word is kept because > is strict
Answer: (a) 5; (b) candle; (c) CKFRL
Practise Python Revision
Revise with this page, then practise: every Python Revision sheet in the app is new, with an answer key and working.
Coming soon for Android and iPhone.