Python · Stage 2 – Doing Things with Values · Lesson 14 of 50

Math Operators

Use Python symbols to perform one calculation at a time.

total = 4 + 3
Line-by-line explanation
  1. 4 is the first number.
  2. + means add.
  3. 3 is the second number.
  4. The result 7 is stored in total.

Python uses + for addition, - for subtraction, * for multiplication, and / for ordinary division.

pieces = 12 // 5
Line-by-line explanation
  1. 12 is divided by 5.
  2. // means whole-number division.
  3. The result is 2; the leftover part is removed.

% gives the remainder, and ** raises a number to a power. Parentheses make the order of a calculation clear.

result = (2 + 3) * 4
Line-by-line explanation
  1. The parentheses make 2 + 3 happen first.
  2. That result is multiplied by 4.
  3. result stores 20.

Tiny Practice

Calculate the cost of 3 items that each cost 5.

Starter template

cost = CHANGE_ME
print(cost)
Line-by-line explanation
  1. The first line must calculate and store the cost.
  2. The second line displays it.
Hint
  1. Use *.
  2. Multiply 3 by 5.
  3. Do not use quote marks.
Show Solution
cost = 3 * 5
print(cost)
Line-by-line explanation
  1. 3 * 5 calculates 15.
  2. The result is stored in cost.
  3. The output is 15.