Assignments operator
Assignment Operators:Assignment operators in Python are used to assign values to variables. They combine the assignment (=) operator with other arithmetic or bitwise operators to perform an operation and assign the result to a variable.
- Assignment (=): Assigns a value to a variable.
x = 5
- Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.
x = 5
x += 3 # Equivalent to x = x + 3. The value of x becomes 8.
- Subtraction Assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
x = 12
x -= 3 # Equivalent to x = x – 3. The value of x becomes 9.
- Multiplication Assignment (*=): Multiplies the right operand with the left operand and assigns the result to the left operand.
x = 5
x *= 3 # Equivalent to x = x * 3. The value of x becomes 15.
- Division Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.
x = 50
x /= 5 # Equivalent to x = x / 3. The value of x becomes 10.0.
- Modulus Assignment (%=): Performs modulus on the left operand with the right operand and assigns the result to the left operand.
x = 5
x %= 3 # Equivalent to x = x % 3. The value of x becomes 2.
- Exponentiation Assignment (**=): Raises the left operand to the power of the right operand and assigns the result to the left operand.
x = 3
x **= 4 # Equivalent to x = x ** 3. The value of x becomes 81.
- Floor Division Assignment (//=): Performs floor division on the left operand with the right operand and assigns the result to the left operand.
x = 5
x //= 3 # Equivalent to x = x // 3. The value of x becomes 2.