Open In App

Assignment Operators in Python

Last Updated : 02 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Python Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic, logical, and bitwise computations. The value the operator operates on is known as the Operand. Here, we will cover Different Assignment operators in Python.

Operators

Sign

Description

Syntax

Assignment Operator

=

Assign the value of the right side of the expression to the left side operandc = a + b 

Addition Assignment Operator

+=

Add right side operand with left side operand and then assign the result to left operanda += b   

Subtraction Assignment Operator

-=

Subtract right side operand from left side operand and then assign the result to left operanda -= b  

Multiplication Assignment Operator

*=

Multiply right operand with left operand and then assign the result to the left operanda *= b     

Division Assignment Operator

/=

Divide left operand with right operand and then assign the result to the left operanda /= b

Modulus Assignment Operator

%=

Divides the left operand with the right operand and then assign the remainder to the left operanda %= b  

Floor Division Assignment Operator

//=

Divide left operand with right operand and then assign the value(floor) to left operanda //= b   

Exponentiation Assignment Operator

**=

Calculate exponent(raise power) value using operands and then assign the result to left operanda **= b     

Bitwise AND Assignment Operator

&=

Performs Bitwise AND on operands and assign the result to left operanda &= b   

Bitwise OR Assignment Operator

|=

Performs Bitwise OR on operands and assign the value to left operanda |= b    

Bitwise XOR Assignment Operator

^=

Performs Bitwise XOR on operands and assign the value to left operanda ^= b    

Bitwise Right Shift Assignment Operator

>>=

Performs Bitwise right shift on operands and assign the result to left operanda >>= b     


Bitwise Left Shift Assignment Operator

<<=

Performs Bitwise left shift on operands and assign the result to left operanda <<= b 

Walrus Operator

:=

Assign a value to a variable within an expression

a := exp

Here are the Assignment Operators in Python with examples.

Assignment Operator

Assignment Operators are used to assign values to variables. This operator is used to assign the value of the right side of the expression to the left side operand.

Python
# Assigning values using 
# Assignment Operator 
a = 3
b = 5

c = a + b 

# Output 
print(c)

Output

8

Addition Assignment Operator

The Addition Assignment Operator is used to add the right-hand side operand with the left-hand side operand and then assigning the result to the left operand.

Syntax: a += b

Example: In this code we have two variables ‘a’ and ‘b’ and assigned them with some integer value. Then we have used the addition assignment operator which will first perform the addition operation and then assign the result to the variable on the left-hand side.

Python
a = 3
b = 5

# a = a + b
a += b

# Output
print(a)

Output:

8

Subtraction Assignment Operator

The Subtraction Assignment Operator is used to subtract the right-hand side operand from the left-hand side operand and then assigning the result to the left-hand side operand.

Syntax: a -= b

Example: In this code we have two variables ‘a’ and ‘b’ and assigned them with some integer value. Then we have used the subtraction assignment operator which will first perform the subtraction operation and then assign the result to the variable on the left-hand side.

Python
a = 3
b = 5

# a = a - b
a -= b

# Output
print(a)

Output:

-2

Multiplication Assignment Operator

The Multiplication Assignment Operator is used to multiply the right-hand side operand with the left-hand side operand and then assigning the result to the left-hand side operand.

Syntax: a *= b

Example: In this code we have two variables ‘a’ and ‘b’ and assigned them with some integer value. Then we have used the multiplication assignment operator which will first perform the multiplication operation and then assign the result to the variable on the left-hand side.

Python
a = 3
b = 5

# a = a * b
a *= b

# Output
print(a)

Output:

15

Division Assignment Operator

The Division Assignment Operator is used to divide the left-hand side operand with the right-hand side operand and then assigning the result to the left operand.

Syntax: a /= b

Example: In this code we have two variables ‘a’ and ‘b’ and assigned them with some integer value. Then we have used the division assignment operator which will first perform the division operation and then assign the result to the variable on the left-hand side.

Python
a = 3
b = 5

# a = a / b
a /= b

# Output
print(a)

Output:

0.6

Modulus Assignment Operator

