Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Operators and Expressions

Use augmented assignment (+=, *=, etc.) operators - Python Program

Example 1:

x = 10 x += 5 x *= 2 print("Final value of x:", x)

Output

 
OUTPUT  :
Final value of x: 30
 

Example 2:

# Augmented Addition (+=) x = 10 x += 5 # Equivalent to x = x + 5 print(f"Augmented Addition (x += 5): x = {x}") # Augmented Subtraction (-=) y = 20 y -= 7 # Equivalent to y = y - 7 print(f"Augmented Subtraction (y -= 7): y = {y}") # Augmented Multiplication (*=) a = 3 a *= 4 # Equivalent to a = a * 4 print(f"Augmented Multiplication (a *= 4): a = {a}") # Augmented Division (/=) b = 25 b /= 5 # Equivalent to b = b / 5 print(f"Augmented Division (b /= 5): b = {b}") # Augmented Floor Division (//=) c = 17 c //= 3 # Equivalent to c = c // 3 print(f"Augmented Floor Division (c //= 3): c = {c}") # Augmented Modulus (%=) d = 10 d %= 3 # Equivalent to d = d % 3 print(f"Augmented Modulus (d %= 3): d = {d}") # Augmented Exponentiation (**=) e = 2 e **= 3 # Equivalent to e = e ** 3 print(f"Augmented Exponentiation (e **= 3): e = {e}") # Augmented Bitwise AND (&=) f = 12 # Binary: 1100 f &= 5 # Binary: 0101. Equivalent to f = f & 5 print(f"Augmented Bitwise AND (f &= 5): f = {f}") # Augmented Bitwise OR (|=) g = 6 # Binary: 0110 g |= 3 # Binary: 0011. Equivalent to g = g | 3 print(f"Augmented Bitwise OR (g |= 3): g = {g}") # Augmented Bitwise XOR (^=) h = 10 # Binary: 1010 h ^= 7 # Binary: 0111. Equivalent to h = h ^ 7 print(f"Augmented Bitwise XOR (h ^= 7): h = {h}") # Augmented Left Shift (<<=) i = 4 # Binary: 0100 i <<= 2 # Equivalent to i = i << 2 print(f"Augmented Left Shift (i <<= 2): i = {i}") # Augmented Right Shift (>>=) j = 16 # Binary: 10000 j >>= 2 # Equivalent to j = j >> 2 print(f"Augmented Right Shift (j >>= 2): j = {j}")

Output

 
OUTPUT  :
Augmented Addition (x += 5): x = 15
Augmented Subtraction (y -= 7): y = 13
Augmented Multiplication (a *= 4): a = 12
Augmented Division (b /= 5): b = 5.0
Augmented Floor Division (c //= 3): c = 5
Augmented Modulus (d %= 3): d = 1
Augmented Exponentiation (e **= 3): e = 8
Augmented Bitwise AND (f &= 5): f = 4
Augmented Bitwise OR (g |= 3): g = 7
Augmented Bitwise XOR (h ^= 7): h = 13
Augmented Left Shift (i <<= 2): i = 16
Augmented Right Shift (j >>= 2): j = 4