Since we were introduced to variables in Post #8, we have been using the single equals sign (=
) constantly. This is the assignment operator, and its primary job is to assign a value to a variable. But what happens when we want to modify a variable’s existing value, for example, by adding to it or subtracting from it? There’s a common pattern for this and a handy shortcut that can make your code cleaner.
In this post, we’ll explore Python’s shorthand assignment operators, which provide a more concise way to update variables.
The Standard Pattern: Modifying a Variable
A very common task in programming is to update a variable based on its own current value. Imagine you’re tracking a player’s score in a game. When the player finds a coin, you need to increase their score.
The standard way to write this looks like this:
Python
score = 100
print(f"Initial score: {score}")
# Player finds a coin worth 10 points
score = score + 10
print(f"Score after finding a coin: {score}")
Let’s break down the key line: score = score + 10
. This line tells Python:
- First, evaluate the right side of the equals sign.
- Take the current value of
score
(which is100
). - Add
10
to it, resulting in110
. - Now, assign this new result (
110
) back to thescore
variable.
The old value of score
is replaced with the new one. This pattern works perfectly, but there’s a shorter way to write it.
The Shorthand: Augmented Assignment Operators
This pattern of variable = variable (operator) value
is so common that Python provides a special shorthand for it. These are known as augmented assignment operators.
The Addition Assignment Operator (+=)
The +=
operator combines addition and assignment into one step.
The statement x += y
is the exact same as x = x + y
.
Let’s rewrite our score example using this shorthand:
score = 100
print(f"Initial score: {score}")
# Player finds a coin worth 10 points (shorthand version)
score += 10
print(f"Score after finding a coin: {score}")
The output is the same (110
), but the code is more concise. This operator clearly signals that we are modifying the variable’s value in-place.
The Subtraction Assignment Operator (-=)
As you might guess, the same logic applies to subtraction. The -=
operator is a shorthand for subtracting from a variable’s current value.
The statement x -= y
is the same as x = x - y
.
Let’s use an example of a player’s health in a game.
health = 100
print(f"Player health: {health}")
# Player takes 25 damage
health -= 25
print(f"Player health after taking damage: {health}")
The final health will be 75
.
All the Other Operators
This convenient shortcut isn’t limited to just addition and subtraction. It works for all of Python’s standard arithmetic operators that we learned in Post #12 and Post #13.
Here is a quick reference:
x *= y
is equivalent tox = x * y
x /= y
is equivalent tox = x / y
x //= y
is equivalent tox = x // y
x %= y
is equivalent tox = x % y
x **= y
is equivalent tox = x ** y
What’s Next?
You’ve now learned about augmented assignment operators like +=
and -=
. They are convenient shortcuts for modifying a variable’s value in place, leading to more concise and often more readable code.
This post marks the end of our introduction to the absolute fundamentals of Python. We’ve covered everything from printing and variables to data types and operators. It’s time to put it all together in a final project for this section. In Post #20, we will recap everything we’ve learned in Part 1 and build a practical application: a simple tip calculator.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com