AmbifulWorksheets
Cheat sheet
CBSE · Class 9 · Computational Thinking and AI
Python Basics
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
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