An operator that is used to store/assign a value to a variable is called assignment operator in Python.
The most common assignment operator is equal operator “=”, which assigns the right-hand value to the left-hand variable.
The general syntax to assign a value to a variable is as follows:
variable_name = value; // Here, a value can be a constant, a variable, an expression, or the function call.
Let us take some examples of using the assignment operator.
x = 20; # Literal assignment. Here, assigns a value 10 to a variable x by = operator. y = x + 2; # Assignment with an expression. z = x * y; # Assignment with an expression with literal. p = q = r = s = 50 # Assigns a common value to multiple variables. p, q, r = 20, 40, 60 # Multiple assignments in a single line. Cities = ('Dhanbad', 'New York', 'Sydney', 'Paris') # Assigns multiple values in a sequence.
Classification of Assignment Operators in Python
We can classify assignment operators in Python into three basic categories. They are as:
- Simple assignment
- Compound assignment
- Assignment as expression
Let us have a look one by one with the help of examples.
Simple Assignment
We use a simple assignment operator to store or assign a value to a variable or to store a value of a variable into another variable.
The general syntax to represent a simple assignment is as below:
variable_name = expression;
In the above syntax,
- variable_name: It is the name of a variable that represents a memory location where a value stores.
- expression: It may be a constant, variable, or a combination of constants, variables, and operators.
- = is an assignment operator.
Let’s take an example program based on the simple assignment operator in Python.
x = 20 y = 50 z = x + y + 2 print("Result = ", z)
Output: Result = 72
Compound Assignment Operators in Python
Compound assignment operators in Python comprise a simple assignment operator with another binary operator. They are also called shorthand operators in Python.
The general syntax of the compound assignment operator is as:
variable_name op = expression;
Here, the op signifies the basic binary operator. It may be +, –, *, /, %, etc. variable_name and expression are the same as explained in the simple assignment.
Python language supports the various kinds of shorthand or compound assignment operators. Let’s understand all the types of compound assignment operators one by one with the help of some examples.
1. Add and Assignment Operator (+=)
This operator adds the value of right operand to the value of the left operand and assigns the result to left operand. The general syntax is:
x += y # It is equivalent to x = x + y.
For example:
x = 20 y = 5 z = 10 x += y print("Result of (x = x + y) = ", x) z += x + y print("Result = ", z)
Output: Result of (x = x + y) = 25 Result = 40
2. Subtract and Assignment Operator (-=)
This operator subtracts the value of right operand from the value of left operand and assigns the result to left operand. The general syntax is:
x -= y # It is equivalent to x = x - y.
For example:
x = 20 y = 5 z = 10 x -= y print("Result of (x = x - y) = ", x) z -= x + y print("Result = ", z)
Output: Result of (x = x - y) = 15 Result = -10
3. Multiply and Assignment operator (*=)
This operator multiplies the value of right operand with the value of left operand and assigns the result to left operand. The general form is as:
x *= y # It is equivalent to x = x * y.
For example:
x = 2 y = 5 z = 3 x *= y print("Result of (x = x * y) = ", x) z *= x * y print("Result = ", z)
Output: Result of (x = x * y) = 10 Result = 150
4. Divide and Assignment Operator (/=)
This operator divides the value of left operand with the value of right operand and assigns the result to left operand. The general syntax is as:
x /= y # It is equivalent to x = x / y.
For example:
x = 20 y = 5 z = 100 x /= y print("Result of (x = x / y) = ", x) z /= x * y print("Result = ", z)
Output: Result of (x = x / y) = 4.0 Result = 5.0
5. Modulus and Assignment operator (%=)
This operator takes modulus on the values using two operands and assigns the result to the left operand. The general syntax is as:
x %= y # It is equivalent to x = x % y.
For example:
x = 211 y = 5 z = 100 x %= y print("Result of (x = x % y) = ", x) z %= x + y print("Result = ", z)
Output: Result of (x = x % y) = 1 Result = 4
6. Floor Division and Assignment Operator (//=)
This operator performs floor division on the operands and assigns the result to the left side operand. The general syntax is:
x //= y # It's equivalent to x = x // y.
For example:
x = 211 y = 5 z = 100 x //= y print("Result of (x = x // y) = ", x) z //= x + y print("Result = ", z)
Output: Result of (x = x // y) = 42 Result = 2
7. Exponent and Assignment Operator (**=)
This operator performs exponential (i.e. power) calculation on the operands and assigns the result to the left side operand. The general syntax is as below:
x **= y # It's equivalent to x = x ** y.
For example:
x = 2 y = 5 z = 2 x **= y print("Result of (x = x ** y) = ", x) z **= x + y - 30 print("Result = ", z)
Output: Result of (x = x ** y) = 32 Result = 128
8. Bitwise AND and Assignment Operator (&=)
This operator performs bitwise AND calculation on the operands and assigns the result to the left side operand. The general syntax is as below:
x &= y # It's equivalent to x = x & y.
For example:
x = 20 y = 5 x &= y print("Result of (x = x & y) = ", x)
Output: Result of (x = x & y) = 4
9. Bitwise OR and Assignment Operator (|=)
This operator performs bitwise OR calculation on the operands and assigns the result to the left side operand. The general syntax is as follows:
x |= y # It's equivalent to x = x | y.
For example:
x = 10 y = 5 x |= y print("Result of (x = x | y) = ", x)
Output: Result of (x = x | y) = 15
10. Bitwise XOR and Assignment Operator (^=)
This operator performs bitwise XOR calculation on the operands and assigns the result to the left side operand. The general form is as follows:
x ^= y # It's equivalent to x = x ^ y.
For example:
x = 20 y = 10 x ^= y print("Result of (x = x ^ y) = ", x)
Output: Result of (x = x ^ y) = 30
11. Bitwise Right Shift and Assignment Operator (>>=)
This operator performs the bitwise right shift calculation on the operands and assigns the result to the left side operand. The general form is as below:
x >>= y # It's equivalent to x = x >> y.
For example:
x = 6 y = 2 x >>= y print("Result of (x = x >> y) = ", x)
Output: Result of (x = x >> y) = 1
12. Bitwise Left Shift and Assignment Operator (<<=)
This operator performs the bitwise left shift calculation on the operands and assigns the result to the left side operand. The general form is as below:
x <<= y # It's equivalent to x = x << y.
For example:
x = 6 y = 2 x <<= y print("Result of (x = x << y) = ", x)
Output: Result of (x = x << y) = 24
Let’s take one more example program for practice based on all the important compound assignment operators in Python.
Program code:
x, y, z = 20, 30, 50 x += y y -= x + z z *= x * y print("x = ", x) print("y = ", y) print("z = ", z )
Output: x = 50 y = -70 z = -175000
Program explanation:
1. The expression x += y will be calculated in the following steps:
x += y is equivalent to x = x + y x = 20 + 30 x = 50
2. The expression y -= x + z is the same as y = y – (x + z)
y = y – ( 50 + 50) y = 30 – 100 y = -70
3. The expression z *= x * y is the same as z = z * (x * y)
z = z * (50 * (-70)) z = 50 * (-3500) z = -175000
Assignment as Expression
In Python, we can also treat an assignment operation as an expression because the operation has a result. The outcome (or result) of an expression is a value that stored in a variable. For example:
x = y – z + 4
Here, the expression y – z + 4 valuated first, and then its result stored into the variable x. Let’s take an example program based on it.
Program code:
a, b, c = 19, 31, 50 a += 1 b -= 1 c *= 2 x = (10 + a) y = x + 100 z = x + y + c print("Value of a: ", a) print("Value of b: ", b) print("Value of c: ", c) print("Value of x: ", x) print("Value of y: ", y) print("Value of z: ", z)
Output: Value of a: 20 Value of b: 30 Value of c: 100 Value of x: 30 Value of y: 130 Value of z: 260
Program explanation:
The following steps involved to evaluate the above expressions of the Python program.
1. Evaluation of expression: a += 1
a = a + 1 a = 19 + 1 a = 20
2. Evaluation of expression: b -= 1
b = b – 1 b = 31 – 1 b = 30
3. Evaluation of expression: c *= 2
c = c * 2 c = 50 * 2 c = 100
4. Evaluation of x = (10 + a)
x = 10 + 20 # Here, the value of x will be 20, not 19. x = 30
5. Evaluation of y = x + 100
y = 30 + 100 /# Here, value of x will be 30. y = 130
6. Evaluation of z = x + y + c
z = 30 + 130 +100 # Here, value of c will be 100. z = 260
Try It Yourself
Program code 1:
a, b, c = 9, 21, 10 a += 1 b -= 1 c *= 2 x = (10 + a)* 2 / 10 y = (x + (11 * x)) / 12 z = x + y + c print("Value of x: ", x) print("Value of y: ", y) print("Value of z: ", z)
Program code 2:
a, b, c = 20, 10, 5 exp = (b + a) * c / 10 print("Value of exp: ", exp)
In this tutorial, you learned assignment operators in Python and its types with example programs. Hope that you will have understood the basic points of assignment operators.
In the next tutorial, we will learn comparison operators in Python with many example programs.
Thanks for reading!!!
Next ⇒ Comparison Operators in Python⇐ Prev Next ⇒