In this article, we will learn about all the “* Assignment Operators in Python*” with examples of each. Assignment operators in Python are essential tools for manipulating and assigning values to variables. These operators not only let you store data in variables but also perform various operations simultaneously. Understanding these operators is crucial for efficient Python programming, as they streamline code while making it more readable and maintainable. If you encounter any challenges in grasping these operators, don’t hesitate to seek Python help from the experts at CodingZap.

## What Are Operators and Operands?

Operators can be understood as the mathematical symbols we have seen since childhood, like the +, -, *, / =. They are used to perform mathematical, logical or bitwise operations in a programming language.

Operands are the values or variables on which operators act. For example, “+” will add the values of two operands, and similarly “*” will multiply the values of the 2 operands.

In the expression “a+b”, “+” is the operator and “a” and “b” are the operands.

What Is Assignment Operators in Python?

Assignment operators are a fundamental concept in Python and most programming languages. They are used to assign values to variables, as well as initialize and update them. They assign the value on the right-hand side to the variable on the left.

In Python, the primary assignment operator is the “Simple assignment operator (=)”, which as the name suggests, assigns values to variables. However, Python offers a rich set of assignment operators that provide more functionality than simple assignments. These operators not only assign values but also perform specific operations at the same time, such as addition, subtraction, multiplication, division, bitwise operations, and more.

## What Are the Basics of Assignments?

First, we’ll clarify some basic rules about the assignments in Python. For this, we’ll use the simple assignment operator “=”.

A sample program using “=” is as follows:

` ````
```# Assignment statement
a = 5
# Printing values
print(a)

Output: 5

In the above example, the value on the right-hand side is assigned to the variable on the left. We check the value assigned by printing the variable and get the expected result, i.e. 5.

In the example above, the statement a = 5 is called an assignment statement, which assigns values to the variables.

The syntax for an assignment statement is as follows:

` ````
```variable = expression

As we can see, it is composed of three components:

- The left operand of an assignment statement must always and only be a variable.
- Then comes the assignment operator itself.
- The right operand can be a value, an expression or an object.

Now that we have this rule clear let us look at the assignment operators in Python. The assignment operators allow us to create, initialise and update the variables.

### The simple assignment operator “=”

It is the primary assignment operator

Let’s look at some examples to understand the uses of the simple assignment operator:

` ````
```# Program to demonstrate simple assignment operator
#assigning value to celsius variable
celsius = 25
#calculating fahrenheit value from celsius
fahrenheit = (celsius * 9 / 5) + 32
#assigning a string
str = "Python is easy and fun"
#initializing a boolean value
val = 24 < 45
#creating a set
set= ([1,2,3,4,5])
#creating a dictionary
dict = {"Name": "Sounetra", "ID": 345128, "Hobbies": ["Writing", "Coding", "Swimming", "Martial arts"] }
#creating a list containing all of the previously assigned variables
list = [celsius, fahrenheit, str, val, set, dict]
# Printing values
print(list)

Output:

[25, 77.0, ‘Python is easy and fun’, True, [1, 2, 3, 4, 5], {‘Name’: ‘Sounetra’, ‘ID’: 345128, ‘Hobbies’: [‘Writing’, ‘Coding’, ‘Swimming’, ‘Martial arts’]}] |

As we can see in the above code, the simple assignment operator is used to create every variable type in Python, be it a primitive integer, string, or complex data structure. Strings are also an interesting concept in Python, if you’re interested to know how to compare strings in Python then you can check out our article.

### Updating values

The assignment operators are also used to update variable values in Python. An example is as follows:

` ````
```#initializing integer and list
a = 20
list = [1,2,3,4,5]
#printing values
print(a)
print(list)
#updating values
a = 45
list[2] = 30
#printing values
print(a)
print(list)

Output:

20 [1, 2, 3, 4, 5] 45 [1, 2, 30, 4, 5] |

In the above example, we initialized our integer and list with initial values and printed them for reference. Then, we modified these values and printed them again to verify the changes.

### Multiple and parallel assignments

Python provides additional functionality to perform multiple and parallel assignments to variables in a single line. This reduces lines of code and complexity. Let’s understand them with an example:

` ````
```#multiple assignments
a = b = c = 45
#parallel assignments
d,e,f = 100, 200 < 300, "This is so fun"
#printing values
print("a =", a, ", b =", b, ", c =", c)
print("d =", d, ", e =", e, ", f =", f)

Output:

a = 45 , b = 45 , c = 45 d = 100 , e = True , f = This is so fun |

In the above code, we demonstrate multiple and parallel assignments.

We can assign the same literal value to multiple variables in multiple assignments. In the above code, the integer value 45 is assigned to all the variables a,b,c. The data type of all the variables in a multiple-assignment statement must be the same.

In parallel assignments, we can assign different values to different variables using comma-separated variables and values on either side of the assignment operator. The values are stored in the order of writing the pairs. The variable types need not be the same in parallel assignments.

