Numbers and Arithmetic

What you’ll learn in this lesson

  • The two kinds of numbers: integers and floats
  • The arithmetic operators: add, subtract, multiply, divide
  • Whole-number division and remainders
  • Powers, and the order Python does operations in
  • What happens when you mix text and numbers

Integers and floats

Python has two kinds of numbers:

  • An integer (called int) is a whole number: 3, 42, -7.
  • A float (called 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.


The basic operators

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.


Whole-number division and remainders

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.


Powers, and the order of operations

** 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.


Text and numbers do not mix

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.


 

Numbers and Arithmetic

Python for Absolute Beginners · Lesson 5

What you’ll learn

  • The two kinds of numbers: integers and floats
  • The arithmetic operators: add, subtract, multiply, divide
  • Whole-number division and remainders
  • Powers, and the order Python does operations in
  • What happens when you mix text and numbers
1 / 14

Integers and floats

  • An integer (called int) is a whole number: 3, 42, −7
  • A 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
2 / 14

The basic operators

  • + adds: 4 + 3 = 7
  • − subtracts: 4 − 3 = 1
  • * multiplies: 4 * 3 = 12
  • / divides: 7 / 2 = 3.5
3 / 14

The basic operators in code

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.

4 / 14

Whole-number division and remainders

  • Sometimes you want the whole-number part of a division, or the leftover
  • // is floor division: how many whole times one number fits in another
  • % is modulo, the remainder operator
  • Modulo is surprisingly useful: x % 2 tells you whether a number is even (remainder 0) or odd (remainder 1)
5 / 14

Floor division and remainders in code

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).

6 / 14

Powers, and the order of operations

  • ** raises a number to a power
  • 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
  • When in doubt, add brackets — they make your intention obvious to anyone reading the code, including future you
7 / 14

Powers and brackets in code

print(2 ** 3)
print(5 ** 2)
print(2 + 3 * 4)
print((2 + 3) * 4)

Output:
8
25
14
20

The third line is 2 + 12 = 14, because 3 * 4 happens first. The fourth is 5 * 4 = 20, because the brackets force the addition first.

8 / 14

Text and numbers do not mix

  • This is the most common arithmetic surprise for beginners
  • “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
9 / 14

Text joins, numbers add

print("5" + "3")
print(5 + 3)

Output:
53
8

The quoted values are text, so + joins them; the unquoted values are numbers, so + adds them.

10 / 14

Hands-on exercise

  • In the shell, work out: 15 + 27, 100 − 38, 9 * 8, 20 / 4 — write down each result and note which ones are floats
  • Use // and % to split 17 cookies between 5 friends: how many whole cookies each, and how many left over?
  • Predict the result of (10 + 2) * 3 before running it, then check
  • Try print("4" * 3) in the shell — what happens, and why do you think it happens?
11 / 14

Key takeaways

  • Integers are whole numbers; floats have decimal points
  • +, −, *, / 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
  • Quotes change a value's kind: text joins, numbers add
12 / 14

Take the quiz

Take the Lesson 5 quiz — 5 quick questions to check what you learned.

13 / 14

Next lesson

Strings: Text, Quotes, and Length

14 / 14

Press play on any slide and the lesson plays on by itself from there. You can still click Next or click a slide to move on.

Download the Lesson 5 slides (PowerPoint, with narration)

Download the Lesson 5 slides (PDF)

Hands-on exercise

  1. In the shell, work out: 15 + 27, 100 - 38, 9 * 8, 20 / 4.
    Write down each result and note which ones are floats.
  2. Use // and % to split 17 cookies between 5 friends: how many
    whole cookies each, and how many left over?
  3. Predict the result of (10 + 2) * 3 before running it, then check.
  4. Try print("4" * 3) in the shell. What happens, and why do you
    think it happens? (Hint: think about what * does to text.)

 

Lesson 05: Numbers and Arithmetic

Python does arithmetic with + , * and ** (to the power of). Change the one number and run again.

Loading Python… this can take a few seconds the first time. Please wait — the Run button will become available when Python is ready.

Try this: Change the number to 5 and press Run again — you should see 8, 10 and 25.

The code that runs



Output

Press Run to see the output here.

Practice file

Save this as lesson05-practice.py and run it with F5 in IDLE.

# 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)

Key takeaways

  • Integers are whole numbers; floats have decimal points.
  • +, -, *, / 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.
  • Quotes change a value's kind: text joins, numbers add.

Next lesson: Strings.


Quick quiz

  1. What is the difference between an integer and a float?
  2. What does 7 / 2 give, and why is it a float?
  3. What do // and % compute? Give the results for 9 // 2 and 9 % 2.
  4. Why does 2 + 3 * 4 give 14 and not 20?
  5. Why does "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.