Vinay Garg Assistant Professor

Lesson 7: All the arithmetic operators

Seven operators do all the arithmetic in Python: add, subtract, multiply, divide, floor divide, remainder and power. One short program shows each one, plus the order Python does them in.

A code window showing print(7 + 3 * 2) and print(7 // 2, 7 % 2)

In short

  • Python has seven arithmetic operators: + - * / // % **.
  • `/` always gives a float, even for 8 / 2. `//` gives a whole number and throws the decimal part away.
  • `%` gives the remainder, which is the basis of the even/odd and prime checks later in this series.
  • Multiplication and division happen before addition and subtraction; brackets change that.

7

arithmetic operators in Python

Python docs

17 ** 5

1419857 — power is a single operator

this program

What this program shows

An operator is the symbol between two values that tells Python what to do with them. The values on either side are called operands. Arithmetic operators work on numbers, and the answer is a number.

The seven operators

OperatorNameExampleAnswer
+Addition7 + 310
-Subtraction7 - 34
*Multiplication7 * 321
/Division7 / 23.5
//Floor division7 // 23
%Remainder (modulo)7 % 21
**Power7 ** 249

The program

# arithmetic.py — every arithmetic operator, with its answer in a comment

a = 17
b = 5

print("a + b  =", a + b)     # 22
print("a - b  =", a - b)     # 12
print("a * b  =", a * b)     # 85
print("a / b  =", a / b)     # 3.4   division gives a float
print("a // b =", a // b)    # 3     floor division drops the decimal part
print("a % b  =", a % b)     # 2     what is left over
print("a ** b =", a ** b)    # 1419857

print(2 + 3 * 4)      # 14, because * runs before +
print((2 + 3) * 4)    # 20, brackets first
print(7 / 2)          # 3.5
print(-7 // 2)        # -4, floor goes down, not towards zero

How it works, step by step

  1. a = 17 and b = 5 give us two numbers to work with.
  2. The first four prints add, subtract, multiply and divide. Notice that a / b prints 3.4 — the slash always returns a float.
  3. a // b asks how many whole times 5 fits into 17. It does not round; it cuts the decimal part off.
  4. a % b asks what remains after those whole 5s: 17 = 5 + 5 + 5 + 2, so the answer is 2.
  5. a ** b means 17 × 17 × 17 × 17 × 17.
  6. The last four prints show the order. Multiplication happens first, so 2 + 3 * 4 is 2 + 12. Brackets change the order, which is why (2 + 3) * 4 is 20.

What it prints

a + b  = 22
a - b  = 12
a * b  = 85
a / b  = 3.4
a // b = 3
a % b  = 2
a ** b = 1419857
14
20
3.5
-4

Common mistakes

  • Expecting 7 / 2 to give 3. It gives 3.5. Use // when you want the whole part.
  • Thinking % means percentage. It means remainder.
  • Forgetting that + also joins text. "2" + "3" gives "23", not 5.
  • Assuming // rounds to the nearest number. It always goes down: -7 // 2 is -4.

Practice

Try these before moving on. Each one uses only what this lesson covered.

  1. Take two numbers, 9 and 4, and print their sum, difference, product and quotient.
  2. Print the remainder when 100 is divided by 7, then explain the answer in a comment.
  3. Use `**` to print 2 to the power 10.
  4. Work out the average of three numbers 45, 67 and 89 using `/` and brackets.
  5. Predict the answer to `10 + 2 * 3 ** 2` before you run it, then check.

Sources

Frequently asked questions

What is the difference between / and // in Python?

/ divides and always returns a float, so 7 / 2 is 3.5. // divides and returns the whole part, so 7 // 2 is 3. Use // when you want a count of whole items.

What does the % operator do?

It gives the remainder after a division. 17 % 5 is 2 because 5 fits into 17 three times with 2 left over. It is used for even/odd tests, prime tests and wrapping numbers into a range.

Which operator runs first in Python?

Power (**), then multiplication, division, floor division and remainder, then addition and subtraction. Comparisons come after that. Brackets override everything, so use them when you are unsure.

Can I use arithmetic operators on text?

+ joins two strings and * repeats a string, as in "ab" * 2 giving "abab". Every other arithmetic operator only works on numbers.

Vinay Garg

Vinay Garg

Assistant Professor of Computer Science at Multani Mal Modi College, Patiala. Twenty-five years of teaching web programming, and a habit of building the things I teach about.