🔢
Coding🔬 Ages 11-13Beginner 11 min read

Python Numbers and Math

Learn Python numbers and math: integers, floats, the arithmetic operators, floor division, modulo, exponents, operator order, and rounding. Runnable code, a worked example and a quiz.

Key takeaways

  • Python has two main number types: integers (whole numbers) and floats (decimals)
  • The math operators are +, -, *, /, // (floor division), % (modulo) and ** (exponent)
  • Regular division with / always gives a float, even for an exact answer
  • Modulo (%) gives the remainder, which is great for checking even or odd numbers
  • Python follows order of operations, and parentheses let you control it

Numbers are everywhere in code

Almost every program does some kind of maths. Games keep score, shops add up prices, and apps count likes. To do any of this you need to understand how Python stores and works with numbers. The good news is that Python is excellent at maths, and the basics feel a lot like the arithmetic you already know.

If you've already met variables, you can store numbers in them and do calculations. This lesson goes deeper into the number types and the operators that work on them.

Two kinds of numbers: integers and floats

Python has two main number types:

  • An integer (or int) is a whole number with no decimal point, like 5, 0, or -12.
  • A float is a number with a decimal point, like 3.5, 0.1, or 2.0.
age = 12          # this is an integer
height = 1.55     # this is a float

print(type(age))     # <class 'int'>
print(type(height))  # <class 'float'>

Line by line:

  • age = 12 stores a whole number, so Python treats it as an int.
  • height = 1.55 has a decimal point, so it is a float.
  • type(age) asks Python what kind of value age is. It reports int. The same trick shows that height is a float.

Even 2.0 is a float, because the decimal point is there. The number 2 (no point) is an integer. They look almost the same to us, but Python keeps them as different types.

The arithmetic operators

Here are the symbols Python uses for maths:

print(10 + 3)   # 13   addition
print(10 - 3)   # 7    subtraction
print(10 * 3)   # 30   multiplication
print(10 / 3)   # 3.3333333333333335  division
  • +, - and work just like in school, but notice is used for multiply (not x).
  • / is division. Look carefully: even though we divided two whole numbers, the answer is a float. In Python, / always produces a float, even when the result is exact, so 10 / 2 gives 5.0, not 5.

Floor division and modulo

Two operators are less familiar but very useful.

Floor division // divides and then throws away anything after the decimal point, rounding down to a whole number:

print(17 // 5)   # 3
print(20 // 6)   # 3
  • 17 // 5 is really 3.4, but // drops the .4 and keeps 3.
  • 20 // 6 is 3.33..., which becomes 3.

Modulo % gives the remainder after division:

print(17 % 5)    # 2
print(20 % 6)    # 2
print(10 % 2)    # 0
  • 17 % 5: 5 goes into 17 three times (15), leaving a remainder of 2.
  • 10 % 2 is 0 because 2 divides into 10 perfectly.

Modulo is incredibly handy. A number is even exactly when number % 2 == 0, and odd when the remainder is 1. You can use it inside the kind of decisions you learned about in conditionals in Python:

number = 7
if number % 2 == 0:
    print("even")
else:
    print("odd")

This prints odd, because 7 % 2 is 1, not 0.

Exponents (powers)

To raise a number to a power, use **:

print(2 ** 3)    # 8     (2 to the power of 3)
print(5 ** 2)    # 25    (5 squared)
print(9 ** 0.5)  # 3.0   (the square root of 9)
  • 2 ** 3 means 2 × 2 × 2, which is 8.
  • 5 ** 2 is 5 squared, 25.
  • Raising to the power 0.5 gives a square root, so 9 ** 0.5 is 3.0.

Order of operations

When several operators appear together, Python doesn't just go left to right. It follows the usual maths rules: brackets first, then **, then * / // %, and finally + -.

print(2 + 3 * 4)      # 14, not 20
print((2 + 3) * 4)    # 20
print(2 ** 3 + 1)     # 9
  • In 2 + 3 4, the 3 4 happens first (12), then 2 + 12 gives 14.
  • Adding parentheses in (2 + 3) * 4 forces the addition first, giving 20.

When in doubt, add brackets. They make your intention clear to Python and to anyone reading your code.

Rounding numbers

Floats can have long, messy decimals. The built-in round() function tidies them up:

price = 19.99
tax = price * 0.20
print(tax)            # 3.998
print(round(tax, 2))  # 4.0
print(round(3.14159, 2))  # 3.14
  • round(tax, 2) rounds to 2 decimal places.
  • round(3.14159, 2) keeps two decimals, giving 3.14.

Worked example: splitting a bill

Let's combine these ideas into a small program that splits a restaurant bill between friends and works out the leftover.

total = 95
people = 4

each = total / people        # exact share
whole = total // people      # whole pounds each
leftover = total % people    # pennies left over

print("Exact share:", each)        # 23.75
print("Whole pounds each:", whole) # 23
print("Leftover:", leftover)       # 3

How it works:

  1. total / people gives the exact share of 23.75 (a float, thanks to /).
  2. total // people gives 23 — the whole number of pounds each person pays if you only deal in whole pounds.
  3. total % people gives 3, the amount left over after handing out 23 to each of 4 people (23 × 4 = 92, and 95 − 92 = 3).

The same three operators describe the whole problem cleanly: / for the exact answer, // for the whole part, and % for the remainder.

Try it yourself

Write a program that converts a number of seconds into minutes and seconds:

  • Store a number of seconds, for example total = 200.
  • Use total // 60 to get the whole minutes.
  • Use total % 60 to get the leftover seconds.
  • Print a message like 200 seconds is 3 minutes and 20 seconds.

Then experiment: change total to 125 and 90, and check the output is correct each time. Once you're comfortable, see how numbers behave inside repetition over in python loops and lists, where you can run the same calculation on many values at once.

Quick quiz

Test yourself and earn XP

What is the result of 7 / 2 in Python?

What does 17 % 5 give you?

What does ** do in Python?

What is the value of 2 + 3 * 4?

What number type is 5.0?

FAQ

Computers store floats in binary, and some decimal fractions like 0.1 cannot be written exactly in binary. This causes tiny rounding errors, so 0.1 + 0.2 prints as 0.30000000000000004. This happens in almost every programming language, not just Python. For money and exact work you can round the result with round(), or use the decimal module.

Use / when you want the exact answer including any decimal part, such as splitting a bill. Use // (floor division) when you only want the whole-number part, such as working out how many full boxes of 12 you can fill from a pile of items. // throws away the remainder and rounds down to the nearest whole number.