In this tutorial, we’ll learn about augmented assignment operators in Python. We’ll try to understand these operators with practical Python code examples to better understand their role in Python.
Outline
- Introduction: Augmented Assignment Operators In Python
- Simple Assignment Operator(=)
- Example: Assigning Values
- Augmented Assignment Operators In Python Implementation (Multiple Examples)
- Addition Assignment (+=)
- Subtraction Assignment (-=)
- Multiplication Assignment (*=)
- Division Assignment (/=)
- Modulus Assignment (%=)
- Exponentiation Assignment (**=)
- Floor Division Assignment (//=)
- Simple Python Augmented Operators Program
- Conclusion
Introduction: Augmented Assignment Operators In Python
As you may already know that assignment(=) operator is used to assign values. If we just use assignment operator then it’ll be a simple operator. But when we combine it with an arithmetic operator, then it becomes an augmented assignment operator. Click here if you want to learn Python arithmetic operators in detail.
Augmented operator is very beneficial to make the code look more professional by removing extra code.
In order to understand augmented operators, we first have to understand assignment operator. Let’s now understand it with practical Python code examples.
Simple Assignment Operator(=)
Let’s first go through a simple Python assignment operator. See below example:
Example: Assigning Values
value=50 print(value) value=20 print(value)
Output
50 20
In the above example, we’ve created a variable and have assigned an integer value to it. After that, we’ve given it a value 20. As we can see in the above output that value 50 is replaced by 20. Let’s say we want to add the new value to the existing value. In order to do that, we’ve to follow the below code:
val=50 print(val) val=val+20 print(val)
Output
50 70
We can see that new value has been added to the existing one. Now to make the code look more professional and remove extra code, we’ve to use Python augmented operators. See below code:
val+=20
Output
50 70
Output is same for both methods. This is how we can use augmented operators to remove extra code but with same functionality. Let’s now explore them in detail.
Augmented Assignment Operators In Python Implementation (Multiple Examples)
Below list contains some of the augmented operators.
- Addition Assignment (+=)
- Subtraction Assignment (-=)
- Multiplication Assignment (*=)
- Division Assignment (/=)
- Modulus Assignment (%=)
- Exponentiation Assignment (**=)
- Floor Division Assignment (//=)
Addition Assignment (+=)
val=10 val+=20 # same as val = val+20 print(val)
Output
30
See below example as well:
val='Python ' val+='Programming' print(val)
Output
Python Programming
This is how we can perform addition and string concatenation using addition augmented operator.
Subtraction Assignment (-=)
val=14 val-=6 # val = val-6 print(val)
Output
8
Multiplication Assignment (*=)
value=4 value*=3 #same as value=value*3 print(value)
Output
12
Division Assignment (/=)
a=40 a/=5 # works same as a=a/5 print(a)
Output
8.0
Modulus Assignment (%=)
b=14 b%=3 # works same as b=b%3 print(b)
Output
2
Exponentiation Assignment (**=)
b=2 b**=3 # same as b=b**3 print(b)
Output
8
Floor Division Assignment (//=)
c=30 c//=5 # same as c=c//5 print(c)
Output
6
An important point is that always use the augmented operators with variables having an existing value. If not, then an error will occur. See below code:
c//=5 print(c)
Output
NameError: name 'c' is not defined
It means we cannot use augmented operator with an empty variable.
Simple Python Augmented Operators Program
val=3 val+=2 print(val) val-=3.0 print(val) val*=2.3 print(val) val/=3 print(val) val%=5 print(val) val**=3 print(val) val//=2 print(val)
Output
5 2.0 4.6 1.5333333333333332 1.5333333333333332 3.6050370370370364 1.0
So this is how we can easily use Python augmented operators. Hope you now have a complete idea of how and why these operators are used. Do feel free to share it with your developer friends.
Click here if you want to learn more about augmented operators.
Also, don’t hesitate to ask if you have any questions regarding the usage or implementation of augmented assignment operators in Python. We’ll be very happy to answer all.
Conclusion
To conclude this tutorial, hope you now have a proper understanding of how to use augmented assignment operators in Python. We’ll be looking forward to receive your valuable feedback on this article.
Do visit our other articles on Python programming. Thank you for reading this article.