Augmented assignment operators in python
Augmented assignment operators in Python combine addition and assignment in one statement. Instead of writing a = a + 1, you can write a += 1. This can make your code more concise and readable.
There are many augmented assignment operators in Python, including:
+= (addition)
-= (subtraction)
*= (multiplication)
/= (division)
//= (floor division)
%= (modulus)
**= (exponentiation)
&= (bitwise AND)
|= (bitwise OR)
^= (bitwise XOR)
<<= (bitwise left shift)
>>= (bitwise right shift)
Augmented assignment operators can be used with any Python variable, including numbers, strings, lists, and dictionaries.
Here are some examples of how to use augmented assignment operators in Python:
Python
# Add 1 to the variable a a = 1 a += 1 # Subtract 2 from the variable b b = 10 b -= 2 # Multiply 3 by the variable c c = 5 c *= 3 # Divide 4 by the variable d d = 10 d /= 4 # Floor divide 5 by the variable e e = 10 e //= 5 # Take the modulus of 6 by the variable f f = 10 f %= 6 # Raise 7 to the power of 2 g = 7 g **= 2 # Perform a bitwise AND operation on the variables h and i h = 10 i = 5 h &= i # Perform a bitwise OR operation on the variables j and k j = 10 k = 5 j |= k # Perform a bitwise XOR operation on the variables l and m l = 10 m = 5 l ^= m # Perform a bitwise left shift on the variable n by 1 bit n = 10 n <<= 1 # Perform a bitwise right shift on the variable o by 1 bit o = 10 o >>= 1
Augmented assignment operators can be a powerful tool for writing concise and readable Python code. They can be used with any Python variable, and they can be used to perform a variety of arithmetic and bitwise operations.


















