Augmented assignment in Python
In this tutorial, we will learn about the AUGMENTED ASSIGNMENT in Python language. Basically when we discuss the assignment operator in Python that is nothing but an equal (=) sign. But the augmented assignment contains the one equal and one mathematical Operator (+,-,*,/,// etc.).
Augmented assignment
The basic syntax of the augmented assignments is an expression in which the same variable name appears on the left and the right of the assignment’s. Now we have seen the example with the use of addition augmented assignment (+=) statement as:
total = 0 for number in [1, 2, 3, 4, 5]: total += number # add number to total print(total)
Output: 15
Let see the other Augmented assignment in Python:
- addition Augmented assignment (+=) : This function (x += y) mathematically written as x = x+y.
Example :x=0 x+=2 print(x)
Output: 2
- Subtraction Augmented assignment (-=) : This function (x -= y) mathematically written as x = x-y.
Example:
x=0 x-=2 print(x)
Output: -1
- Multiplication Augmented assignment (*=): This function (x *= y) mathematically written as x = x*y.
Example:x=2 x*=2 print(x)
Output: 4
- Division Augmented assignment (/=) : This function (x /= y) mathematically written as x = x/y.Example:
x=2 x/=2 print(x)
Output: 1
- Exponential Augmented assignment (**=) : This function (x **= y) mathematically written as x = x**y.
Example:x=2 x**=2 print(x)
Output: 4
You can also see:
Leave a Reply