Ints, floats & arithmetic
Do math with integers and floats, including floor division and modulo.
Why arithmetic types matter
Every counter, price, average, and timestamp your code touches is a number, and Python has two everyday flavors: integers (int, whole numbers like 3 or -7) and floats (float, decimals like 3.14 or 2.0). The distinction is not cosmetic. An int is exact and can grow arbitrarily large; a float is a fixed-width binary approximation that trades exactness for a decimal point. Pick the wrong one and a report that should read 2 hours reads 2.0833333 hours, or a total that should be 100 drifts to 99.99999999. Interviewers and data pipelines both care which type you end up holding.
The operators, and the type each returns
7 + 2 # 9 int + int -> int
7 - 2 # 5
7 * 2 # 14
7 / 2 # 3.5 true division ALWAYS returns a float
2 ** 10 # 1024 power
The one to memorize: / always gives a float, even when the result is whole. 6 / 2 is 3.0, not 3. That is exactly what your average(a, b, c) exercise wants: sum the three numbers and divide by 3, and a decimal answer is correct.
Floor division and modulo: splitting into groups
// gives the whole number of times the divisor fits, and % gives what is left over. Together they split one number into a quotient and a remainder:
total = 125
hours = total // 60 # 2 whole hours
mins = total % 60 # 5 leftover minutes
Which operator answers each question: floor division // or modulo %?
That is the entire trick behind minutes_to_hm(total_minutes): return [total_minutes // 60, total_minutes % 60], which is [2, 5] for 125. Reach for // and % whenever you mean "how many whole groups" and "what is left".
Pitfalls
Float equality lies. Because floats are binary approximations, 0.1 + 0.2 == 0.3 evaluates to False (0.1 + 0.2 is actually 0.30000000000000004). Never compare floats with ==. Compare with a tolerance, for example abs(x - y) < 1e-9, or use math.isclose(x, y).
// is not truncation. // floors toward negative infinity, so -7 // 2 is -4, not -3. And % takes the sign of the divisor: -7 % 2 is 1 in Python. This surprises people coming from C or Java. For your minute problems the inputs are non-negative, so // and % line up with everyday intuition, but know the edge case exists.
| Expression | Python | C and Java | What differs |
|---|---|---|---|
| 7 // 3 | 2 | 2 | nothing: both agree on positives |
| 7 % 3 | 1 | 1 | nothing |
| -7 // 3 | -3 | -2 | Python floors toward negative infinity, C truncates toward zero |
| -7 % 3 | 2 | -1 | in Python the remainder takes the DIVISOR's sign |
| 7 % -3 | -2 | 1 | same rule, mirrored |
Interview nuance: the identity a == (a // b) * b + (a % b) always holds in Python, and because // floors (rather than truncating toward zero like C, Java, and Go), Python's % result always carries the sign of the divisor b, never the sign of a. Interviewers use this to test whether you actually know your language's division semantics: -7 % 3 is 2 in Python but -1 in C. When you need clock-style wraparound (an index that stays in range), Python's flooring % is the behavior you want.
total = 125
print(total // 60) # 2 (whole hours)
print(total % 60) # 5 (leftover minutes)
print(7 / 2) # 3.5
print(2 ** 10) # 1024Apply
Your turn
The task this lesson builds to.
Implement minutes_to_hm(total_minutes): split a number of minutes into hours and minutes.
Return a list [hours, minutes] where hours is the whole hours and minutes is what's left
over. For 125 minutes, return [2, 5].
3 hints and 4 automated checks are waiting in the workspace.
Practice
Make it stick
A second problem on the same idea, so it survives past today.
Implement average(a, b, c): return the mean of three numbers.
Add them up and divide by 3. The result may be a decimal (a float), which is fine.
2 hints and 4 automated checks are waiting in the workspace.