Python’s identifiers and keywords, data types, operators and their precedence, type conversion, input and print, and syntax, runtime and logical errors.
| // and % | 17 // 5 = 3, 17 % 5 = 2; −17 // 5 = −4, −17 % 5 = 3 |
| They always fit | a = b × (a // b) + a % b |
| Precedence | 5 + 3 * 2 = 11; 2 ** 3 ** 2 = 512; −3 ** 2 = −9 |
| Floats | 10 / 5 = 2.0; 7 // 2.0 = 3.0; 4 + 1.0 = 5.0; True + 4 = 5 |
| round() | a tie goes to the even neighbour: round(2.5) = 2, round(3.5) = 4 |
| Conversion | int('12') + 3 = 15; str(4) + str(2) = '42'; int(7.9) = 7 |
| and, or | 5 and 9 gives 9; 0 and 9 gives 0; 5 or 9 gives 5; 0 or 9 gives 9 |
| print() | print(1, 2, sep='-') prints 1-2; end='' stops the new line |
What does this program print? Give (a) the first line (b) the second.
a = 23
b = 1 - 23
print(a // b)
print(a % b)
Working: a = 23, b = −22: // rounds down (towards minus infinity), and a = b × (a // b) + a % b
Answer: (a) −2; (b) −21