Assignment-Operators

Wed 12 November 2025
#  created : 20250113
#  https://www.scientecheasy.com/2022/10/assignment-operators-in-python.html/
# Assignment Operators in Python
# Simple Assignment
x = 20
y = 50
z = x + y + 2
print("Result = ", z)
Result =  72
# x += y  It is equivalent to x = x + y.
x = 20
y = 5
z = 10
x += y
print("Result of (x = x + y) = ", x)
Result of (x = x + y) =  25
z += x + y
print("Result = ", z)
Result =  40
x -= y # It is equivalent to x = x - y.
x = 20
y = 5
z = 10
x -= y
print("Result of (x = x - y) = ", x)
Result of (x = x - y) =  15
z -= x + y # --> z = z - (x + y)
print("Result = ", z)
Result =  -10
x *= y # It is equivalent to x = x * y.
x = 2
y = 5
z = 3
x *= y
print("Result of (x = x * y) = ", x)
Result of (x = x * y) =  10
z *= x * y
print("Result of (z = z*(x * y) = ", z)
Result of (z = z*(x * y) =  150
x /= y # It is equivalent to x = x / y.
x = 20
y = 5
z = 100
x /= y
print("Result of (x = x / y) = ", x)
Result of (x = x / y) =  4.0
z /= x * y
print("Result = ", z)
Result =  5.0
x %= y # It is equivalent to x = x % y.
x = 211
y = 5
z = 100
x %= y
print("Result of (x = x % y) = ", x)
Result of (x = x % y) =  1
z %= x + y
print("Result = ", z)
Result =  4
x //= y # It's equivalent to x = x // y.
x = 211
y = 5
z = 100
x //= y
print("Result of (x = x // y) = ", x)
Result of (x = x // y) =  42
z //= x + y
print("Result = ", z)
Result =  2
x = 211
y = 5
z = 100
x //= y

print("Result of (x = x // y) = ", x)

z //= x + y
print("Result = ", z)
Result of (x = x // y) =  42
Result =  2
#  Exponent and Assignment Operator (**=)
x **= y # It's equivalent to x = x ** y.
x = 2
y = 5
z = 2
x **= y

print("Result of (x = x ** y) = ", x)

z **= x + y - 30
print("Result = ", z)
Result of (x = x ** y) =  32
Result =  128
# Bitwise AND and Assignment Operator (&=)
x &= y # It's equivalent to x = x & y.
x = 20
y = 5
x &= y
print("Result of (x = x & y) = ", x)
Result of (x = x & y) =  4
#  Bitwise OR and Assignment Operator (|=)
x |= y # It's equivalent to x = x | y.
x = 10
y = 5
x |= y
print("Result of (x = x | y) = ", x)
Result of (x = x | y) =  15
# Bitwise XOR and Assignment Operator (^=)
x ^= y # It's equivalent to x = x ^ y.
x = 20
y = 10
x ^= y
print("Result of (x = x ^ y) = ", x)
Result of (x = x ^ y) =  30


Score: 40

Category: python-basics