Numbers and Math
Python has two kinds of numbers: integers (whole numbers like 7) and
floats (decimals like 3.75). You combine them with math operators.
Arithmetic operators
+add,-subtract,*multiply/divide — always gives a float (10 / 2is5.0)//floor divide — divides and drops the decimal part (7 // 2is3)%modulo — the remainder left over (7 % 2is1)**power (2 ** 3is8)
print(10 + 3)
print(10 / 3)
print(10 // 3)
print(10 % 3)
Division always makes a float
Even when it divides evenly, / produces a decimal number:
print(6 / 3)
That prints 2.0, not 2. Any use of / gives a float — worth
remembering when your answer has an unexpected .0 on the end.
Rounding
round(value, n) rounds a number to n decimal places:
average = 37 / 3
print(average)
print(round(average, 2))
Order of operations
Python follows normal maths order — * and / happen before + and -.
Use parentheses when you want a different order:
print(2 + 3 * 4)
print((2 + 3) * 4)
See also: Variables.
Your turn
Add two lines: print a floor-divided by b, then print the remainder of
a divided by b.
a = 17
b = 5
# add two prints: floor division first, then the remainder
Expected output
3
2
Practise this in the free Python course, where every exercise checks your code instantly.