➕ Operators & Expressions
Module 3 · Topic 5

Assignment & Augmented Operators

Assign values to variables with = and compound operators (+=, -=, *=, etc.) for concise and readable code.

Theory

Assignment Operators:

| Operator | Example | Equivalent |
|----------|---------|------------|
| = | x = 5 | Assign 5 to x |
| += | x += 3 | x = x + 3 |
| -= | x -= 3 | x = x - 3 |
| *= | x *= 3 | x = x * 3 |
| /= | x /= 3 | x = x / 3 |
| //= | x //= 3 | x = x // 3 |
| %= | x %= 3 | x = x % 3 |
| **= | x **= 3 | x = x ** 3 |
| &= | x &= 3 | x = x & 3 |
| |= | x |= 3 | x = x | 3 |
| ^= | x ^= 3 | x = x ^ 3 |
| <<= | x <<= 2 | x = x << 2 |
| >>= | x >>= 2 | x = x >> 2 |

Walrus Operator := (Python 3.8+):
Assign and return a value in a single expression:

if (n := len(data)) > 10:  
    print(f"Too long: {n} chars")  
Syntax
x = 10      # Simple assignment
x += 5      # Add and assign (x = x + 5)
x -= 3      # Subtract and assign
x *= 2      # Multiply and assign

# Walrus operator (Python 3.8+)
if (n := len(items)) > 0:
    print(f"{n} items")