The Modulus Assignment Operator is used to take the modulus, that is, it first divides the operands and then takes the remainder and assigns it to the left operand.

Syntax: a %= b

Example: In this code we have two variables ‘a’ and ‘b’ and assigned them with some integer value. Then we have used the modulus assignment operator which will first perform the modulus operation and then assign the result to the variable on the left-hand side.

Python
a = 3
b = 5

# a = a % b
a %= b

# Output
print(a)

Output:

3

Floor Division Assignment Operator

The Floor Division Assignment Operator is used to divide the left operand with the right operand and then assigs the result(floor value) to the left operand.

Syntax: a //= b

Example: In this code we have two variables ‘a’ and ‘b’ and assigned them with some integer value. Then we have used the floor division assignment operator which will first perform the floor division operation and then assign the result to the variable on the left-hand side.

Python
a = 3
b = 5

# a = a // b
a //= b

# Output
print(a)

Output:

0

Exponentiation Assignment Operator

The Exponentiation Assignment Operator is used to calculate the exponent(raise power) value using operands and then assigning the result to the left operand.

Syntax: a **= b

Example: In this code we have two variables ‘a’ and ‘b’ and assigned them with some integer value. Then we have used the exponentiation assignment operator which will first perform exponent operation and then assign the result to the variable on the left-hand side.

Python
a = 3
b = 5

# a = a ** b
a **= b

# Output
print(a)

Output:

243

Bitwise AND Assignment Operator

The Bitwise AND Assignment Operator is used to perform Bitwise AND operation on both operands and then assigning the result to the left operand.

Syntax: a &= b

Example: In this code we have two variables ‘a’ and ‘b’ and assigned them with some integer value. Then we have used the bitwise AND assignment operator which will first perform Bitwise AND operation and then assign the result to the variable on the left-hand side.

Python
a = 3
b = 5

# a = a & b
a &= b

# Output
print(a)

Output:

1

Bitwise OR Assignment Operator

The Bitwise OR Assignment Operator is used to perform Bitwise OR operation on the operands and then assigning result to the left operand.

Syntax: a |= b

Example: In this code we have two variables ‘a’ and ‘b’ and assigned them with some integer value. Then we have used the bitwise OR assignment operator which will first perform bitwise OR operation and then assign the result to the variable on the left-hand side.

Python
a = 3
b = 5

# a = a | b
a |= b

# Output
print(a)

Output:

7

Bitwise XOR Assignment Operator 

The Bitwise XOR Assignment Operator is used to perform Bitwise XOR operation on the operands and then assigning result to the left operand.

Syntax: a ^= b

Example: In this code we have two variables ‘a’ and ‘b’ and assigned them with some integer value. Then we have used the bitwise XOR assignment operator which will first perform bitwise XOR operation and then assign the result to the variable on the left-hand side.

Python
a = 3
b = 5

# a = a ^ b
a ^= b

# Output
print(a)

Output:

6

Bitwise Right Shift Assignment Operator

The Bitwise Right Shift Assignment Operator is used to perform Bitwise Right Shift Operation on the operands and then assign result to the left operand.

Syntax: a >>= b

Example: In this code we have two variables ‘a’ and ‘b’ and assigned them with some integer value. Then we have used the bitwise right shift assignment operator which will first perform bitwise right shift operation and then assign the result to the variable on the left-hand side.

Python
a = 3
b = 5

# a = a >> b
a >>= b

# Output
print(a)

Output:

0

Bitwise Left Shift Assignment Operator

The Bitwise Left Shift Assignment Operator is used to perform Bitwise Left Shift Opertator on the operands and then assign result to the left operand.

Syntax: a <<= b

Example: In this code we have two variables ‘a’ and ‘b’ and assigned them with some integer value. Then we have used the bitwise left shift assignment operator which will first perform bitwise left shift operation and then assign the result to the variable on the left-hand side.

Python
a = 3
b = 5

# a = a << b
a <<= b

# Output
print(a)

Output:

96

Walrus Operator

The Walrus Operator in Python is a new assignment operator which is introduced in Python version 3.8 and higher. This operator is used to assign a value to a variable within an expression.

