Worksheets › Class 11 › Brief Overview of Python › Cheat sheet
Class 11 Brief Overview of Python: cheat sheet
CBSE Informatics Practices. Python basics: identifiers and keywords, data types, operators and their precedence, type conversion, errors, if-elif-else, and for and while loops.
Key ideas
- An identifier starts with a letter or _, has only letters, digits and _, is case-sensitive and cannot be a keyword such as for, if, True or None.
- Python works out ** first (right to left), then *, /, // and %, then + and −, going left to right; brackets come before all of them.
- / always gives a float (10 / 5 is 2.0); // gives the quotient rounded down and % the remainder.
- type() reports int, float, str, bool, list, tuple or dict; lists and dicts are mutable, while int, float, str and tuple are immutable.
- input() always returns a str; int(), float() and str() convert explicitly, and int() simply drops the fraction part.
- In an if-elif-else ladder only the FIRST true condition runs; indentation shows which lines belong to each block.
- range(a, b, c) starts at a, steps by c and stops before b; a while loop repeats as long as its condition is True.
- Syntax errors (a missing colon, bad indentation) stop a program before it runs; runtime errors such as ZeroDivisionError or ValueError happen while it runs; logical errors give wrong answers silently.
Remember
| Precedence | 20 + 6 * 3 = 38; 2 ** 3 ** 2 = 2 ** 9 = 512 |
| // and % | 47 // 5 = 9; 47 % 5 = 2 (47 = 5 × 9 + 2) |
| / gives a float | 12 / 4 → 3.0; 7 / 2 → 3.5 |
| Conversions | int(27.9) → 27; float(29) → 29.0; int('14') + 5 → 19 |
| print options | print(3, 4, 5, sep='-') → 3-4-5; print(3, end='#') then print(4) → 3#4 |
| Errors | 'Total: ' + 5 → TypeError; int('ten') → ValueError; an unknown name → NameError |
| Tracing a loop | total = 0 for i in range(1, 11): if i % 3 == 0: total = total + i print(total) # 3 + 6 + 9 = 18 |
Watch out
- Doing + before *, or working 2 ** 3 ** 2 from the left: it is 512, not 64.
- Writing 3 for 12 / 4: / always prints a float, 3.0.
- Counting range(a, b) up to b: the stop value is never included.
- Joining a str and an int with + (TypeError): convert with str() or int() first.
Worked example
What do these give? (a) int(27.9) (b) float(29) (c) int('14') + 5
Working: int() drops the fraction part; float() adds .0; int() turns the text into a number
Answer: (a) 27; (b) 29.0; (c) 19
Practise Brief Overview of Python
Revise with this page, then practise: every Brief Overview of Python sheet in the app is new, with an answer key and working.
Coming soon for Android and iPhone.