Learn / Beginner / Lesson 4

Numbers

10 min Tested on Python 3.15Updated By
In short

Python has whole numbers (int, like 42) and decimal numbers (float, like 3.14). You calculate with + - * /, use // for whole-number division, % for the remainder and ** for powers. Integers can be as large as you like.

The arithmetic operators

OperatorMeaningExampleResult
+Add7 + 29
-Subtract7 - 25
*Multiply7 * 214
/Divide (always a float)7 / 23.5
//Whole-number division7 // 23
%Remainder7 % 21
**Power7 ** 249
Python
print(7 / 2)
print(8 / 2)
print(7 // 2)
print(7 % 2)
print(2 ** 10)
Output
3.5
4.0
3
1
1024

Notice that 8 / 2 gives 4.0, not 4: the / operator always returns a float. Use // when you want a whole number.

Order of operations

Python
print(2 + 3 * 4)
print((2 + 3) * 4)
Output
14
20

Python follows normal maths rules: powers first, then * / // %, then + -. Brackets make your intention clear.

Big integers

Python
print(2 ** 100)
print(1_000_000 * 3)
Output
1267650600228229401496703205376
3000000

Python integers never overflow. Underscores like 1_000_000 are allowed in numbers just to make them easier to read.

Converting and rounding

Python
text = "25"
print(int(text) + 5)
print(float("2.5") * 2)
print(round(3.14159, 2))
print(int(9.99))
Output
30
5.0
3.14
9

Why 0.1 + 0.2 isn't exactly 0.3

Python
print(0.1 + 0.2)
print(round(0.1 + 0.2, 2))

from decimal import Decimal
print(Decimal("0.1") + Decimal("0.2"))
Output
0.30000000000000004
0.3
0.3

Computers store floats in binary, so some decimals can't be stored exactly. That's true in every language, not just Python. For money, use the decimal module or count in whole cents.

Practice

A trip costs 250 euros and is split between 3 friends. Print each friend's share rounded to 2 decimals, then on a second line the whole euros each pays (no decimals), then the euros left over.

Expected output:
83.33
83
1
Your code

Quick check

1. What is 10 / 5?

2. What is 17 % 5?

3. What does int("7") + 3 give?

Use AI at work

Python is the base of AI. On AIMates you learn to use AI tools well in your daily job.

Visit AIMates →