Python – Assignment Operators
Assignment operators are used to assign values to variables.
Assign (=
): Assigns the right-hand operand to the left-hand operand.
c = 10
print(c) # Output: 10
Add and Assign (+=
): Adds right operand to the left operand and assigns the result to the left operand.
c += 5 # Equivalent to c = c + 5
print(c) # Output: 15
Subtract and Assign (-=
): Subtracts right operand from the left operand and assigns the result to the left operand.
c -= 3 # Equivalent to c = c - 3
print(c) # Output: 12
Multiply and Assign (*=
): Multiplies left operand with the right operand and assigns the result to the left operand.
c *= 2 # Equivalent to c = c * 2
print(c) # Output: 24
Divide and Assign (/=
): Divides left operand by the right operand and assigns the result to the left operand.
c /= 4 # Equivalent to c = c / 4
print(c) # Output: 6.0
Modulus and Assign (%=
): Takes modulus using two operands and assigns the result to the left operand.
c %= 5 # Equivalent to c = c % 5
print(c) # Output: 1.0