← All concepts

Lecture 1

Operators: arithmetic, comparison and logic

Operators do the maths, compare values, and combine True/False answers.

Arithmetic

** 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

Comparisons and logic

== 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.45
print(od == 0.45, od != 0.45)
print(od > 0.4 and od < 0.6)
print(not od > 0.4)

Output

True False
True
False

Going further

For the advanced questions.

Chained comparisons and splitting units

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.7
print(0.4 < od < 0.6)
seconds = 200
print(seconds // 60, "min", seconds % 60, "s")

Output

False
3 min 20 s