Numbers & Operators

Python does math the way you'd expect from a calculator, plus a couple of operators that don't show up on one — floor division and the modulo operator both turn out to be genuinely useful, not just trivia.

The standard arithmetic operators

+, -, *, and / behave as you'd guess. Here's a small grade calculator that averages three test scores:

>>> averaging test scores
score1 = 85
score2 = 90
score3 = 78
average = (score1 + score2 + score3) / 3
print(round(average, 2))
Output
84.33

round() is doing real work here, not just tidying up the display — dividing numbers that don't split evenly produces a long decimal, and rounding to two places keeps it readable.

Floor division and the remainder

// divides and throws away anything after the decimal point; % gives you what's left over after that division. Together they're the standard way to convert a raw number of minutes into hours and minutes:

>>> converting minutes to hours and minutes
total_minutes = 135
hours = total_minutes // 60
minutes = total_minutes % 60
print(hours, minutes)
Output
2 15

135 minutes is 2 full hours (120 minutes) with 15 minutes left over — exactly what // and % hand back.

Exponents and order of operations

** raises a number to a power, and normal math precedence applies: multiplication and division happen before addition and subtraction, unless parentheses say otherwise.

>>> precedence in practice
print(2 ** 10)
print(2 + 3 * 4)
print((2 + 3) * 4)
Output
1024
14
20

Putting it together: a temperature converter

>>> celsius to fahrenheit
celsius = 24
fahrenheit = celsius * 9 / 5 + 32
print(fahrenheit)
Output
75.2
Note: mixing an int and a float in the same calculation always produces a float — and / always returns a float too, even when the division comes out even (10 / 2 gives 5.0, not 5). If you specifically want a whole number back, that's what // is for.