Assignment operators

The assignment operators in Python are used to store data into a variable. We’ve already used the most common assingment operator (=), but there are many more of them:

  • = – assigns the value found in the right operands to the left operand. Example: x = 5.
  • += – adds the value found in the right operand to the value found in the left operand. Example: x = 5, x += 3 results in x = 8.
  • -= – substracts the value of the right operand from the value found in the left operand. Example: x = 5, x -= 3 results in x = 2.
  • *= – multiplies the value of the right operand by the value of the left operand. Example: x = 5, x *= 3 results in x = 15.
  • /= – divides the value of the left operand by the value of the right operand. Example: x = 5, x /=3 results in x = 1.667.
  • %= – divides the value found in the left operand by the value found in the right operand. The reminder will be placed in the left operand. Example: x = 5, x %=3 results in x = 2.
  • **= – determines the exponential value found in the left operand when raised to the power of the value in the right operand. Example: x = 5, x **=3 results in x = 125.
  • //= – divides the value found in the left operand by the value of the right operand. The integer result will be placed in the left operand. Example: x = 5, x //= 3 results in x = 1.
Geek University 2022