## Augmented assignment operators in Python

The simple assignment operator is used to assign values to variables in Python. However, Python also supports complex assignments using which we can calculate various values and assign them to the variable in a single line.

The basic syntax for augmented assignment operators is as follows:

`variable $= expression` |

The ‘$’ in the above syntax can be replaced by various operators to perform operations on operands before assigning the final value to the variable.

Python equates the above syntax to the following expression:

`variable = variable $ expression` |

We’ll understand these operators better if we directly look at their examples.

Let’s have a look at all the augmented assignment operators in Python one at a time:

### Add assignment operator “+=”

This operator adds the left and the right operands and assigns the resulting value to the variable on the left.

An example would be

**a += b;**

Which will equate to

**a = a+b;**

It is important to note that only the value of “a” will change, whereas the value of “b” will remain the same. This is because the sum of “a” and “b” is finally stored in the variable “a”.

Let’s look at the code and its output:

` ````
```#initializing 2 variables and a list
a = 10
b = 20;
list = [1,2,3,4,5]
#printing values
print("a = ", a, "b =", b)
print("list =", list)
# updating values using add assignment operator
a += b
list += [6,7]
#printing values
print("a = ", a, "b =", b)
print("list =", list)

Output:

a = 10 b = 20 list = [1, 2, 3, 4, 5] a = 30 b = 20 list = [1, 2, 3, 4, 5, 6, 7] |

In the above code, we initialized 2 variables and added the value of the second variable to the first one using the add assignment operator. Note that the value of “a” changes, but the value of “b” remains the same.

This operator can also be used on some data structures like lists and tuples, using which we can append values at the end of the list, as can be seen in the code above.

### Subtract assignment operator

This operator is used to subtract the right operand from the left and assign the resulting value to the variable on the left.

A sample code is as follows:

` ````
```#initializing 2 variables
a = 50
b = 2;
#printing values
print("a = ", a, "b =", b)
# updating values using subtract assignment operator
a -= b
#printing values
print("a = ", a, "b =", b)

Output:

a = 50 b = 2 a = 48 b = 2 |

In the above code, we initialized the 2 variables “a” and “b” and printed their values for reference. Then, we subtracted the value of “b” from “a” using the subtract assignment operator. The final expression equated to “a=a-b”. At last, we printed the final values of “a” and “b”.

### Multiplication assignment operator

The multiplication assignment operator is used to multiply the right operand with the left and assign the resulting value to the variable on the left.

A sample code is as follows:

` ````
```#initializing 2 variables
a = 50
b = 3;
#printing values
print("a = ", a, "b =", b)
# updating values using multiplication assignment operator
a *= b
#printing values
print("a = ", a, "b =", b)

Output:

a = 50 b = 3 a = 150 b = 3 |

In the above code, we initialized the 2 variables “a” and “b” and printed their values for reference. Then, we multiplied the values of “a” and “b” using the multiplication assignment operator. The final expression equated to “a=a*b”. At last, we printed the final values of “a” and “b” to check the resulting values.

### Division assignment operator

The division assignment operator divides the left operand from the right and assigns the resulting value to the operand on the left.

A sample code is as follows:

` ````
```#initializing 2 variables
a = 50
b = 3;
#printing values
print("a = ", a, "b =", b)
# updating values using division assignment operator
a /= b
#printing values
print("a = ", a, "b =", b)

Output:

a = 50 b = 3 a = 16.666666666666668 b = 3 |

In the above code, we initialized the 2 variables “a” and “b” and printed their values for reference. Then, we divided “a” by “b” using the division assignment operator. The final expression equated to “a=a/b”. At last, we printed the final values of “a” and “b”.

### Floor assignment operator

The floor assignment operator divides the operand on the left from the right and rounds the value to the greatest integer less than or equal to the resultant value. This value is then stored in the operand on the left.

Sample code is as follows:

` ````
```#initializing 2 variables
a = 50
b = 3;
#printing values
print("a = ", a, "b =", b)
# updating values using floor assignment operator
a //= b
#printing values
print("a = ", a, "b =", b)

Output:

a = 50 b = 3 a = 16 b = 3 |

In the above code, we initialized the 2 variables “a” and “b” and printed their values for reference. Then, we divided “a” from “b” using the floor assignment operator. This operator also divides the variables in the same way as the division assignment operator but rounds off the answer to the greatest integer less than or equal to the answer. In this case, the value “16.66” was rounded off to “16”.

At last, we printed the final values of “a” and “b”.

### Modulus assignment operator

The modulus assignment operator is used to extract the remainder after dividing the operand on the left from the right. The remainder is then stored in the operand on the left.

Sample code is as follows:

` ````
```#initializing 2 variables
a = 50
b = 3;
#printing values
print("a = ", a, "b =", b)
# updating values using modulus assignment operator
a %= b
#printing values
print("a = ", a, "b =", b)

Output:

