Plenty of π
Module 3: Operators and User Interaction
Compound Assignments

Compound assignment operators are shorthand for performing an operation and then assigning the result back to the original variable.

  • += (Addition assignment): x += y is equivalent to x = x + y
  • -= (Subtraction assignment): x -= y is equivalent to x = x - y
  • *= (Multiplication assignment): x *= y is equivalent to x = x * y
  • /= (Division assignment): x /= y is equivalent to x = x / y
  • %= (Modulus assignment): x %= y is equivalent to x = x % y
  • **= (Exponentiation assignment): x **= y is equivalent to x = x ** y
let score = 100;
score += 50; // score is now 150

let price = 20;
price *= 0.9; // price is now 18 (10% discount)

let counter = 5;
counter %= 3; // counter is now 2 (remainder of 5/3)

These operators make code more concise and often easier to read.