The Four Families

You've met operators one at a time. Here's the full map, in four families:

Arithmetic, + - * / % ** //, produce numbers

Comparison, > < >= <= == !=, produce Booleans

Logical, and or not / && || !, combine Booleans

Assignment, = += -= *= /=, store values

Compound assignment is the family you haven't met. total += 5 is shorthand for total = total + 5. It's shorter, and it's the standard way to accumulate a running total.

Compound assignment

PYTHON

total = 0
total += 10   # same as total = total + 10
total *= 2    # same as total = total * 2

JAVASCRIPT

let total = 0;
// same as total = total + 10
total += 10;
total *= 2;   // same as total = total * 2
Compare → Identical in both languages. You'll use += constantly once loops arrive in Module 12: it's how a running total grows one item at a time.
EXAMPLE
loading editor…
YOUR TURN
Your turn → This is the accumulator pattern, the backbone of every total, every counter, every sum you'll ever write. Add two more donations.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Precedence, What Runs First →
← Back