Lecture 1
Operators do the maths, compare values, and combine True/False answers.
Lecture slides
Notebooks (open in Colab)
** is a power, // divides and drops the decimal part, % gives the remainder. Python follows the usual order — ** first, then * / // %, then + and - — and brackets go first of all.
print(2 ** 5)print(17 // 5, 17 % 5)print(2 + 3 * 4, (2 + 3) * 4)Output
32 3 2 14 20
== asks 'equal?' (a single = stores a value). != means 'not equal'. and needs both sides True, or needs at least one, and not flips True and False.
od = 0.45print(od == 0.45, od != 0.45)print(od > 0.4 and od < 0.6)print(not od > 0.4)Output
True False True False
For the advanced questions.
0.4 < od < 0.6 means 0.4 < od and od < 0.6. Together, // and % split a quantity into whole units and what is left over.
od = 0.7print(0.4 < od < 0.6)seconds = 200print(seconds // 60, "min", seconds % 60, "s")Output
False 3 min 20 s