Arithmetic operators · 1 / 18 Theory

Python arithmetic operators

Python can be used as a very powerful calculator. Arithmetic operators are the symbols that tell it which operation to perform on two values, called operands. You will use them in every exercise of this lesson.

The seven operators

OperatorNameExampleResult
+Addition7 + 29
-Subtraction7 - 25
*Multiplication7 * 214
/Division7 / 23.5
//Floor division7 // 23
%Modulo (remainder)7 % 21
**Power7 ** 249

Try them

print(7 + 2)    # 9
print(7 - 2)    # 5
print(7 * 2)    # 14
print(7 / 2)    # 3.5
print(7 // 2)   # 3
print(7 % 2)    # 1
print(7 ** 2)   # 49

With variables

Operands can be numbers written directly or variables. You can show the result right away or store it in another variable to use it later.

x = 5
square = x ** 2
print(x + square)   # 30

Which type of number does it return?

  • +, -, *, //, % and ** with two integers give an integer.
  • / always gives a decimal number (float), even when the result is exact: 6 / 3 is 2.0.
  • If any operand is a decimal, the result is a decimal too: 2 * 1.5 is 3.0.

Common mistake: using ^ for powers

In other languages and on calculators ^ means power, but in Python it is a different operation (bitwise XOR). To raise a number to a power always use **:

print(2 ** 3)   # 8  -> power
print(2 ^ 3)   # 1  -> not a power

Practice these operators in the exercises about squares, multiplying three factors and the basic calculator.