×

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

Python Compiler

What is Compiler? The compiler is mainly a program used to convert the source code into the machine or binary code. The source code is generally a computer program written using...

6 minutes read.

How to Pass a list as an Argument in Python

Lists in Python A list is a datatype in python which is used to store the data in a sequence. The lists are mutable; that is, we can change the values...

3 minutes read.

Python Parser

Introduction : Parsing is referred to as the processing and translation of a Python program into machine language. In general, we could indeed say that the command parse is used to...

4 minutes read.

Socket Programming in Python

Socket Programming in Python In this tutorial, we will discuss network programming using Python programming language. We will explore all basic concept of network with Python script. Network Services in Python Python has...

9 minutes read.

Python Infinity

Introduction We will discover how to define an infinite number in Python in this tutorial. Infinity, as we all know, is a value that cannot be defined and can either be...

5 minutes read.

Calculator Program in Python

Simple Calculator Program in Python This example helps to learn how to create a simple calculator program to perform operations like addition, subtraction, multiplication, and division. Source Code The following is the source...

2 minutes read.

Salary of Python Developers in India

In this tutorial, we will understand who python developers are and what is their salary when they work in India. Python Developers Python Developers are the people who are involved in designing...

4 minutes read.

How to run a Python file in CMD

Introduction Today, let us learn about how to run a Python file using cmd. Cmd is nothing but command prompt. So first let us know how to run a Python code...

4 minutes read.

OSError in 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...

4 minutes read.

Inheritance in Python

Inheritance in Python Object-Oriented Programming provides reusable patterns to the code for restricting the redundancy in development projects. One of the basic principles of Object-Oriented Programming that helps achieve recyclable code...

8 minutes read.

Python MySQL Delete Operation

Python MySQL Delete Operation: Like the update operation where we were updating required field from a SQL table, we can also delete an entry from the table which we have...

4 minutes read.

Data Distribution in python

Before discussing data distribution, we will discuss each word that is data and Distribution. What is meant by data? A collection of raw facts and figures is called data. The word "raw"...

18 minutes read.

How to Open a File in Python with a Path?

File in Python File handling is an important operation; it allows the programmer to access the data in the files, such as text, binary values and excel sheets. The CSV files...

7 minutes read.

Python int()

Python int() class The int() class in Python returns an integer object constructed from a number or string x. Syntax class int(x, base=10) Parameter x: This parameter represents a number or a string that can be converted into...

1 minute read.

Writing to Excel using Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

3 minutes read.

Application to Search Installed Application using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Latest Project Ideas using Python 2024

Python is one of the well-known languages for programming in the contemporary domain of computer science. The demand to adopt Python for IT purposes is steadily increasing because of its...

7 minutes read.

Mrcnn Python

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.

Accuracy_score Function in Sklearn

A crucial stage in data science is measuring our model's performance using the appropriate metric. In this article, we will examine two methods for calculating the accuracy of your predictions:...

13 minutes read.

Applications of Python

Top 10 Applications of Python in 2020- 2021 Python language is famous for its general-purpose nature, which enables developers to use it in every field of development. Python can be found...

6 minutes read.