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.