Chapter 5: Operators and Expressions

Arithmetic operators

face Josiah Wang

You can combine objects with operators like + and - to form expressions.

Operators and expressions

Expressions are something that can produce a value, for example 5+2 evaluates to 7.

A literal on its own (5) is also an expression.

The arithmetic operators in Python are quite straightforward:

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • // (floor division, or integer part of division)
  • % (modulo, or remainder after division)
  • ** (exponential - raised to the power)

Try the quiz below! Make an informed guess before verifying your answer (no penalty for the wrong guess). If the quiz raises further thoughts and questions, test it out in a Python interactive prompt!

1 2 3 4 5 6 7 8 9 10 11

Question 1

What is the value of the following expression?

7 + 2

9

Question 2

What is the value of the following expression?

7.3 * 2

14.6

Question 3

What is the value of the following expression?

7 / 2

3.5

Question 4

What is the value of the following expression?

7 // 2

3
Explanation:

7 divided by 2 is 3.5, and the floor \lfloor 3.5 \rfloor = 3. You can also think of it as taking the integer portion of 3.5, which is 3.

Question 5

What is the value of the following expression?

7 % 2

1
Explanation:

The remainder after dividing 7 by 2 is 1.

Question 6

What is the value of the following expression?

7 ** 2

49
Explanation:

7^2 = 49

Question 7

What is the value of the following expression?

7

7
Explanation:

A literal on its own is also an expression. So the value is its own value.

Question 8

What is the type of the following literal?

7.3 / 2

float

Question 9

What is the type of the following literal?

6 / 2

float
Explanation:

The division operator always results in a float, even if the output could have been an integer.

Question 10

What is the type of the following literal?

7 // 2

int
Explanation:

Floor division results in an integer.

Question 11

What is the type of the following literal?

7 % 2

int
Explanation:

Modulo division results in an integer.