Numbers
Integers, floats, and arithmetic
In this lesson
Explain it like I’m 5
Whole numbers are ints; numbers with a decimal point are floats. Python does the arithmetic exactly the way you’d expect — with two kinds of division worth knowing.
int and float
int is a whole number (7); float has a decimal point (7.0). Any operation involving a float usually produces a float. You rarely have to think about it, but it explains why 4 / 2 gives 2.0 rather than 2.
print(4 / 2)
print(type(4 / 2))
print(5 + 2.0)
2.0 <class 'float'> 7.0
The operators
+ - *— add, subtract, multiply/— true division, always a float:7 / 2 == 3.5//— floor division, drops the remainder:7 // 2 == 3%— modulo, the remainder:7 % 2 == 1**— power:2 ** 3 == 8
% is more useful than it looks: n % 2 == 0 tests for even numbers, and modulo is how you wrap values around (clocks, cycles).
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(2 ** 10)
3.5 3 1 1024
A pizza is cut into 8 slices and 3 friends share it equally. Print how many whole slices each gets (floor division) and how many are left over (modulo).
slices = 8
friends = 3
print(slices // friends)
print(slices % friends)
// gives the whole number each; % gives the remainder.
slices = 8
friends = 3
print("each:", slices // friends)
print("left over:", slices % friends)
2
2
Rounding and the floating-point surprise
round(x, n) rounds to n decimal places. But beware: 0.1 + 0.2 prints 0.30000000000000004. That’s not a Python bug — computers store decimals in binary, which can’t represent some fractions exactly. For money, round when displaying, or use the decimal module for exactness.
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)
print(round(0.1 + 0.2, 2))
0.30000000000000004 False 0.3
total = 19.99
qty = 3
grand = total * qty
print(grand)
print(round(grand, 2))
59.97 59.97
Handy built-in number tools
Python includes small helpers you’ll use all the time, with no import needed: abs(x) gives the distance from zero (so abs(-7) is 7), min() and max() pick the smallest or largest of several values (or of a list), and divmod(a, b) hands back the whole-number quotient and the remainder together as a pair.
For heavier maths — square roots, π, trigonometry — Python’s standard-library math module has it covered. Learn more about importing modules here.
print(abs(-7))
print(min(4, 9, 2))
print(max([3, 8, 5]))
print(divmod(17, 5))
7 2 8 (3, 2)
Common mistake: Expecting / to give a whole number
In Python 3, / is always true division and returns a float, so 10 / 2 is 5.0, not 5.
Use // when you want an integer result, or wrap with int(...) if appropriate.
print(10 // 2) # 5
print(10 / 2) # 5.0
What is 17 % 5?
Modulo is the remainder: 17 = 3×5 + 2, so the answer is 2.
Which operator gives the remainder of a division?
% is modulo (remainder); // is the whole-number quotient.
Mini exercise (medium)
You have 100 minutes. Print how many whole hours that is and how many minutes are left over. Use // for whole hours and % for the remainder.
Do it here. Finish the code below and press Run to try your answer right in the browser — no setup needed.
total_minutes = 100
hours = 0 # TODO: whole hours — use //
minutes = 0 # TODO: leftover minutes — use %
print(hours, "hours", minutes, "minutes")
Hours = 100 // 60; leftover minutes = 100 % 60.
total_minutes = 100
hours = total_minutes // 60
minutes = total_minutes % 60
print(hours, "hours", minutes, "minutes")
1 hours 40 minutes
__no_output_bypass__ = True
import ast as _ast
_binops = [type(n.op) for n in _ast.walk(_ast.parse(__user_code__)) if isinstance(n, _ast.BinOp)]
assert _ast.FloorDiv in _binops, "use // (floor division) to get whole hours"
assert _ast.Mod in _binops, "use % (modulo) to get the leftover minutes"
__no_output_bypass__ = False
assert hours == 1 and minutes == 40, "100 minutes is 1 hour with 40 minutes left over"
print("✓ Correct!")