Working with Numbers

Beginner · 18 min read · ▶ live playground · ✦ checkpoint

Python can split 17 pizza slices among 5 friends three different ways, and all three answers can be useful. Working with numbers means choosing the symbol that matches the question you mean to ask: exact share, whole share, leftover, power, or square root.

Operators make numbers move

An expression is code that produces a value. An operator is a symbol that asks Python to do work with values, like + for adding or * for multiplying.

You already know numbers have types. Now those numbers start doing jobs:

score = 12
bonus = 5
penalty = 3
apples_per_bag = 4
bags = 6
 
print(score + bonus)
print(score - penalty)
print(apples_per_bag * bags)
print(21 / 3)

17
9
24
7.0

The first three lines probably feel natural. The last one matters: / gives 7.0, not 7. That .0 tells you Python made a float, even though the mathematical answer is a whole number.

The everyday arithmetic operators are:

  • + adds
  • - subtracts
  • * multiplies
  • / divides; with the int and float examples here, it returns a float

If a name refers to a number, you can use that name in a numeric expression. Python follows the sticky-label model from lesson 2.1: it looks at the object the name refers to, uses its value, and produces a new object for the result.

Division has more than one answer

The / operator performs true division, which means exact division rather than whole-share division. With the int and float examples here, Python returns a float; complex arithmetic stays complex. The // operator performs floor division, which means Python divides and rounds down on the number line to a whole number. Whole numbers stay unchanged. With positive integers, floor division gives the whole-number share.

The % operator performs modulus, the remainder operation: it tells you what is left after making equal groups.

slices = 17
friends = 5
fair_slices = 18
 
print(slices / friends)
print(slices // friends)
print(slices % friends)
print(fair_slices / 6)
print(type(fair_slices / 6))

3.4
3
2
3.0
<class 'float'>

Read those first three answers as a tiny story:

  • 17 / 5 asks, "What is the exact share?" → 3.4
  • 17 // 5 asks, "How many whole slices per friend?" → 3
  • 17 % 5 asks, "How many slices are left over?" → 2

That pair, // and %, shows up constantly in real programs: pages of search results, minutes and seconds, rows and seats, batches and leftovers. You don't need loops yet to understand the idea. You just need the question: whole groups, or leftover?

Powers and precedence

Exponentiation means raising a number to a power. Python writes it with **, so 2 ** 3 means "2 to the third power."

When an expression has several operators, Python uses operator precedence, the rule order for deciding which operation runs first. It is the same idea as the order of operations from school: powers first, then multiplication and division, then addition and subtraction. Parentheses are your highlighter: they mark the part Python should handle first.

badge_level = 2
combo = 3
 
print(badge_level ** combo)
print(2 + 3 * 4)
print((2 + 3) * 4)
print(20 - 6 / 3)

8
14
20
18.0

The second and third outputs are the lesson. 2 + 3 * 4 is 14 because multiplication happens before addition. (2 + 3) * 4 is 20 because parentheses force the addition first.

Use parentheses when the expression is even a little hard to read. You are not writing for Python only; you are writing for the tired human who reads the code later, including future you.

Try it — tune the scoreboard

Run this, then change the numbers. Try making players bigger, or changing misses to 0. Predict which lines will become floats before you press Run.

python — playgroundlive
⌘/Ctrl + Enter to run

Extra tools from math

Python ships with extra numeric tools in math. A module is an importable bundle of Python names and tools, often but not always a .py file, and an import statement makes a module available in your file; imports and modules get their full story in lesson 8.1.

For now, import math means "let this file use the names inside math." A function call is code that runs a function with parentheses; functions get their full story in lesson 7.1, but you can already read the shape of math.sqrt(...): use the sqrt tool from the math module.

import math
 
stage_width = 9
ticket_price = 4.2
average_score = 4.8
 
print(math.sqrt(stage_width))
print(math.ceil(ticket_price))
print(math.floor(average_score))

3.0
5
4

Square root means the number that multiplies by itself to make the original number, so math.sqrt(9) gives 3.0. Ceiling means round up on the number line to a whole number, with whole numbers staying unchanged, so math.ceil(4.2) gives 5. Floor means round down on the number line to a whole number, with whole numbers staying unchanged, so math.floor(4.8) gives 4.

The math module has more, but these three are enough for now: square roots, rounding up, and rounding down.

Checkpoint

Answer all three to mark this lesson complete

Numbers can now do real work inside your program. Next, you'll connect those results to a human: printing cleaner messages, reading typed input, and converting text like "42" into the number 42.

+50 XP on completion