Lesson 3 of 34 · Fundamentals
Python Numbers and Arithmetic Operators
Games count gold, apps convert units and scripts crunch numbers all day long. Python has two everyday number types and a small set of arithmetic operators that cover almost everything you'll need.
This lesson also introduces a tiny piece of syntax you'll use in every exercise from now on: writing your code inside a function, so the tests can try it with many different inputs.
int and float
Whole numbers are int; numbers with a decimal point are float:
gold = 250 # int
price = 4.99 # float
huge = 2 ** 100 # ints can be as large as you like
budget = 1_000_000 # underscores make long numbers readable
Mixing them is fine: 3 + 0.5 gives the float 3.5.
Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
addition | 7 + 2 |
9 |
- |
subtraction | 7 - 2 |
5 |
* |
multiplication | 7 * 2 |
14 |
/ |
division | 7 / 2 |
3.5 |
// |
floor division | 7 // 2 |
3 |
% |
modulo (remainder) | 7 % 2 |
1 |
** |
exponent (power) | 7 ** 2 |
49 |
Two details surprise newcomers:
/always returns a float, even when the division is exact:6 / 3is2.0.//rounds down, toward negative infinity, so-7 // 2is-4, not-3.
// and % are a perfect pair for splitting a quantity into units. With 17 potions and boxes of 5, 17 // 5 is 3 full boxes and 17 % 5 is 2 potions left over. The % operator is also the standard divisibility test: n % 2 == 0 means n is even.
Precedence and parentheses
Python follows the usual math order: ** first, then *, /, // and %, and finally + and -. Operators on the same level run left to right. Use parentheses to change the order — or simply to make your intent obvious:
print(2 + 3 * 4) # 14
print((2 + 3) * 4) # 20
print(-2 ** 2) # -4 (the power happens before the minus!)
Updating a variable
Augmented assignment combines an operation with assignment:
score = 10
score += 5 # same as score = score + 5 -> 15
score *= 2 # 30
score //= 4 # 7
Helpful built-in functions
print(abs(-7)) # 7
print(round(3.14159, 2)) # 3.14
print(max(3, 9, 4)) # 9
print(min(3, 9, 4)) # 3
Floats are approximations
Computers store floats in binary, so some decimal values can't be represented exactly:
print(0.1 + 0.2) # 0.30000000000000004
This happens in every programming language. Round the numbers you display, and avoid comparing floats with == when tiny errors matter. Also note that round() sends exact halves to the nearest even number: round(2.5) is 2 and round(3.5) is 4.
Your code goes inside a function
From now on, each exercise gives you a function to complete. You'll master functions in the next chapter; for now you only need this much:
def double(number):
return number * 2
def double(number):creates a function nameddoublethat receives a value callednumber. The indented lines underneath belong to the function.returnhands the result back to whoever called the function. The tests calldouble(21)and check that the answer is42.
Starter code uses the keyword pass as a placeholder that does nothing. Replace it with your return statement.
Assignment
Help the Hollow's timekeeper turn a number of seconds into hours, minutes and seconds. Complete three functions:
whole_hours(total_seconds)— the number of complete hours.remaining_minutes(total_seconds)— the complete minutes left over after removing the full hours (0–59).remaining_seconds(total_seconds)— the seconds left over after removing the full minutes (0–59).
For 7384 seconds, which is 2 hours, 3 minutes and 4 seconds:
whole_hours(7384) # 2
remaining_minutes(7384) # 3
remaining_seconds(7384) # 4
All three functions must return int values. Use // and % — no loops needed.
Numbers and math operators in other languages
- JavaScript Numbers and Math Operators · JavaScript
- Go Constants and Arithmetic Operators · Go
- Java Operators: Integer vs Double Math · Java
- C# Operators: Arithmetic, Comparison and Logic · C#
Need a quick reminder? The Python cheat sheet has every topic of Learn Python on one page.
Lesson last updated on .