# Numbers and Math

Python has whole numbers (int) and decimal numbers (float), seven math operators that follow the order of operations, and round() for the tiny errors decimals carry, which is how a program gets money right to the cent.

Novice · Python · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Do math with int and float values using + - \* / // % and \*\*, and work out the result in the order Python does
- Round a money amount to cents with round(), and explain why a float sum can come out a tiny bit off

## Key takeaways

- An int is a whole number, such as 3, and a float has a decimal point, such as 6.5. Mixing them, or dividing with /, gives a float.
- // (floor division) gives the whole times one number goes into another, and % (remainder) what's left over. ** is read "to the power of".
- Python does ** first, then * / // and %, then + and -, and anything in parentheses before all of them.
- A float can't hold most decimals exactly, so 0.1 + 0.2 shows 0.30000000000000004. Round money to cents with round(amount, 2).

Most programs do some math: a receipt's total, the tax on it, an average grade, how many boxes an order fills. Python's math looks like a calculator's, with a few surprises: two kinds of numbers, operators a calculator doesn't have, and decimals that come out a hair off. Knowing them is how you get a total right to the cent.

This lesson's example is the snack bar at Starlight Cinema: popcorn is $6.50, a soda is $3.75, and there's 7% sales tax.

## Two kinds of numbers

Here's a puzzle. Why does one of these print a whole number, and the other two a decimal point?

```python
popcorns = 3
popcorn_price = 6.50
print(popcorns * 2)
print(popcorn_price * 2)
print(6 / 2)
```

```text
6
13.0
3.0
```

Python has two kinds of numbers. An **int** (short for *integer*, read "int") is a whole number: `3`, `0`, `-2`. A **float** (short for *floating-point number*) has a decimal point: `6.50`, `3.0`. Python writes a float without the zeros it doesn't need, so `6.50` shows as `6.5` and `13.00` as `13.0`.

Two rules decide which kind you get. Mixing an int and a float gives a float, so `6.50 * 2` is `13.0`. And dividing with `/` always gives a float, even when it comes out even, so `6 / 2` is `3.0`.

## The math operators, read aloud

The popcorn machine makes a batch of 47 cups, and a bag holds 6 cups. How many bags can the snack bar fill? Dividing gives 7.83, but nobody sells 0.83 of a bag. What the snack bar wants is how many *full* bags, and how many cups are left over:

```python
cups = 47
cups_per_bag = 6
print(cups / cups_per_bag)
print(cups // cups_per_bag)
print(cups % cups_per_bag)
```

```text
7.833333333333333
7
5
```

`//` is read "floor division": it divides and keeps only the whole part, 7 full bags. `%` is read "remainder" (or "mod", short for *modulo*): what's left over, 5 cups. Together they answer the question you started with: 7 bags, with 5 cups to spare.

Here are all seven of Python's math operators, the **arithmetic operators**:

| Operator | Read it | Example | Result |
|---|---|---|---|
| `+` | plus | `6.50 + 3.75` | `10.25` |
| `-` | minus | `20 - 13` | `7` |
| `*` | times | `6.50 * 2` | `13.0` |
| `/` | divided by | `7 / 2` | `3.5` |
| `//` | floor division | `47 // 6` | `7` |
| `%` | remainder | `47 % 6` | `5` |
| `**` | to the power of | `2 ** 3` | `8` |

`2 ** 3` is 2 × 2 × 2. Notice that `*` is times: the × from math class isn't on the keyboard.

## Which part goes first

These two lines use the same numbers, and print different answers:

```python
print(6.50 + 3.75 * 2)
print((6.50 + 3.75) * 2)
```

```text
14.0
20.5
```

Python doesn't work left to right. It does `**` first, then `*`, `/`, `//` and `%`, then `+` and `-`, and anything in parentheses before any of them. Your math class calls this the *order of operations* (you may know it as PEMDAS). So the first line is one popcorn and two sodas, 6.50 + 7.50; the second, with parentheses, is two combos of one popcorn and one soda each. When you aren't sure which part goes first, add parentheses: they cost nothing, and they show the next reader what you meant.

## Money, and the 0.1 + 0.2 surprise

Try this in a file, or in the REPL:

```python
print(0.1 + 0.2)
```

```text
0.30000000000000004
```

It isn't a bug in Python. A computer stores a float in binary, and most decimals can't be written exactly in binary, the same way 1/3 can't be written exactly in decimal (0.333… never ends). The tiny difference usually hides, and sometimes it shows, as here, or in the tax on an order of three popcorns and two sodas:

```python
subtotal = 3 * 6.50 + 2 * 3.75
tax = subtotal * 0.07
print(subtotal)
print(tax)
print(round(tax, 2))
print(subtotal + round(tax, 2))
```

```text
27.0
1.8900000000000001
1.89
28.89
```

`round(tax, 2)` is read "round tax to 2 places": it gives the nearest number with two decimal places, which for money means the nearest cent. Round the tax once, then add it, and the total comes out right. Leave out the second number, as in `round(7.6)`, and `round()` gives the nearest whole number, an int: `8`.

One thing `round()` can't do is add zeros: 13.0 stays `13.0`, not `13.00`. Showing money with two decimal places is a job for text, and *Strings and f-strings* shows how.

## Do it: bag the popcorn and price the batch

At closing time, the snack bar bags the last batch of popcorn and works out what the full bags will sell for.

- `cups`: how many cups the batch made, `50`.
- `cups_per_bag`: how many cups fill one bag, `6`.
- `popcorn_price`: the price of one bag before tax, `6.50`.

**Your job:** below the starter's three lines, make three variables, then print them:

- `full_bags`: how many bags the cups fill all the way (whole bags only);
- `cups_left_over`: the cups left after filling those bags;
- `sales`: what the full bags sell for at `popcorn_price` each, with 7% tax added, rounded to cents with `round()`;
- print `full_bags`, then `cups_left_over`, then `sales`, each on its own line.

Worked through: 50 cups at 6 a bag fill 8 full bags (48 cups), with 2 cups left over. Eight bags at 6.50 is 52.0, and 7% tax adds 3.64, so the sales are 55.64. The program prints `8`, `2` and `55.64`, one per line.

Practiced live on the lesson page: write the Python right there and check it.

## Check yourself

2 questions to try on the lesson page.

## So what

Two kinds of numbers, seven operators and one `round()` are all the math a receipt needs, and knowing where decimals go wrong is what makes yours right to the cent.

## Key terms

- [Integer](https://www.ask-codey.com/glossary/integer)
- [Float](https://www.ask-codey.com/glossary/floats)
- [Arithmetic Operators](https://www.ask-codey.com/glossary/arithmetic-operators)
- [Floor Division](https://www.ask-codey.com/glossary/floor-division)
- [Modulo](https://www.ask-codey.com/glossary/modulo)
- [round()](https://www.ask-codey.com/glossary/round)

## Related

- [Variables: Giving a Value a Name](https://www.ask-codey.com/lessons/python-variables)
- [Strings and f-strings](https://www.ask-codey.com/lessons/python-strings-and-f-strings)

Source: https://www.ask-codey.com/lessons/python-numbers-and-math