a = 50 b = 3 a = 2 b = 3 |

In the above code, we initialized the 2 variables “a” and “b” and printed their values for reference. We then used the modulus assignment operator to get the remainder of the division “a/b”, and store the resulting value in “a”. At last, we printed the final values of “a” and “b”.

### Exponentiation assignment operator

This operator is used to calculate the exponent of the left-side operand raised to the power of the right-side operand, which is then stored in the operand on the left.

Sample code is as follows:

` ````
```#initializing 2 variables
a = 50
b = 3;
#printing values
print("a = ", a, "b =", b)
# updating values using exponentiation assignment operator
a **= b
#printing values
print("a = ", a, "b =", b)

Output:

a = 50 b = 3 a = 125000 b = 3 |

In the above code, we initialized the 2 variables “a” and “b” and printed their values for reference. We then used the exponentiation assignment operator to get the value of “a” raised to the power of “b”, and store the resulting value in “a”. At last, we printed the final values of “a” and “b”.

### Bitwise AND assignment operator

This operator calculates the Bitwise AND value of the operands on the left and right and stores the value in the left operand.

Sample code:

` ````
```#initializing 2 variables
a = 50
b = 3;
#printing values
print("a = ", a, "b =", b)
# updating values using bitwise AND assignment operator
a &= b
#printing values
print("a = ", a, "b =", b)

Output:

a = 50 b = 3 a = 2 b = 3 |

In the above code, we initialized the 2 variables “a” and “b” and printed their values for reference. We then used the bitwise AND assignment operator to get the bitwise AND of “a” and “b”, and store the resulting value in “a”. At last, we printed the final values of “a” and “b”.

### Bitwise OR assignment operator

This operator calculates the Bitwise OR value of the operands on the left and right and stores the value in the left operand.

Sample code:

` ````
```#initializing 2 variables
a = 50
b = 3;
#printing values
print("a = ", a, "b =", b)
# updating values using bitwise OR assignment operator
a |= b
#printing values
print("a = ", a, "b =", b)

Output:

a = 50 b = 3 a = 51 b = 3 |

In the above code, we initialized the 2 variables “a” and “b” and printed their values for reference. We then used the bitwise OR assignment operator to get the bitwise

OR of “a” and “b”, and store the resulting value in “a”. At last, we printed the final values of “a” and “b”.

### Bitwise XOR assignment operator

This operator calculates the XOR of the 2 operands and stores the value in the left operand.

Sample code:

` ````
```#initializing 2 variables
a = 50
b = 3;
#printing values
print("a = ", a, "b =", b)
# updating values using bitwise AND assignment operator
a ^= b
#printing values
print("a = ", a, "b =", b)

Output:

a = 50 b = 3 a = 49 b = 3 |

In the above code, we initialized the 2 variables “a” and “b” and printed their values for reference. We then used the bitwise XOR assignment operator to get the bitwise XOR of “a” and “b” and store the resulting value in “a”. At last, we printed the final values of “a” and “b”.

### Bitwise Left Shift assignment operator

This operator left-shifts the bits of the operand on the left by the units as declared by the operand on the right and stores the value in the left operand.

Sample code:

` ````
```#initializing 2 variables
a = 50
b = 3;
#printing values
print("a = ", a, "b =", b)
# updating values using bitwise AND assignment operator
a <<= b
#printing values
print("a = ", a, "b =", b)

Output:

a = 50 b = 3 a = 400 b = 3 |

In the above code, we initialized the 2 variables “a” and “b” and printed their values for reference. We then used the bitwise left shift assignment operator to left shift the bits of “a” by “b” units and store the resulting value in “a”. At last, we printed the final values of “a” and “b”.

### Bitwise Right Shift assignment operator

This operator right-shifts the bits of the operand on the left by the units as declared by the operand on the right, and stores the value in the left operand.

Sample code:

` ````
```#initializing 2 variables
a = 50
b = 3;
#printing values
print("a = ", a, "b =", b)
# updating values using bitwise AND assignment operator
a >>= b
#printing values
print("a = ", a, "b =", b)

Output:

a = 50 b = 3 a = 6 b = 3 |

In the above code, we initialized the 2 variables “a” and “b” and printed their values for reference. We then used the bitwise right shift assignment operator to right shift the bits of “a” by “b” units, and store the resulting value in “a”. At last, we printed the final values of “a” and “b”.

## Conclusion

In this article, we learned about the assignment operators in Python. The primary assignment operator is the simple assignment operator (“=”).

We then learned that Python supports more complex assignment operators that can be used to calculate values in place.

The assignment operators can be used to calculate mathematical, logical, and bitwise operations.

We also saw a consistent property of all the assignment operators, in which they calculated the expression on the right and assigned the resulting value to the variable on the left.

There are many reasons why students look for assignment help online like difficulty in understanding the assignment, time limitation, etc. So, if you’re also looking then you can always hire CodinngZap experts.