Introduction to Assignment Operators in Python
Assignment operators in python allow assigning value to a variable in a particular manner. Each of them has been designed to address a particular situation and must be used based on the context. Python provides various types of assignment operators, and the application of each of them is solely governed by the user’s need in the given context. Assignment operators allow skipping of steps and, in a way, act as a shortcut. With many assignment operators in existence, it becomes essential to understand the working of each of them.
Types of Assignment Operators
Various types of assignment operators in Python have been discussed, as summarized in the following table.
Assignment Operator in Python |
Description |
Example |
= | It assigns a particular value to a variable. | a = 29, temperature = -30.9 |
+= | Adds the value specified on the right-hand side operator to the variable on the operator’s left-hand side. | Imagine that the value of variable a is 10, and we perform a += 21, then variable a becomes 31. |
-= | Subtracts the value specified on the operator’s right-hand side from the variable on the left-hand side of the operator. | Imagine that the value of variable a is 21, and we perform a -= 10, then variable a becomes 11. |
*= | Multiplies the variable specified on the left-hand side of the operator with the value on the right-hand side of the operator. | Imagine that the value of variable a is 6, and we perform a *= 4, then variable a becomes 24. |
/= | Divides the variable specified on the operator’s left-hand side by the value specified on the right-hand side of the operator. | Imagine that the value of variable a is 9, and we perform a /= 3, then variable a becomes 9. |
%= | Divides the variable on the operator’s left-hand side by the value specified on the operator’s right-hand side and places the remainder, thus returned by the operation into the variable on the left-hand side. | Imagine that the value of variable a is 12, and we perform a %= 5. The remainder returned by the operation is 2, and a will be assigned the value of 2. |
//= | Divides the variable on the operator’s left-hand side by the value specified on the right-hand side of the operator and places the integer part of the result in the variable. | Imagine that the value of variable a is 12, and we perform a //= 5. The integer part of the result is 2. So, a will be assigned the value of 2. |
**= | Raises the value of the variable specified on the left-hand side of the operator to the power of value specified on the right-hand side of the operator | Imagine that the value of a is 2, and when we perform a **= 4, it will raise 2 to the power of 4, thus returning an as 16. |
Implementing Assignment Operators
Now that we have understood each of the operators’ function let’s go through the working of each of them discussed through the following Python program codes and the subsequent outputs.
1. ‘=’ Operator
Code:
b = 100
print('The initial value of variable b is ' + str(b))
Output:
Code:
b = 12.67 * 94.53
print('The value of variable b is ' + str(b))
Output:
2. ‘+=’ Operator
Code:
b = 100
b += 21.9
print('The value of variable b is ' + str(b))
Output:
3. ‘-=’ Operator
Code:
b = 100
b -= 21.9
print('The value of variable b is ' + str(b))
Output:
4. ‘*=’ Operator
Code:
b = 13.4
b *= 7.57
print('The value of variable b is ' + str(b))
Output:
5. ‘/=’ Operator
Code:
b = 130
b /= 1.33
print('The value of variable b is ' + str(b))
Output:
6. ‘%=’ Operator
Code:
b = 27
b %= 5
print('The value of variable b is ' + str(b))
Output:
7. ‘//=’ Operator
Code:
b = 27
b //= 5
print('The value of variable b is ' + str(b))
Output:
8. ‘**=’ Operator
Code:
b = 3
b **= 4
print('The value of variable b is ' + str(b))
Output:
In the above section, we just saw how to employ each of the operators and validated their functionality by going through the respective outputs. Now, we will demonstrate their use through another program code, which is as discussed in the following section. Let’s go through the program code.
Code:
p = 10
q = 12.5
r = 14.2
sum = p + q
print('Sum of ' + str(p) +' and '+str(q)+' is '+str(sum))
sum += r
print(sum)
sum -= r
print(sum)
sum *= r
print(sum)
sum /= r
print(sum)
sum %= r
print(sum)
sum //= r
print(sum)
sum **= r
print(sum)
Output:
Initially, three variables, p, q, and r, are assigned certain values. This is followed by assigning the sum of values of variables p and q to a variable that we have named as a sum. The sum of the variables p and q is then printed. Further, we make use of each of the assignment operators starting with ‘+=’. We add variable r to the variable sum, and the result is returned accordingly. Then we subtract the value of variable r from the variable sum. Each time an operator is utilized, the result is printed. Next, we employ the ‘*=’ operator to check how multiplication takes place and what value is returned. Examining the result after the use of each operator is important, as it helps understand how the operation is affecting the result. Next, we use the ‘/=’ operator that returns the result after a division operation. The last two operations return zero value. This is because of the value being assigned to the variable sum as a result of previous assignment operations.
Conclusion
Assignment operators are crucial elements in Python programming. They are quite useful in situations that involve breaking bigger tasks into smaller units. The application of these operators is evident in large-scale programs where the program code needs to be optimized to the greatest possible extent.
Recommended Articles
We hope that this EDUCBA information on “Assignment Operators in Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.