AmbifulWorksheets
Cheat sheet
CBSE · Class 11 · Informatics Practices

Brief Overview of Python

Python basics: identifiers and keywords, data types, operators and their precedence, type conversion, errors, if-elif-else, and for and while loops.

Key ideas

Remember

Precedence20 + 6 * 3 = 38; 2 ** 3 ** 2 = 2 ** 9 = 512
// and %47 // 5 = 9; 47 % 5 = 2 (47 = 5 × 9 + 2)
/ gives a float12 / 4 → 3.0; 7 / 2 → 3.5
Conversionsint(27.9) → 27; float(29) → 29.0; int('14') + 5 → 19
print optionsprint(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 looptotal = 0
for i in range(1, 11):
    if i % 3 == 0:
        total = total + i
print(total) # 3 + 6 + 9 = 18

Watch out

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