LambdaLabTM
Computer Science · Class 11 · Expressions & Operators
ExpressionsMaths⏱️ 12 min read

Arithmetic Operators

Python does all its maths with seven operators. You already know four of them from your maths class. The other three — **, % and // — are new. Two of them give answers that surprise most students at first, so read those parts carefully.

1The seven operators

+
Addition
7 + 29
-
Subtraction
7 - 25
*
Multiplication (asterisk)
7 * 214
**
Exponentiation (power)
7 ** 249
/
Division
10 / 25.0
//
Floor division (quotient)
10 // 42
%
Modulus (remainder)
10 % 42

The three blue operators work just as they do in maths. The purple one means "to the power of". The three amber ones are the ones you must study carefully, so most of this lesson is about them.

2Division always gives a float

In Python, the / operator always gives a float — a number with a decimal point. It does this even when the division is exact and the answer looks like a whole number.

Python prompt — interactive mode
>>> print(10 / 4)
2.5
>>> print(10 / 2) # exact division — but the answer is still a float
5.0
>>> print(20 / 5) # again: 4.0, and not 4
4.0
Do not lose marks here
10 / 2 is 5.0, and not 5. If a question asks for the output of print(20 / 5), the correct answer is 4.0. Students forget the .0 very often. Do not be one of them.

3One float makes the whole answer a float

This is an important rule for all arithmetic in Python. If any operand in the expression is a float, then the final answer is also a float. Just one operand with a decimal point is enough to make the whole answer a float.

Python prompt — interactive mode
>>> print(2 + 3) # both int → int
5
>>> print(2 + 3.0) # one float → float
5.0
>>> print(2 * 3 + 1.5) # one float anywhere → float
7.5
Key Takeaway
int with int gives an int. But if even one operand is a float, the answer is a float. And / always gives a float, even when both operands are int.

4** — to the power of

You cannot type a small raised number on a keyboard, so Python uses two asterisks instead. 2 ** 3 means 23, which is 2 × 2 × 2.

Python prompt — interactive mode
>>> print(2 ** 3) # 2 × 2 × 2
8
>>> print(5 ** 2) # 5 squared
25
>>> print(25 ** 0.5) # a square root! (25 to the power of one-half)
5.0

5% — the remainder

When you divide 10 by 4, the quotient is 2 and the remainder is 2. The % operator gives you that remainder. It does not give the quotient. This operator is also called the modulus operator.

Python prompt — interactive mode
>>> print(10 % 4) # 4 divides 10 twice, and the remainder is 2
2
>>> print(20 % 6) # 6 divides 20 three times, and the remainder is 2
2
>>> print(20 % 4) # 4 divides 20 exactly, so the remainder is 0
0
>>> print(7 % 25) # 25 is bigger than 7, so the remainder is 7 itself
7
A useful trick: even or odd
A number is even when it is divided by 2 and the remainder is 0. So the expression n % 2 == 0 means "n is an even number". You will use this again and again when you learn if statements.

6// — the quotient (floor division)

The % operator gives the remainder. The // operator gives the quotient — that is, how many complete times the divisor divides the dividend. With positive numbers, it looks like Python is simply removing the digits after the decimal point:

Python prompt — interactive mode
>>> print(25 / 4) # the exact answer
6.25
>>> print(25 // 4) # only the whole number part
6
>>> print(18 // 7)
2

But "remove the decimal part" is not the correct rule. It gives the wrong answer as soon as a negative number is used. The correct rule is given by the name itself. Floor means Python moves down the number line, to the whole number just below the exact answer.

Python prompt — interactive mode
>>> print(-16 / 3) # the exact answer is about -5.33
-5.333333333333333
>>> print(-16 // 3) # move DOWN from -5.33, so the answer is -6 (not -5)
-6
>>> print(-13 // 3) # -4.33 moves down to -5
-5

Move the sliders below and see this for yourself. The purple mark is the exact answer. The green arrow shows // moving down to the whole number below it. Make the dividend negative, and you will see the answer move away from zero, and not towards it.

Floor division — always move downwards
>>> 25 / 4
6.2500
>>> 25 // 4
6
25 / 4 lies between two whole numbers. // moves down to the whole number below it, which is 6. Now make the dividend negative. That is where most students make a mistake.
↑ ceiling
9
8
7
6
5
6.25
↓ floor
Move down, not towards zero
-16 // 3 is -6, and not -5. Floor division always moves down the number line. For positive numbers, moving down gives the same answer as removing the decimal part. For negative numbers, it does not.

7Try them all

Type your own expressions. Try the three that students get wrong most often: 10 / 2, -16 // 3 and 7 % 25.

Python prompt — interactive mode
# Try each operator. See which answers come back as floats.
>>>
try

8Recap

Key Takeaway
+ - * work as they do in maths. ** means "to the power of". / always gives a float. % gives the remainder. // gives the quotient, by moving down the number line. And if any one operand is a float, the whole answer is a float.
Quick Check

What does print(10 / 2) show?

Quick Check

What does print(-16 // 3) show?

Quick Check

What does print(7 % 25) show?

Quick Check

What does print(2 + 3.0) show?