Python Assignment Operators (=, +=, *=, etc.)
What Are Assignment Operators?
An assignment operator is a symbol that assigns a value to a variable. It combines a math or bitwise operation with an assignment in one step. This makes your code shorter and easier to read.
For example, count += 1 is a simpler way of writing count = count + 1.
Want to master Python? This lesson is part of our Free Python course covering operators, loops, functions, and more.
Explore this free Python course →The Simple Assignment Operator (=)
The most basic assignment operator is the equals sign (=). It takes the value from the right side and assigns it to the variable on the left side. This operator is the foundation for all other assignment operators in Python.
This code assigns the number 10 to the variable score:
score = 10
print(score) # Output: 10
Your output will appear here...
Compound Assignment Operators
Python offers several compound operators that combine an arithmetic operation with assignment. They give you a shorthand for updating a variable's value.
Here are the most common compound operators:
+=(Addition Assignment)-=(Subtraction Assignment)*=(Multiplication Assignment)/=(Division Assignment)%=(Modulus Assignment)**=(Exponentiation Assignment)//=(Floor Division Assignment)
The Addition Assignment Operator (+=)
The += operator adds a value to a variable and stores the result back in it. It's a shorthand for variable = variable + value. This works great for incrementing numbers or adding to sequences like strings, lists, and tuples.
# For numbers
count = 5
count += 2
print(count)
# For strings
message = "Hello"
message += ", World!"
print(message)
Your output will appear here...
The Subtraction Assignment Operator (-=)
The -= operator subtracts a value from a variable and saves the result. It's a compact way to decrease a variable's value.
health = 100
damage = 15
health -= damage # Equivalent to health = health - damage
print(health)
Your output will appear here...
The Multiplication Assignment Operator (*=)
The *= operator multiplies a variable by a value and updates the variable with the result. You can also use it to repeat sequences like strings.
score = 10
score *= 2
print(score)
divider = "-"
divider *= 10
print(divider)
Your output will appear here...
The Division Assignment Operator (/=)
The /= operator divides a variable by a value and assigns the result. The result is always a decimal number (float).
total_cost = 50.0
items = 4
total_cost /= items
print(total_cost)
Your output will appear here...
The Modulus Assignment Operator (%=)
The %= operator performs a modulus operation and assigns the remainder to the variable. It's useful for tasks involving cycles or checking if numbers divide evenly.
number = 17
number %= 5
print(number)
Your output will appear here...
The Exponentiation Assignment Operator (**=)
The **= operator raises a variable to the power of a value and assigns the result back to the variable.
base = 2
base **= 3
print(base)
Your output will appear here...
The Floor Division Assignment Operator (//=)
The //= operator performs floor division and assigns the whole number result to the variable. It throws away any decimal part.
value = 15
value //= 4
print(value)
Your output will appear here...
You're making great progress! Practice Python challenges with real world exercises?
Take Python ExercisesBitwise Assignment Operators
Python also provides assignment operators for bitwise operations. These work with binary representations of numbers and are commonly used in low-level programming, flag management, and performance optimization.
The Bitwise AND Assignment Operator (&=)
The &= operator performs a bitwise AND operation and assigns the result to the variable. It compares each bit of two numbers and returns 1 only if both bits are 1.
x = 12 # Binary: 1100
x &= 10 # Binary: 1010
print(x) # Output: 8
Your output will appear here...
The Bitwise OR Assignment Operator (|=)
The |= operator performs a bitwise OR operation and assigns the result. It returns 1 if at least one of the corresponding bits is 1.
x = 12 # Binary: 1100
x |= 10 # Binary: 1010
print(x) # Output: 14
Your output will appear here...
The Bitwise XOR Assignment Operator (^=)
The ^= operator performs a bitwise XOR (exclusive OR) operation and assigns the result. It returns 1 if the bits are different, and 0 if they're the same.
x = 12 # Binary: 1100
x ^= 10 # Binary: 1010
print(x) # Output: 6
Your output will appear here...
The Right Shift Assignment Operator (>>=)
The >>= operator shifts the bits of a number to the right by a specified number of positions and assigns the result. This effectively divides the number by 2 for each shift.
x = 16 # Binary: 10000
x >>= 2 # Equivalent to x = x >> 2
print(x)
Your output will appear here...
The Left Shift Assignment Operator (<<=)
The <<= operator shifts the bits of a number to the left by a specified number of positions and assigns the result. This effectively multiplies the number by 2 for each shift.
x = 4 # Binary: 100
x <<= 2 # Equivalent to x = x << 2
print(x)
Your output will appear here...
Why Use Compound Operators?
Compound operators offer several benefits:
- Better readability: The syntax makes it clearer that you're modifying a variable.
- Less redundancy: You only type the variable name once, which reduces typos.
- Cleaner code: They make your code more concise without sacrificing clarity.
Final Challenge: The Bank Account
Simulate a bank account transaction log using compound operators.
Your Mission:
- Start with a
balanceof 1000. - Add a deposit of 500 using
+=. - Subtract a withdrawal of 200 using
-=. - Apply a 5% interest rate (multiply by 1.05) using
*=. - Print the final balance.
balance = 1000
balance += 500 # Add deposit
balance -= 200 # Subtract withdrawal
balance *= 1.05 # Apply interest
print(balance)
Your output will appear here...
Lesson Completed
You have successfully learned about Python Assignment Operators, how to use compound assignments, and how they make code cleaner.
Full Python Course
Master Python with 11+ hours of content, 50+ exercises, and real-world projects.
Enroll Now