×

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

How to handle exceptions in python

Prerequisite: Exceptions and errors in Python What are Exceptions? Exceptions are run-time errors that unexpectedly occur and crash a program. When occurred, it alters the flow of the program. These errors are...

9 minutes read.

Add in Dictionary Python

Dictionary in python: Dictionaries are a useful data configuration for storing information in Python because they can mimic real-world data arrangements where a particular value exists for a particular key. The...

4 minutes read.

Python Modules

Python Modules The Python module is a collection of definition and statements. It can contain functions, classes, and variables.  Combining the related code into a module makes the code easier to understand and use....

5 minutes read.

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.

List Iteration in Python

In this tutorial, we will learn how to iterate list in Python. List in Python A list is an ordered group of values which includes several kinds of values.A list is a mutable...

3 minutes read.

Check if the directory exists in Python

To prevent any error, while loading or editing a file, we first have to check that the directory or the file exists or not. This is also done to prevent...

5 minutes read.

Python MongoDB Tutorial

An Introduction to MongoDB MongoDB is a document-oriented database application. It is an Open-source and platform-independent program. MongoDB is similar to few NoSQL databases that store the data in the documents...

17 minutes read.

OS Module in Python

What is a module? The Modules are the kind of files that contains python statements and definitions. The module is known by the name of the file followed by the suffix...

8 minutes read.

What does the if __name__ == "__main__" do in Python

In this article, you will learn about the If__name__==__main__ is a statement in python to define modules and the names of the modules. This statement plays a vital role in...

3 minutes read.

Problem-solving with algorithm and data structures using Python

What is problem-solving? There is no universal method for solving problems. It's frequently a special process that balances your immediate and long-term goals with your available resources. However, several models emphasise...

3 minutes read.

Python Tkinter Tutorial

What is Tkinter? Tkinter is GUI library of Python. When we integrated Tinkter with Python, it provides an easy and quick way to develop GUI applications. It provides the Tk GUI...

14 minutes read.

Python Algorithms

A quote goes, "A goal without a plan is just a wish." To achieve anything in life, we can't simply go for it without making a plan and sticking to...

4 minutes read.

Python String rfind() method

The string. rfind() method in Python returns the highest index in the string if the substring sub is found else it returns -1 if the substring is not found. Syntax string.rfind(sub [,start [,end]]) Parameter sub: This parameter...

2 minutes read.

Loan Calculator using PyQt5 in Python

In the following tutorial, we will learn how to build a Loan Calculator application using the PyQt5 library in the Python programming language. So, let's get started. Introduction to the code: The heading...

4 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.

Merge Sort using Python

Merge Sort is a technique that is used for sorting elements in an array using a special method known as divide and conquer. It is the best example of the...

5 minutes read.

Python String upper() method

Python String upper() method The string.upper() method in Python returns a copy of the string converted to uppercase. Syntax string.upper() Parameter NA Return This method returns a copy of the string converted to uppercase. Example 1 # Python...

1 minute read.

Python Boolean

In this article, you will learn the boolean variables in python, bool() function in python, and bool operators with examples, Boolean Objects in Python. There are the only two possible values...

6 minutes read.

Python lambda() Function

In this tutorial, we will learn about a new concept known as the Lambda function. It is a relatively new concept while learning Python. Python lambda functions are anonymous functions. It...

3 minutes read.

Python Strong Number

Strong Number- Logic Divide each of the number's digits into separate units to determine if it is a strong number or not.The factorial of each digit must then be determined. The...

5 minutes read.