Code Examples
Example 1
The basic assignment operator (=) assigns the value on the right to the variable on the left. Python also supports multiple assignments in a single statement, which is useful for assigning several values at once.
# Assigning a value to a variable
x = 10
print(x) # Output: 10
# Assigning one variable's value to another
first_name = "Alex"
user_name = first_name
print(user_name) # Output: Alex
# Multiple assignments on one line
a, b, c = 5, 10, 15
print(a) # Output: 5
print(b) # Output: 10
print(c) # Output: 15
Example 2
The += operator adds the value on the right to the variable on the left and assigns the result back to the variable. It's a shorter way to add to an existing value without repeating the variable name.
# Regular addition and assignment
score = 0
score = score + 10
print(score) # Output: 10
# Using addition assignment instead
score = 0
score += 10 # Same as: score = score + 10
print(score) # Output: 10
# Adding to a running total
total = 0
total += 5
print(total) # Output: 5
total += 7
print(total) # Output: 12
total += 3
print(total) # Output: 15
Example 3
The -= operator subtracts the value on the right from the variable on the left and assigns the result back to the variable. It's commonly used for counters, decreasing values, or countdowns.
# Regular subtraction and assignment
lives = 3
lives = lives - 1
print(lives) # Output: 2
# Using subtraction assignment
lives = 3
lives -= 1 # Same as: lives = lives - 1
print(lives) # Output: 2
# Countdown example
countdown = 10
print(countdown) # Output: 10
countdown -= 3
print(countdown) # Output: 7
countdown -= 2
print(countdown) # Output: 5
Example 4
The *= operator multiplies the variable by the value on the right and assigns the result back to the variable. It's useful for scaling values or calculating repeated growth.
# Regular multiplication and assignment
value = 5
value = value * 2
print(value) # Output: 10
# Using multiplication assignment
value = 5
value *= 2 # Same as: value = value * 2
print(value) # Output: 10
# Doubling a value multiple times
population = 1000
print(population) # Output: 1000
population *= 2 # Population doubles
print(population) # Output: 2000
population *= 2 # Population doubles again
print(population) # Output: 4000
Example 5
The /= operator divides the variable by the value on the right and assigns the result back to the variable. Remember that in Python 3, division always returns a float.
# Regular division and assignment
money = 100
money = money / 4
print(money) # Output: 25.0
# Using division assignment
money = 100
money /= 4 # Same as: money = money / 4
print(money) # Output: 25.0
# Splitting something repeatedly
pizza_slices = 8
print(pizza_slices) # Output: 8
pizza_slices /= 2 # Split each slice in half
print(pizza_slices) # Output: 4.0
pizza_slices /= 2 # Split again
print(pizza_slices) # Output: 2.0
Example 6
The //= operator performs floor division (divides and rounds down to the nearest integer) and assigns the result back to the variable.
# Regular floor division and assignment
points = 20
points = points // 3
print(points) # Output: 6
# Using floor division assignment
points = 20
points //= 3 # Same as: points = points // 3
print(points) # Output: 6
# Repeatedly dividing and rounding down
value = 100
print(value) # Output: 100
value //= 3
print(value) # Output: 33
value //= 2
print(value) # Output: 16
Example 7
The %= operator calculates the remainder after division and assigns it back to the variable. It's particularly useful for creating values that cycle or wrap around.
# Regular modulus and assignment
counter = 10
counter = counter % 3
print(counter) # Output: 1
# Using modulus assignment
counter = 10
counter %= 3 # Same as: counter = counter % 3
print(counter) # Output: 1
# Creating a cycling counter
position = 0
print(position) # Output: 0
position += 1
position %= 3 # Keeps position in range 0-2
print(position) # Output: 1
position += 1
position %= 3
print(position) # Output: 2
position += 1
position %= 3
print(position) # Output: 0 (cycles back to beginning)
Example 8
The **= operator raises the variable to the power of the value on the right and assigns the result back to the variable. This can be used for compound interest or exponential growth calculations.
# Regular exponentiation and assignment
base = 2
base = base ** 3
print(base) # Output: 8
# Using exponentiation assignment
base = 2
base **= 3 # Same as: base = base ** 3
print(base) # Output: 8
# Compounding example
investment = 1000 # $1000 initial investment
interest_rate = 1.05 # 5% interest
print(investment) # Output: 1000
investment **= interest_rate # After 1 year
print(investment) # Output: approximately 1050.0