Augmented Assignment Operators
In our previous lessons, we learned how to create a variable and assign it a value, like this: score = 500
If we want to increase that value, we have a few options. We could hard-code the math: score = 500 + 20
However, a better way is to use the variable itself to update its own value. Since we already know score equals 500, we can write: score = score + 20
In both cases, if you print(score), you will get 520. Python processes the right side of the equals sign first (500 + 20) and then updates the "box" with the new total.
Introducing the Shortcut
While score = score + 20 works perfectly, programmers are often looking for ways to write cleaner, more efficient code. Instead of typing the variable name twice, we can use an Augmented Assignment Operator.
Instead of: score = score + 20
We can simply write: score += 20
This += symbol tells Python: "Take the current value of the variable and add 20 to it." Itβs shorter, reduces the chance of typos, and is the professional way to update variables in Python.
The Full Toolkit of Shortcuts
The += trick doesn't just work for addition; it works for all the major math operations weβve covered so far. Here is the full list of Augmented Assignment Operators you will use as a programmer:
| Operator | Meaning | Example | Equivalent To |
|---|---|---|---|
| βπ° | Add and Assign | score += 5 | score = score + 5 |
| βπ° | Subtract and Assign | health -= 10 | health = health - 10 |
| βοΈπ° | Multiply and Assign | coins *= 2 | coins = coins * 2 |
| βπ° | Divide and Assign | total /= 2 | total = total / 2 |
Why Professionals Use Them
You might be wondering, "If the long version works, why learn the shortcut?" There are three main reasons:
- Efficiency: It saves you keystrokes.
- Readability: It clearly signals that you are modifying a variable rather than just setting it to a random new value.
- Safety: Imagine you have a long variable name like total_player_inventory_score. If you write total_player_inventory_score = total_player_inventory_score + 1, you might not notice the typo in the second name. With +=, you only type the name once, so you can't make that mistake!
A Quick Note on Division (/=)
Remember from our previous lessons that division in Python always results in a Float (a decimal). So, even if your variable starts as a whole number (Integer), using /= will turn it into a decimal.
Example:
points = 10
points /= 2
print(points) # Result will be 5.0
Test Your Knowledge
Select the correct answer below to see if you've mastered Python math logic.
Loading quiz...
Time to experiment!
Coding Exercises (VS Code) Instructions:
- Create a file named shortcuts.py in your part9 folder.
- Complete the tasks and use # comments to explain your answers.
Exercise 1: Leveling Up Create a variable called xp and set it to 100. The player just completed a quest and earned 75 more XP.
The Task: Use the += operator to update the xp variable. Print the new total.
Exercise 2: The Shopping Cart Create a variable called inventory and set it to 20. A customer just bought 6 items.
The Task: Use the -= operator to decrease the inventory. Print the remaining amount.
Exercise 3: Critical Hit Create a variable called damage and set it to 15. The player finds a "Triple Damage" power-up.
The Task: Use the *= operator to triple the damage. Print the result. In a comment, write what the "long version" of this line would look like (without the shortcut).
Exercise 4: Splitting the Bill Create a variable called total_bill and set it to 120. You want to split it between 4 people.
The Task: Use the /= operator to divide the bill by 4. Print the result. Note in a comment whether the answer is an Integer or a Float.
Exercise 5: The Challenge Create a variable called my_string and set it to "High ".
The Task: Use += to add the word "Score" to the variable. Print my_string. Does the augmented assignment work on text, or does it cause an error?
Don't Forget to commit and Push!