Fundamentals · Lesson 5 of 94

Numbers and arithmetic

Watch this lesson

View full playlist ↗

Read the lesson

What you'll learn

Concept

Python has two everyday number types. An int is a whole number with no size limit; you can compute 2 to the power of 1000 and get every digit. A float is a number with a decimal point, stored in a fixed 64 bits, which means it is an approximation. Most of the time that does not matter. Occasionally it matters a great deal, and knowing when is what separates a beginner from a developer.

The operators are the ones from school plus three you may not have seen. +, -, and * do what you expect. / always gives a float, even for 6 divided by 3. // is floor division: it divides and rounds down to a whole number. % is the modulo operator: it gives the remainder after division. ** is exponentiation, so 2 ** 10 is 1024.

Here is how the operators behave on the same pair of numbers.

Expression Result Type What it means
7 + 2 9 int addition
7 / 2 3.5 float true division, always a float
7 // 2 3 int floor division, rounds down
7 % 2 1 int remainder
7 ** 2 49 int seven squared

Mixing types follows one rule: if any operand is a float, the result is a float. Python never silently turns a float back into an int, because that would lose information. When you want an int, you ask for it with int(), which truncates toward zero, or round(), which rounds to the nearest whole number.

Order of operations follows the math you learned: ** first, then *, /, //, %, then + and -, with parentheses overriding everything. 2 + 3 * 4 is 14, not 20. When an expression has more than two operators, add parentheses even if they are not needed; the next reader, who is often you in a month, should not have to remember precedence tables.

Floats cannot represent most decimal fractions exactly, in the same way that a third cannot be written exactly in decimal. So 0.1 + 0.2 is not quite 0.3. This is not a Python bug; every language using 64-bit floats does it. The fix is to round for display, and to use the decimal module or integer cents when money is involved.

Code

Start with the operators on a single pair of numbers. Watch the type of each result: only true division produces a float here.

# operators.py
a = 7
b = 2

print(a + b, a - b, a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)
Output
9 5 14
3.5
3
1
49

Converting between types is explicit. int() truncates, round() rounds, float() widens, and str() turns a number into text so you can join it to other text.

# conversions.py
price = 19.99

print(int(price))
print(round(price))
print(round(price, 1))
print(float(3))
print("Price: " + str(price))
Output
19
20
20.0
3.0
Price: 19.99

Now the surprise, and the correct response to it. The raw sum is off by a tiny amount; rounding for display hides it, and comparing with a tolerance is how you test for equality with floats.

# float_surprise.py
total = 0.1 + 0.2

print(total)
print(total == 0.3)
print(round(total, 2))
print(abs(total - 0.3) < 1e-9)
Output
0.30000000000000004
False
0.3
True

Try it yourself

Create tip.py. Store a bill of 84.5 and a tip rate of 0.15. Compute the tip, the total, and the total split between 3 people, and print each rounded to 2 decimal places. Then change the split to 4 people and check the numbers make sense.

Common mistakes

What's next

Numbers are covered. Next comes the other everyday value, text, and how Python lets you pick it apart character by character.

Course overview