Numbers

Integers, floats, and arithmetic

Beginner 10 min

In this lesson

Python works with two main kinds of numbers: integers (whole numbers) and floats (numbers with a decimal point). It also makes an excellent calculator, and you’ll learn every everyday operator plus a famous gotcha about decimals.

Explain it like I’m 5

Whole numbers are ints; numbers with a decimal point are floats. Python does the arithmetic exactly the way you’d expect — with two kinds of division worth knowing.

int and float

int is a whole number (7); float has a decimal point (7.0). Any operation involving a float usually produces a float. You rarely have to think about it, but it explains why 4 / 2 gives 2.0 rather than 2.

Example
print(4 / 2)
print(type(4 / 2))
print(5 + 2.0)
Output
2.0
<class 'float'>
7.0
A float anywhere makes the result a float.

The operators

  • + - * — add, subtract, multiply
  • /true division, always a float: 7 / 2 == 3.5
  • //floor division, drops the remainder: 7 // 2 == 3
  • %modulo, the remainder: 7 % 2 == 1
  • ** — power: 2 ** 3 == 8

% is more useful than it looks: n % 2 == 0 tests for even numbers, and modulo is how you wrap values around (clocks, cycles).

Example
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(2 ** 10)
Output
3.5
3
1
1024
The four division-related operators.

A pizza is cut into 8 slices and 3 friends share it equally. Print how many whole slices each gets (floor division) and how many are left over (modulo).

slices = 8
friends = 3
print(slices // friends)
print(slices % friends)

Rounding and the floating-point surprise

round(x, n) rounds to n decimal places. But beware: 0.1 + 0.2 prints 0.30000000000000004. That’s not a Python bug — computers store decimals in binary, which can’t represent some fractions exactly. For money, round when displaying, or use the decimal module for exactness.

Example
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)
print(round(0.1 + 0.2, 2))
Output
0.30000000000000004
False
0.3
The famous floating-point surprise — and the fix.
Example
total = 19.99
qty = 3
grand = total * qty
print(grand)
print(round(grand, 2))
Output
59.97
59.97
Multiplying money and rounding for display.

Handy built-in number tools

Python includes small helpers you’ll use all the time, with no import needed: abs(x) gives the distance from zero (so abs(-7) is 7), min() and max() pick the smallest or largest of several values (or of a list), and divmod(a, b) hands back the whole-number quotient and the remainder together as a pair.

For heavier maths — square roots, π, trigonometry — Python’s standard-library math module has it covered. Learn more about importing modules here.

Example
print(abs(-7))
print(min(4, 9, 2))
print(max([3, 8, 5]))
print(divmod(17, 5))
Output
7
2
8
(3, 2)
abs, min/max, and divmod in action.

Common mistake: Expecting / to give a whole number

Why it happens:

In Python 3, / is always true division and returns a float, so 10 / 2 is 5.0, not 5.

How to fix it:

Use // when you want an integer result, or wrap with int(...) if appropriate.

print(10 // 2)  # 5
print(10 / 2)   # 5.0

What is 17 % 5?

Which operator gives the remainder of a division?

Mini exercise (medium)

You have 100 minutes. Print how many whole hours that is and how many minutes are left over. Use // for whole hours and % for the remainder.

Do it here. Finish the code below and press Run to try your answer right in the browser — no setup needed.

total_minutes = 100
hours = 0       # TODO: whole hours — use //
minutes = 0     # TODO: leftover minutes — use %
print(hours, "hours", minutes, "minutes")

What to learn next

You explored numbers: int versus float, the arithmetic operators, true versus floor division, modulo, rounding, and turning text into numbers. You used // and % together to split 100 minutes into 1 hour and 40 minutes.

Numbers and text describe data; next you need values that describe truth, so a program can make decisions. Booleans and None covers True and False, how comparisons produce them, truthiness, and the special None value. When you’re ready, continue to the next lesson.