×

Assignment Operators in Python

The prime usage of Assignment Operators is to assign values to variables.

These are taken into account to do operations on values and variables. There are some special symbols in python to perform arithmetic, logical, and bitwise computations. Operands are the values on which operators perform operations.

Following are the assignment operators used in python:

  1. Assign: operator used =
  2. Add and Assignment: operator used +=
  3. Subtract and Assignment: operator used -=
  4. Multiply AND Assignment: operator used *=
  5. Divide and Assignment: operator used /=
  6. Modulus and Assignment: operator used % =
  7. Divide (floor) and Assignment: operator used //=
  8. Bitwise AND and Assignment: operator used &=
  9. Bitwise XOR and Assignment: operator used !=
  10. Bitwise Right Shift and Assignment: operator used ^=
  11. Bitwise right shift Assignment: operator used >>=
  12. Bitwise left shift Assignment: operator used <<=

Let’s understand each of the above operators in detail

Assignment operator:

The utility of this operator is to simply assign the value present on the right side of the operator to the operand present on the left side.

Syntax:

z = x + y

Example: Showing the implementation of add, and assignment operator

Assignment Operators in Python

Output:

Assignment Operators in Python

Add and Assignment operator:

The usefulness of this operator is to assign the result to the left operand after calculating the sum of values present on left and right side of the operator.

Syntax: 

x + = y

Example 1: Showing the implementation of add and assignment operator

Assignment Operators in Python

Output:

Assignment Operators in Python

Example 2: Showing the implementation of add and assignment operator

Assignment Operators in Python

Output:

Assignment Operators in Python

Subtract and Assignment:

The usefulness of this operator is to assign the result to the left operand after subtracting the values present on the right side.

Syntax:

x -= y

Example: Showing the implementation of subtract and assignment operator

Assignment Operators in Python

Output:

Assignment Operators in Python

Multiply and Assignment:

The usefulness of this operator is to assign the result to the left side of the operator after obtaining the result on multiplying values present on the left and right sides of the operator.

Syntax:

x *= y

Example: Showing the implementation of multiply and assignment operator

Assignment Operators in Python

Output:

Assignment Operators in Python

Divide and Assignment:

The utility of this operator is to assign the result in the variable present on the left side of operator. Here, the result is obtained after dividing the operand present on left side with the right operand.

Syntax: 

x /= y

Example: Showing the implementation of divide and assignment operator

Assignment Operators in Python

Output:

Assignment Operators in Python

Modulus and Assignment:

The utility of this operator is to assign the result to the operand present on the left side of the operator after finding out the modulus by dividing the left operand with the right one.

Syntax:

x %= y

Example: Showing the implementation of modulus and assignment operator

Assignment Operators in Python

Output:

Assignment Operators in Python

Divide (floor) and Assignment:

The utility of this operator is to assign the result to the operand present on the left side of the operator after finding out the floor value by dividing the left operand with the right one.

Syntax:

x //= y

Example: Showing the implementation of divide(floor) and assignment operator

Assignment Operators in Python

Output:

Assignment Operators in Python

Exponent and Assignment:

The utility of this operator is to assign the result to the operand present on the left side of the operator after calculating the exponent by raising the power to the left operand.

Syntax:

x **= y

Example: Showing the implementation of exponent and assign operator

Assignment Operators in Python

Output:

Assignment Operators in Python

Bitwise AND and Assignment:

The utility of this operator is to assign the result to the operand present on the left side of the operator after calculating the Bitwise AND.

Syntax:

x &= y

Example: Showing the implementation of Bitwise add and assignment operator

Assignment Operators in Python

Output:

Assignment Operators in Python

Bitwise OR and Assignment:

The utility of this operator is to assign the result to the operand present on the left side of the operator after calculating the Bitwise OR.

Syntax:

x |= y

Example: Showing the implementation of bitwise OR and assignment operator

Assignment Operators in Python

Output:

Assignment Operators in Python

