Assignment operators in Python, and in other language programming, are used to assign values to variables.
For example, if we indicate a = 5 and b = 3 we are assigning the value 5 to the variable a and the value 3 to the variable b.
But let’s see in detail what other assignment operators exist:
Assignment Operators Python = assign
Example:
a = 5 assigns the value 5 to the variable a on the left (ie what is to the right of the equal.)
Another example could be a = b + c where to a this time we assign the sum b + c.
Assignment Operators Python += add and assign
Assigns the sum between it and the right operand to the left operand.
For example:
a += 2 is equivalent to doing a = a + 2, so it assigns to the variable a on the left the value of a (we always suppose a = 5) added to 2. So we will get a = 7.
Assignment Operators Python -= subtract and assign
Assigns the difference between it and the right operand to the left operand.
Ex:
a – = 2 is equivalent to doing a = a-2, so it assigns to the variable a on the left the value of a (we always assume a = 5) minus 2. So we will get a = 3.
Assignment Operators Python *= multiply and assign
Assigns the product between it and the right operand to the left operand.
An example:
a * = 2 is equivalent to doing a = a * 2, so it assigns to the variable a on the left the value of a (let’s always suppose a = 5) multiplied by 2. So we will get a = 10.
Assignment Operators Python /= divide and assign
Assigns the real division between it and the right operand to the left operand.
As for example:
a /= 2 is equivalent to doing a = a / 2, so it assigns to the variable a on the left the value of a (we always assume a = 5) divided by 2. So we will get a = 2.5.
Assignment Operators Python //= divide and assign
Assign the rounded division between it and the right operand to the left operand.
Eg: a //= 2 is equivalent to doing a = a // 2, so it assigns the value of a to the variable a on the left (we always assume a = 5) divided by 2 rounded to the integer. So we will get a = 2.
Assignment Operators Python %= calculate the remainder and assign
Assigns to the left operand the remainder obtained from the division between it and the right operand.
Example:
a%= 2 is equivalent to doing a = a% 2, so it assigns the value of a to the variable a on the left (we always assume a = 5) divided by 2 rounded to the integer. So we will get a = 1.
Assignment Operators Python **= calculate the power and assign
Assigns to the left operand the result of exponentiation of it as the base and exponent of the right operand.
Example:
a **= 2 is equivalent to doing a=a2, so it assigns to the variable a on the left the value of a (we always suppose a = 5) raised to 2. So we will get a = 25.
Try these simple examples interactively to familiarize yourself with the language.
Some exercises
Some exercises on assignment operators in Python
a = 10
b = 15 + a
c = b * 2
c += 1
print(a,b,c)
Other exercises
a = b = c = 10
a *= 10
b **= 2
c //= 4
print(a,b,c)
In this lesson we have studied the assignment operators in Python and have seen many exercises and practical examples.