Worksheets › Class 9 › Python Basics › Cheat sheet
Class 9 Python Basics: cheat sheet
CBSE Computational Thinking and AI. Python stores values in variables, works them out with operators in a fixed order, and has four basic types: int, float, str and bool.
Key ideas
- Python works out ** first, then *, /, // and %, then + and −, going left to right; brackets come before all of them.
- // gives the whole-number part of a division and % the remainder; / always gives a float.
- Every value has a type: int (7), float (3.5), str ('7') or bool (True, False).
- input() always gives text (a str); use int() or float() to turn it into a number.
- + adds numbers but joins strings; a string times a whole number repeats it.
- x += 3 is short for x = x + 3; likewise x *= 2 and x //= 4.
Remember
| Precedence | 5 + 3 * 2 = 11; (5 + 3) * 2 = 16; 2 ** 3 = 8 |
| // and % | 17 // 5 = 3; 17 % 5 = 2 (17 = 5 × 3 + 2) |
| / is a float | 10 / 5 gives 2.0 |
| Last digit; drop it | x % 10 and x // 10: 4923 → 3 and 492 |
| Text and numbers | int('12') + 3 = 15; str(4) + str(2) = '42'; 'ab' * 3 = 'ababab' |
| Tracing | x = 7 x += 3 print(x // 4, x % 4) # 2 2 |
Watch out
- Doing + before *: 5 + 3 * 2 is 11, not 16.
- Forgetting that / prints a decimal: 10 / 5 is 2.0, not 2.
- Doing arithmetic on input() without int(): typing 5 for a = input() makes a * 2 print 55.
Worked example
For x = 4923, what is (a) x % 10 (b) x // 10?
Working: % 10 gives the last digit; // 10 removes it
Answer: (a) 3; (b) 492
Practise Python Basics
Revise with this page, then practise: every Python Basics sheet in the app is new, with an answer key and working.
Coming soon for Android and iPhone.