2. Data Types and Operators
2.4 Arithmetic and comparison operators
Highlights & notes
Select lesson text to highlight it, or tap Add note.
Bookmarks
Bookmark a lesson to see it here.
Maths signs do sums. Comparison signs are a weighing scale that only says True or False.
- // shares sweets equally: how many each child gets.
- % is the sweets left over.
- == asks "are these the same?"
Thumb rule: One = puts a value in a box. Two == asks a question.
Watch it run Β· 1
print(7 // 2, 7 % 2)3 1
Watch it run Β· 2
print(5 > 3)True
Python does maths with arithmetic operators:
+add,-subtract,*multiply/divide, which always gives a float:10 / 2is5.0//whole groups:23 // 10is2%remainder:23 % 10is3power:2 3is8
Comparison operators ask a yes/no question and give True or False: == equal, != not equal, <, >, <=, >=.
Combine checks with and (both must be true), or (at least one must be true) and not (flips the answer).
Batching 23 rows into groups of 10
23 // 10β2full batches23 % 10β3rows left over23 % 10 > 0βTrue, so you need one more small batch
/ always returns a float, even for whole results: 10 / 5 is 2.0. Use // when you need a whole number.
How it fits together
- 23 rows
- 23 // 10 β 2 batches
- 23 % 10 β 3 left
Step through the code
Python
rows, batch_size = 23, 10full_batches = rows // batch_sizespare_rows = rows % batch_sizeprint(full_batches, spare_rows)print(spare_rows > 0)
Output
2 3 True
Practice in Python β Solved
For 35 rows and batches of 8, print the number of full batches followed by the remaining row count.
Expected output
4 3
Reference solution
rows = 35
batch_size = 8
print(rows // batch_size, rows % batch_size)
Question 1 Β· Easy
What does 7 % 3 give?
Question 2 Β· Medium
What does print(10 / 4, 10 // 4) show?
Question 3 Β· Hard
What does print(5 > 3 and not 2 == 2) show?
Key takeaways
Use arithmetic to calculate values and comparisons to test them.
Save your progress
Start free and your progress, code and notes stay saved. Free for 1 month, no card needed.
Start free Log in