Python | Assignment Operators

We already looked at one assignment operator when we talked about variables. Let’s take a look at few of the most commonly used assignment operators.

The Add AND += operator adds the values of the left and right operands together then assigns the resulted value to the left operand.

x += y

is the same as

x = x+y

The Divide AND /= operator takes the value of the left operand and divides it by the value of the right operand then assigns the result to the left operand.

x /= y

is the same as

x = x/y

The Modulus AND %= operator divides the value of the right operand by the value of the left operand then assigns the reminder to the left operand.

x %= y

is the same as

x = x%y

The rest of the assignment operators (-=, *=, **=, //=, work in the same way.

Leave a Reply