Assignment operators in Python are used to assign values to variables. They include the basic assignment operator (=) and various compound assignment operators that perform an operation on the variable while assigning a value.
-
Basic Assignment (=): Assigns a value to a variable.
-
Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.
-
Subtraction Assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
-
Multiplication Assignment (*=): Multiplies the left operand by the right operand and assigns the result to the left operand.
-
Division Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.
-
Floor Division Assignment (//=): Performs floor division on the left operand and assigns the result to the left operand.
-
Modulus Assignment (%=): Calculates the modulus of the left operand and assigns the result to the left operand.
-
Exponentiation Assignment (=):** Raises the left operand to the power of the right operand and assigns the result to the left operand.
x = 5y = 10
y += 3 # Equivalent to y = y + 3