Syntax: a := expression

Example: In this code, we have a Python list of integers. We have used Python Walrus assignment operator within the Python while loop. The operator will solve the expression on the right-hand side and assign the value to the left-hand side operand ‘x’ and then execute the remaining code.

Python
# a list
a = [1, 2, 3, 4, 5]

# walrus operator
while(x := len(a)) > 2:
    a.pop()
    print(x)

Output:

5
4
3


Previous Article
Next Article

Similar Reads

Augmented Assignment Operators in Python
An assignment operator is an operator that is used to assign some value to a variable. Like normally in Python, we write "a = 5" to assign value 5 to variable 'a'. Augmented assignment operators have a special role to play in Python programming. It basically combines the functioning of the arithmetic or bitwise operator with the assignment operator
4 min read
Increment += and Decrement -= Assignment Operators in Python
If you're familiar with Python, you would have known Increment and Decrement operators ( both pre and post) are not allowed in it. Python is designed to be consistent and readable. One common error by a novice programmer in languages with ++ and -- operators are mixing up the differences (both in precedence and in return value) between pre and post
3 min read
A += B Assignment Riddle in Python
Predict the output of these two expressions on Python console On console Geek = (1, 2, [8, 9]) Geek[2] += [3, 4] Output: Explanation: Look at the bytecode Python generates for the expression s[a] += b. It becomes clear how that happens. It works step by step Put the value of s[a] on Top Of Stack(TOS). Perform TOS += b. This succeeds, If TOS refers
1 min read
Python | Priority key assignment in dictionary
Sometimes, while working with dictionaries, we have an application in which we need to assign a variable with a single value that would be from any of given keys, whichever occurs first in priority. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop This task can be performed in a brute force manner using loop. I
4 min read
Python - Maximum value assignment in Nested Dictionary
Sometimes, while working with Python dictionaries, we can have a problem in which we need to assign to the outer key, the item with maximum value in inner keys. This kind of problem can occur in day-day programming and web development domains. Let's discuss a way in which this task can be performed. Input : test_dict = {'Manjeet': {'English': 19, '
3 min read
Different Forms of Assignment Statements in Python
We use Python assignment statements to assign objects to names. The target of an assignment statement is written on the left side of the equal sign (=), and the object on the right can be an arbitrary expression that computes an object. There are some important properties of assignment in Python :- Assignment creates object references instead of co
3 min read
Python - Sequence Assignment to Words
Given a String of words, assign an index to each word. Input : test_str = 'geeksforgeeks is best' Output : {0: 'geeksforgeeks', 1: 'is', 2: 'best'} Explanation : Index assigned to each word. Input : test_str = 'geeksforgeeks best' Output : {0: 'geeksforgeeks', 1: 'best'} Explanation : Index assigned to each word. Method #1: Using enumerate() + dict
5 min read
Python - Minimum value key assignment
Given two dictionaries, the task is to write a Python program to assign minimum values for matching keys from both dictionaries. Examples: Input : test_dict1 = {"gfg" : 1, "is" : 7, "best" : 8}, test_dict2 = {"gfg" : 2, "is" : 2, "best" : 10}Output : {"gfg" : 1, "is" : 2, "best" : 8}Explanation : Minimum of 1 and 2 is 1, hence, gfg is assigned with
5 min read
Python Indexerror: list assignment index out of range Solution
In python, lists are mutable as the elements of a list can be modified. But if you try to modify a value whose index is greater than or equal to the length of the list then you will encounter an Indexerror: list assignment index out of range. Python Indexerror: list assignment index out of range ExampleIf ‘fruits’ is a list, fruits=[‘Apple’,’ Banan
3 min read
How to Fix - UnboundLocalError: Local variable Referenced Before Assignment in Python
Developers often encounter the UnboundLocalError Local Variable Referenced Before Assignment error in Python. In this article, we will see what is local variable referenced before assignment error in Python and how to fix it by using different approaches. What is UnboundLocalError: Local variable Referenced Before Assignment?This error occurs when
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg