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, like5,0, or-12. - A float is a number with a decimal point, like
3.5,0.1, or2.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 = 12stores a whole number, so Python treats it as anint.height = 1.55has a decimal point, so it is afloat.type(age)asks Python what kind of valueageis. It reportsint. The same trick shows thatheightis afloat.
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
+,-andwork just like in school, but noticeis used for multiply (notx)./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, so10 / 2gives5.0, not5.
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 // 5is really 3.4, but//drops the.4and keeps3.20 // 6is 3.33..., which becomes3.
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 of2.10 % 2is0because 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 ** 3means 2 × 2 × 2, which is8.5 ** 2is 5 squared,25.- Raising to the power
0.5gives a square root, so9 ** 0.5is3.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, the3 4happens first (12), then2 + 12gives14. - Adding parentheses in
(2 + 3) * 4forces the addition first, giving20.
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, giving3.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:
total / peoplegives the exact share of23.75(a float, thanks to/).total // peoplegives23— the whole number of pounds each person pays if you only deal in whole pounds.total % peoplegives3, 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 // 60to get the whole minutes. - Use
total % 60to 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?
The single slash / is true division, which always returns a float. 7 / 2 is 3.5.
What does 17 % 5 give you?
The % operator returns the remainder. 17 divided by 5 is 3 with a remainder of 2, so 17 % 5 is 2.
What does ** do in Python?
** is the exponent operator. 2 ** 3 means 2 to the power of 3, which is 8.
What is the value of 2 + 3 * 4?
Multiplication happens before addition, so 3 * 4 is 12 first, then 2 + 12 is 14.
What number type is 5.0?
Any number written with a decimal point is a float, even if the part after the point is zero.
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.
Keep exploring
More in Coding