Numbers and Arithmetic

Python for Absolute Beginners · Lesson 5

What you’ll learn

1 / 14

Integers and floats

2 / 14

The basic operators

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

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

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

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

11 / 14

Key takeaways

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.