Numbers and math
Work with whole and decimal numbers, the math library and rounding.
What you will be able to do
- Tell int and float apart
- Use arithmetic operators including // and %
- Round numbers with round()
- Use functions from the math module
In plain English
Python has two main number types: int for whole numbers and float for decimal numbers. Operators like +, -, *, /, // and % do arithmetic, and / always returns a float. round() trims a number to a set number of decimal places. The math module (reached with import math) adds helpers such as sqrt for square roots.
Step by step
- 1Use int for whole numbers and float for decimals.
- 2Use / for true division and // to drop the remainder.
- 3Trim decimals with round(value, places).
- 4Import math to reach functions like sqrt and floor.
import math
apples = 7
price = 3.14159
print(apples + 2)
print(apples / 2)
print(apples // 2)
print(apples % 2)
print(round(price, 2))
print(round(0.1 + 0.2, 2))
print(math.sqrt(16))
print(math.floor(price))Common mistakes
- Using / when you want whole-number division and being surprised by the .0 result.
- Treating 0.1 + 0.2 as exactly 0.3 instead of rounding it for display.
- Calling math.sqrt without import math first.
- Confusing % (the remainder) with division.
Your turn
EasySplit the bill
Split a bill of 47.5 between 3 people and print how much each pays, rounded to 2 decimal places.
PYTHON · starter
total = 47.5
people = 3
Check your understanding before moving on.
Knowledge checkQuestion 1
What does 7 // 2 evaluate to?
Select an answer to continue
Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.