curriculum/challenges/english/blocks/lecture-numbers-and-mathematical-operations/6839b3295323f563efc68f5c.md
Augmented assignment combines a binary operation with an assignment in one step. It takes a variable, applies an operation to it with another value, and stores the result back into the same variable.
If you're familiar with a language like JavaScript, you've probably heard of the addition assignment operator (+=) or subtraction assignment (-=), and others. Those exist in Python, too. The only difference is that they're referred to as augmented assignments.
The basic syntax of an augmented assignment looks like this:
variable <operator>= value
Which is a more efficient way of doing this:
variable = variable <operator> value
For example, here's an example of using augmented assignment to add 5 to an existing variable:
my_var = 10
my_var += 5
print(my_var) # 15
And here is the same thing, but without augmented assignment:
my_var = 10
my_var = my_var + 5
print(my_var) # 15
The advantage of augmented assignment is that it provides a concise and readable way to update a variable value without repeating the variable name. In turn, this reduces redundancy and potential errors that might arise from a typo or something similar.
Every operator can use an augmented assignment. We've looked at the addition assignment operator (+=), so let's look at others.
-=) subtracts the right operand from the left variable and stores the difference in the left variable:count = 14
count -= 3
print(count) # 11
*=) multiplies the left variable by the right operand and stores the product back in the left variable:product = 65
product *= 7
print(product) # 455
/=) divides the left variable by the right and stores the result back in the left variable:price = 100
price /= 4
print(price) # 25.0
//=) floor‑divides the left variable by the right and stores the result back in the left variable:total_pages = 23
total_pages //= 5
print(total_pages) # 4
%=) computes the remainder of the left variable divided by the right and stores it back in the left variable:bits = 35
bits %= 2
print(bits) # 1
**=) raises the left variable to the power of the right and stores the result back in the left variable:power = 2
power **= 3
print(power) # 8
You can use some augmented assignment operators with strings, too. For example, the addition assignment operator makes it easy to concatenate strings:
greet = 'Hello'
greet += ' World'
print(greet) # Hello World
And the multiplication assignment operator can be used to repeat a string:
greet = 'Hello'
greet *= 3
print(greet) # HelloHelloHello
Other augmented assignments throw a TypeError when you use them with strings:
greet = 'Hello'
greet -= ' World'
print(greet) # TypeError: unsupported operand type(s) for -=: 'str' and 'str'
greet = 'Hello'
greet /= 'World'
print(greet) # TypeError: unsupported operand type(s) for /=: 'str' and 'str'
If you're wondering if increment and decrement operators (++ and --) work in Python, they don't. That's because Python deliberately avoids C-style increment and decrement shortcuts in order to keep the language clear and explicit.
Instead of x++, you can simply write x += 1, which makes it obvious that you're incrementing the value of x by 1.
Writing ++x in Python just applies the unary plus twice, and does not increment anything:
my_var = 5
print(+my_var) # 5
print(++my_var) # 5
print(+++my_var) # 5
my_var += 1
print(my_var) # 6
What is the main advantage of using augmented assignment operators?
They automatically convert data types.
Think about how augmented assignments help avoid redundancy and reduce typing mistakes.
They prevent all possible syntax errors.
Think about how augmented assignments help avoid redundancy and reduce typing mistakes.
They provide a concise way to update variables without repeating the variable name.
They make mathematical operations faster.
Think about how augmented assignments help avoid redundancy and reduce typing mistakes.
3
What happens when you use the increment (++) and decrement (--) operators in Python?
They work exactly like in C/C++.
Think about how Python avoids C-style increment and decrement operators.
They cause a syntax error and crash the program.
Think about how Python avoids C-style increment and decrement operators.
They are interpreted as double unary operators with no actual increment/decrement.
They only work in Python 2 but were removed in Python 3.
Think about how Python avoids C-style increment and decrement operators.
3
Which augmented assignment operator raises a variable to a power and stores the result back to that variable?
^=
Think about the operator that uses the same symbol as exponentiation operator.
**=
*=
Think about the operator that uses the same symbol as exponentiation operator.
^^=
Think about the operator that uses the same symbol as exponentiation operator.
2