6. Python Numbers
What you will learn
- Integers and floats
- The arithmetic operators, including
//and% - Rounding and useful math functions
- Random numbers and exact decimals
Integers and floats
An int is a whole number with no size limit. A float has a decimal point.
Python code
print(7 + 3)
print(7 - 3)
print(7 * 3)
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(2 ** 10)
Output
10 4 21 3.5 3 1 1024
| Operator | Meaning |
|---|---|
/ |
Division. Always gives a float |
// |
Floor division: divides and drops the decimals |
% |
Remainder |
** |
Power |
7 / 2 is 3.5, but 8 / 2 is 4.0, a float. Use // when you want a whole number.
Remainder tricks
Python code
number = 14
print("Even" if number % 2 == 0 else "Odd")
minutes = 135
print(minutes // 60, "hours and", minutes % 60, "minutes")
Output
Even 2 hours and 15 minutes
Rounding
Python code
print(round(3.14159, 2))
print(round(7.5))
print(round(8.5))
print(int(4.9))
Output
3.14 8 8 4
Notice that round(7.5) and round(8.5) both give 8. Python rounds exact halves to the nearest even number. If you need “always round half up”, use the decimal module.
Built-in math helpers
Python code
print(abs(-7))
print(max(3, 9, 5))
print(min(3, 9, 5))
print(sum([1, 2, 3, 4]))
print(pow(2, 3))
print(divmod(17, 5))
Output
7 9 3 10 8 (3, 2)
divmod(17, 5) returns the quotient and remainder together.
The math module
More functions live in the math module. You load it with import.
Python code
import math
print(math.sqrt(81))
print(math.ceil(4.1))
print(math.floor(4.9))
print(round(math.pi, 4))
Output
9.0 5 4 3.1416
Big numbers and readability
Python integers can be as large as memory allows. Use underscores to make long numbers readable.
Python code
population = 8_000_000_000
print(population)
print(2 ** 100)
Output
8000000000 1267650600228229401496703205376
Float accuracy
Computers store decimals in binary, so some sums are very slightly off, in every language.
Python code
import math
from decimal import Decimal
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)
print(math.isclose(0.1 + 0.2, 0.3))
print(Decimal("0.1") + Decimal("0.2"))
Output
0.30000000000000004 False True 0.3
Compare floats with math.isclose(). For money, use Decimal with text values, or store whole cents as integers.
Random numbers
Python code
import random
print(random.randint(1, 6))
print(random.choice(["red", "green", "blue"]))
Output (different each time)
4 green
random is fine for games, but it is not secure. For passwords or tokens, use the secrets module.
Common mistakes
- Expecting
/to give a whole number. - Comparing floats with
==. - Using
randomfor security. - Dividing by zero, which raises
ZeroDivisionError. - Doing maths on strings from
input()without converting.
Exercise
A price is 19.99 and the tax rate is 0.2. Print the price including tax, rounded to 2 decimals.
Show solution
price = 19.99
tax_rate = 0.2
total = price * (1 + tax_rate)
print(round(total, 2))
Output: 23.99
Knowledge check
1. What does 17 // 5 give?
3.
2. Which module gives you sqrt()?
math
3. Which module should you use for secure random values?
secrets
Summary
- Python has int and float, with
/(float),//(floor),%, and**. - Use
round,abs,max,min, and themathmodule. - Beware of float inexactness, and use
secretsinstead ofrandomfor security.