What are Assignment Operators ?
Assignment operators are symbols used in Python to assign, store, or update values inside a variable. The most common assignment operator is the simple equal sign (=), which takes whatever value is on the right side and stores it inside the variable container on the left side. Python also offers combined symbols called Augmented Assignment Operators (like +=, -=, *=). These act as a fast shorthand, allowing you to perform an arithmetic calculation on a variable and update its value simultaneously in a single line of code.
Python Assignment Operators Table
Assuming we start with a variable x = 10 before each operation :
| Operator | Name | Syntax |
|---|---|---|
| = | Assign | x = 5 |
| += | Add and Assign | x += 3 |
| -= | Subtract and Assign | x -= 2 |
| *= | Multiply and Assign | x *= 4 |
| /= | Divide and Assign | x /= 2 |
| //= | Floor Divide and Assign | x //= 3 |
1. Basic Assignment Operator (=)
The basic assignment operator (=) evaluates the expression or value on the right-hand side and saves it directly into the variable on the left-hand side. Let's look at some examples :
# Assigning a starting integer value to a variable
score = 100
print("The initial score is:", score)
# Output: The initial score is: 100
2. Add and Assign Operator (+=)
The add and assign operator (+=) takes the existing value of the variable, adds the right-hand value to it, and updates the variable with the new total. Let's look at some examples :
# Initializing the score
score = 100
# Adding bonus points to the existing score
score += 15
print("The updated score is:", score)
# Output: The updated score is: 115
3. Subtract and Assign Operator (-=)
The subtract and assign operator (-=) deducts the right-hand value from the variable's existing value and saves the updated lower value back into that same variable. Let's look at some examples :
# Initializing player health
health = 100
# Taking damage from an enemy
health -= 25
print("Remaining health:", health)
# Output: Remaining health: 75
5. Divide and Assign Operator (/=)
The divide and assign operator (/=) divides the variable's current value by the right-hand value and updates the variable with the result. Rule: Just like standard division, this operator always turns the variable's value into a decimal float. Let's look at some examples :
# Storing available funds
savings = 500
# Splitting the savings exactly in half
savings /= 2
print("Your half of the savings is:", savings)
# Output: Your half of the savings is: 250.0
6. Floor Divide and Assign Operator (//=)
The floor divide and assign operator (//=) divides the variable's current value by the right-hand value, strips away any decimal parts, and updates the variable with the remaining whole integer. Let's look at some examples :
# Storing a bulk number of total hours
total_hours = 25
# Converting hours to full days (24 hours per day)
total_hours //= 24
print("Total full days calculated:", total_hours)
# Output: Total full days calculated: 1