Python has two kinds of numbers:
int) is a whole number: 3, 42, -7.float) is a number with a decimal point:
3.5, 0.25, -1.75. The name comes from "floating point",
which is just how computers store decimal numbers.Python tells them apart by the decimal point. 7 is an integer; 7.0
is a float, even though they represent the same amount. That matters
because division always gives you a float, as you will see below.
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
Add | 4 + 3 |
7 |
- |
Subtract | 4 - 3 |
1 |
* |
Multiply | 4 * 3 |
12 |
/ |
Divide | 7 / 2 |
3.5 |
Try them together:
print(4 + 3)
print(10 - 4)
print(6 * 7)
print(7 / 2)
Output:
7
6
42
3.5
Note that 7 / 2 gives 3.5, not 3. Division in Python always
produces a float, even when the answer looks whole: 8 / 2 gives
4.0, not 4.
Sometimes you want the whole-number part of a division, or the leftover. Python has two operators for that:
print(7 // 2)
print(7 % 2)
print(8 % 2)
Output:
3
1
0
7 // 2 is 3 (how many whole 2s fit in 7), 7 % 2 is 1 (what is
left over), and 8 % 2 is 0 (nothing left over). The // operator
is called floor division, and % is the modulo (remainder)
operator. Modulo is surprisingly useful: x % 2 tells you whether a
number is even (remainder 0) or odd (remainder 1), which you will use
in the guessing game and beyond.
** raises a number to a power:
print(2 ** 3)
print(5 ** 2)
Output:
8
25
When several operators appear in one line, Python follows the same order you learned in maths — powers first, then multiplication and division, then addition and subtraction. Brackets override everything:
print(2 + 3 * 4)
print((2 + 3) * 4)
Output:
14
20
The first line is 2 + 12 = 14, because 3 * 4 happens first. The
second is 5 * 4 = 20, because the brackets force the addition first.
When in doubt, add brackets — they make your intention obvious to
anyone reading the code, including future you.
This is the most common arithmetic surprise for beginners:
print("5" + "3")
print(5 + 3)
Output:
53
8
"5" + "3" joins two pieces of text into "53" — that is called
concatenation, and you will meet it properly in Lesson 6. 5 + 3
adds two numbers and gives 8. Same symbols, different result,
because quotes change what a value is. If your arithmetic ever gives a
strange-looking answer, check whether one of your values is secretly
text in quotes.
15 + 27, 100 - 38, 9 * 8, 20 / 4.
Write down each result and note which ones are floats.// and % to split 17 cookies between 5 friends: how many
whole cookies each, and how many left over?(10 + 2) * 3 before running it, then check.print("4" * 3) in the shell. What happens, and why do you
think it happens? (Hint: think about what * does to text.)7 / 2 give, and why is it a float?// and % compute? Give the results for 9 // 2 and 9 % 2.2 + 3 * 4 give 14 and not 20?"5" + "3" give 53 while 5 + 3 gives 8?Answer key: 1. An integer is a whole number; a float has a decimal point. 2. 3.5 — division in Python always produces a float. 3. // is floor (whole-number) division, 9 // 2 = 4; % is the remainder, 9 % 2 = 1. 4. Multiplication happens before addition, so it is 2 + 12. 5. The quoted values are text, so + joins them; the unquoted values are numbers, so + adds them.
+, -, *, / work as expected, but / always gives a float.// gives the whole-number part of a division; % gives the remainder.** is power; brackets override the normal order of operations.Next lesson: Strings.
Work through the slides with Helio's narration, or download them:
Open the slidesDownload PowerPointDownload PDF
Run the lesson's example with real Python, right in your browser (Python loads from the internet, so you need to be online).
Download lesson05-practice.pyDownload the solution
# Lesson 5: Numbers and Arithmetic
# WHAT TO DO: Share 17 cookies between 5 friends. // (floor division)
# gives whole cookies each, and % (remainder) gives what is left over.
# The "each" line is already right, but one line is wrong. Fix line 13:
# change the leftover from 0 to 17 % 5.
# HINT: 17 shared between 5 friends leaves 2 cookies over, so a correct
# fix prints "Left over: 2".
# Run with F5 in IDLE. You are done when the last line prints 2.
cookies = 17
friends = 5
each = cookies // friends
leftover = 0 # <-- CHANGE THIS LINE: use 17 % 5 (cookies % friends)
print("Each friend gets:")
print(each)
print("Left over:")
print(leftover)
Save the file as lesson05-practice.py and run it with F5 in IDLE. On a Mac, if F5 does nothing, use Run, then Run Module, from the menu.
Five quick questions on this lesson. Take the Lesson 5 quiz
Enroll free in the full course — same 17 lessons, with your progress saved. Free, no payment.