Bitwise XOR and Assignment:

The utility of this operator is to assign the result to the operand present on the left side of the operator after calculating the Bitwise XOR.

Syntax:

x ^= y

Example: Showing the implementation of bitwise XOR and assign operator

Assignment Operators in Python

Output:

Assignment Operators in Python

Bitwise Right Shift and Assignment:

The utility of this operator is to assign the result to the operand present on the left side of the operator after calculating the Bitwise Right Shift.

Syntax:

x >>= y

Example: Showing the implementation of bitwise right shift and assign operator

Assignment Operators in Python

Output:

Assignment Operators in Python

Bitwise Left Shift and Assignment:

The utility of this operator is to assign the result to the operand present on the left side of the operator after calculating the Bitwise Left shift.

Syntax:

x <<= y

Example: Showing the implementation of bitwise left shift and assign operator

Assignment Operators in Python

Output:

Assignment Operators in Python

Related Topics

Write a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

5 minutes read.

Python TCP Server

The TCP server library is also known as the socket library. Socket programming is the method of connecting two nodes through a network to form communication between two nodes. In...

3 minutes read.

How to import numy in python

How to Install NumPy in Python Python is a vast ocean of libraries, modules, and different functions. It has a solution for almost everything. Using Python, we can simplify even a...

3 minutes read.

Python Stack

Python Stack: The work Stack is defined as arranging a pile of objects or items on top of another. It is the same method of allocating memory in the stack...

10 minutes read.

Python while loop

Loops are essential in Python or any other programming language, as they help to execute a block of code repetitively. Sometimes, situations arise where you would need to use a piece of code...

2 minutes read.

Python Web Development projects

What is Python? Python is currently one of the most widely used computer programming languages. This isn't just an exaggeration; Python is the second-most famous programming language on the software development...

3 minutes read.

Python Set remove() method

Python Set remove() method The set.remove() method in Python removes the specified element from the set if it is present in the set else this method will raise an error if the specified item does...

2 minutes read.

Python Comment Block

In this tutorial, we will see what comment blocks mean in Python. Further, we will see the commenting methods supported in Python. We will understand the topics deeply with the...

6 minutes read.

Python System Requirements

Introduction As we know, Python is a popular programming language usually used to write scripts for operating systems. It’s handy adequate for utilizing in web development and application design. In this article,...

2 minutes read.

Python Line Break

Introduction In this tutorial, you will learn line breaks in python.In Python, the new line character is used to indicate the start of a new line and the end of an...

5 minutes read.

How to Concatenate Two Strings in Python

How to Concatenate Two Strings in Python Like the other data types, operations on strings are quite useful when we deal with some real-life applications. Here we will talk about a simple...

4 minutes read.

Subprocess in Python

In this tutorial, we will understand what is a subprocess in python and will understand how to use it. Subprocess A subprocess is a very prevailing portion of the python library. It...

3 minutes read.

Uint8 Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

How to create a file in Python?

How to create a file in python? Files provide us a convenient way of storing our information. Once we have our data, the next task is to store it so that...

3 minutes read.

Python datetime

Python provides a module named datetime to work with the date and time. Sometimes in the real life application development, we need to work with the date and time. The date is...

3 minutes read.

Cx_Oracle Python with Example

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python float()

Python float() class The float() class in Python returns a floating point number constructed from a number or string x. Syntax class float([x]) Parameter x: This parameter represents a number or a string that can be converted...

1 minute read.

Time. Sleep() in Python

Python time sleep () function suspends execution for a certain seconds given by user. Time sleep () syntax: Sleep(seconds) Limitations: The number of seconds to be suspends the code as per its requirements. Returns: Void The execution...

3 minutes read.

Python String count() method

Python String count() method The string.count() method returns the number of times a specified value appears in the string. Syntax string.count(sub[, start[, end]]) Parameter sub: This parameter represents a substring to be searched. start: This parameter...

1 minute read.

Python Support

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.