Basic Assignment (=)
Assigns a value to a variable.
x = 10
Addition Assignment (+=)
Adds a value to a variable and assigns the result to the variable.
x += 5 # Equivalent to x = x + 5
Subtraction Assignment (-=)
Subtracts a value from a variable and assigns the result to the variable.
x -= 3 # Equivalent to x = x - 3
Multiplication Assignment (*=)
Multiplies a variable by a value and assigns the result to the variable.
x *= 2 # Equivalent to x = x * 2
Division Assignment (/=)
Divides a variable by a value and assigns the result to the variable.
x /= 4 # Equivalent to x = x / 4
Floor Division Assignment (//=)
Performs floor division (integer division) on a variable and assigns the result to the variable..
x //= 3 # Equivalent to x = x // 3
Modulus Assignment (%=)
Computes the remainder of dividing a variable by a value and assigns the result to the variable.
x %= 4 # Equivalent to x = x % 4
Exponentiation Assignment (**=)
Raises a variable to the power of a value and assigns the result to the variable.
x **= 3 # Equivalent to x = x ** 3
Bitwise AND Assignment (&=)
Applies a bitwise AND operation on a variable and assigns the result to the variable.
x &= 2 # Equivalent to x = x & 2
Bitwise OR Assignment (|=)
Applies a bitwise OR operation on a variable and assigns the result to the variable.
x |= 2 # Equivalent to x = x | 2
Bitwise XOR Assignment (^=)
Applies a bitwise XOR operation on a variable and assigns the result to the variable.
x ^= 2 # Equivalent to x = x ^ 2
Bitwise Left Shift Assignment (<<=)
Applies a bitwise left shift operation on a variable and assigns the result to the variable.
x <<= 1 # Equivalent to x = x << 1
Bitwise Right Shift Assignment (>>=)
Applies a bitwise right shift operation on a variable and assigns the result to the variable.
x >>= 1 # Equivalent to x = x >> 1
Python program demonstrating the use of assignment operators:
# Initial value a = 10 b = 5 # Using assignment operators a += b # Equivalent to a = a + b print(f'a += b: {a}') # Output: a += b: 15 a -= b # Equivalent to a = a - b print(f'a -= b: {a}') # Output: a -= b: 10 a *= b # Equivalent to a = a * b print(f'a *= b: {a}') # Output: a *= b: 50 a /= b # Equivalent to a = a / b print(f'a /= b: {a}') # Output: a /= b: 10.0 a %= b # Equivalent to a = a % b print(f'a %= b: {a}') # Output: a %= b: 0.0 a **= b # Equivalent to a = a ** b print(f'a **= b: {a}') # Output: a **= b: 1.0 a //= b # Equivalent to a = a // b print(f'a //= b: {a}') # Output: a //= b: 0.0
Output
a += b: 15 a -= b: 10 a *= b: 50 a /= b: 10.0 a %= b: 0.0 a **= b: 1.0 a //= b: 0.0