Join Our 4-Week Free Gen AI Course with select Programs.

Request a callback

or Chat with us on

What is an Assignment Operator in Python, and How do I Use It?

Basics of Python
Basics of Python
icon
5 Hrs. duration
icon
9 Modules
icon
1800+ Learners
logo
Start Learning

In this article, we will learn about the assignment operator in the Python language. This operator assigns values to variables. Python language has different types of types of operators like addition, subtraction, multiplication and so on.  For example, ‘ x= 15’.

What is an Assignment Operator in Python?

An assignment operator is a symbol or combination of symbols used to assign a value to a variable in Python language. The most common assignment operator is the equals sign ‘=’  for example

 

x = 5

 

This line assigns 5 values in the x variable.

Assignment Operator

The assignment operator is used to assign value to the variable. This operator is used to assign the value of the right side of the expression to the left-hand side operand. The following program demonstrates the assignment operator example.

 

Program

 

# Assigning values using # Assignment Operator a = 300 b = 500 c = a + b # Output print(c)

 

Output

 

800

Addition Assignment Operator

This operator is used to add the right-hand side operand and left-hand side operands with the left-hand side operand. The result is stored in the left-hand operand.

 

Syntax:

 

a += b or a = a+b

 

Example:  In this code, we have two variables, ‘a’ and ‘b’ we will assign some integer value to those variables. Then, we used the addition assignment operator to add those values

 

The following program demonstrates the addition assignment operator.

 

Program

 

a = 3 b = 5 # a = a + b a += b # Output print(a)

 

Output

 

8

 

Subtraction Assignment Operator

This operator is very similar to the addition assignment operator. However, this operator subtracts the value from the left-hand side operand and then assigns the result to the left-hand side operand. 

 

Syntax

 

a -= b

 

Example: In this example, we have two variables, ‘a’ and ‘b’ and assign some integer value. Then, we subtract the lowest value from the highest value. Then assign the value on the left-hand side variable.

 

The following program demonstrates the subtraction assignment operator.

 

Program

 

a = 103 b = 34 # a = a – b a -= b # Output print(a)

 

Output

 

69

Multiplication Assignment Operator

 

The multiplication assignment operator multiplies the right-hand side operand and assigns the result to the left-hand side operand.

 

Syntax:

 

a *= b

 

Example: In this program, we have two variables, “a” and “b”, and we will assign them some integer value on those variables. Then, we will multiply with the multiplication assignment operator. Then the result will be stored in the left-hand side operator.

 

The following program demonstrates the multiplication assignment operator.

 

Program

 

a = 3 b = 5 # a = a * b a *= b # Output print(a)

 

Output

 

15

Division Assignment Operator

The division operator divides the left-hand side operand by the right-hand side operator and then assigns the value to the left-hand side operator. 

 

Syntax:

 

a /= b

 

Example:  In this example, we have two variables, ‘a’ and ‘b’ and assign them with some integer value.  We can divide those variables using the division assignment operator. After division, we will assign that value to the left-hand side operator.

 

Syntax

 

a /= b

 

The following program demonstrates the Division Assignment Operator 

 

Program

 

a = 30 b = 20 # a = a / b a /= b # Output print(a)

 

Output

 

1.5

 

Modulus Assignment Operator

 

The operator is used to take the modules. That is, it first divides the operands and then assigns the remainder to the left-hand operand. 

 

Syntax:

 

a %= b

 

Example: In this example, we have two variables, ‘a’ and ‘b’, assigned with some integer value like 5 or 10. Then, we will use those values with the module operator. The result will be stored in the left hand side operand.

 

The following program demonstrates the module’s assignment operator.

 

Program

 

a = 40 b = 20 # a = a % b a %= b # Output print(a)

 

Output

 

0

 

Floor Division Assignment Operator

The floor division operator is used to divide the two numbers, left-hand operand and right-hand operand and then the result will be stored in the left-hand side operand. 

 

Syntax

<b> a //= b </b>

 

Example: In this example, we have two variables, ‘a’ and ‘b’ and assign them with some integer value. Then, we will use the floor division assignment operator within the operand after we store the result of the floor division left-hand side operand. The following program demonstrates the Floor Division Assignment Operator:

 

Program

 

a = 50 b = 20 # a = a // b a //= b # Output print(a)

 

Output

 

2

Exponentiation Assignment Operator

 

The exponentiation assignment operator in Python raises a variable to power and assigns the result to the left-hand variable. 

 

Syntax

 

a **= b

 

 

Example: In this program, we have two variables, “a” and “b”, and assign some integer value. Then, we have to assign them with some integer value. Then, will perform the exponentiation operator on those variables. Then, we will store the result into the left hand side operator.

 

Program

 

a = 5 b = 34 # a = a ** b a **= b # Output print(a)

 

Output

 

582076609134674072265625

 

DevOps & Cloud Engineering
Internship Assurance
DevOps & Cloud Engineering

Bitwise AND Assignment Operator 

In this section, we will discuss the bitwise and assignment operators in Python. This operator performs the bitwise operation on both operands and then assigns the result to the left-hand side operand.

 

Syntax

 

a &= b

 

Example: In this program, we have two variables, “a” and “b.” We will assign them an integer value. Then, we will use bitwise and operator after getting the result will be stored in the left hand side bitwise operator.

 

The following program demonstrates the bitwise and assignment operator

 

Program

 

a = 23 b = 30 # a = a & b a &= b # Output print(a)

 

Output

 

22

Bitwise OR Assignment Operator

The bitwise or assignment operator is used to perform bitwise or on the operands. After getting the result, it will be stored in the left-hand side operand.

 

Syntax

a |= b

 

Example: In this example, we have two variables, “a “ and “b”, then we will perform the bitwise operator after storing the result to the left-hand side operator.

 

The following program demonstrates the bitwise or Assignment Operator

 

Program

 

a = 3435 b = 343 # a = a | b a |= b # Output print(a)

 

Output

 

3455

Bitwise XOR Assignment Operator

The bitwise XOR Assignment Operator performs an XOR operation on the operands and then  assigns those values to the left-hand result operand.

 

Syntax

 

a ^= b

 

Example: In this example, we have two variables, “a” and “b”. we will assign them some integer value. Then, we will use the XOR Assignment Operator after the result is stored on the left-hand side operator.

 

Program

 

a = 3 b = 334 # a = a ^ b a ^= b # Output print(a)

 

Output

 

333

 

Bitwise Right Shift Assignment Operator

 

This operator performs bitwise right-shift Operations on the operands. The result will be stored on the left-hand side operand.

 

Syntax:

 

a >>= b

 

Example: In the following program, we have two variables, “a” and “b,” and assign them an integer value. Then, we perform the Bitwise Right shift operation on those operands. The result will be stored on the left-hand side operand. The following program demonstrates the Bitwise Right Shift Assignment Operator:

 

Program

 

a = 34 b = 343 # 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 operations on the operands. The result is stored on the left-hand side operand.

 

Syntax

 

a <<= b

 

Example: In this example, we have two variables, “a” and “b”. In those variables, we will assign them integer values. Then, we used those operands with a bitwise left shift assignment operator. After getting the result will be stored on the left-hand side operator.

 

Program

 

a = 3 b = 5 # a = a << b a <<= b # Output print(a)

 

Output

 

96

 

Walrus Operator

The Walrus Operator is a new assignment operator in Python language version 3.8. It assigns a value to a variable within an expression.

 

Syntax

 

a := expression

 

Example: In this program, we have a list of integers in Python language. This operator is used within the while loop. After solving the expression using the walrus operator then, we will store those results in the left-hand side operand. 

 

Program

# a list a = [51, 32, 43, 24, 345] # walrus operator while(x := len(a)) > 2: a.pop() print(x)

 

Output

 

5 4 3

Conclusion

 

In this article, we learned about the assignment operator in the Python language. The assignment operator, denoted by the equals sign (=), is a fundamental tool for assigning values to variables. It allows programmers to store data in computer memory locations. The assignment operator is the backbone of variable initialization and reassignment. By understanding its syntax and usage, Python developers can easily build robust and effective applications in the meantime.

FAQs
Yes, we can assign the same value to multiple variables in a single line of code, for example, ‘x = y = z = 10’
Yes, we can assign the return value of a function directly to a variable. For example “ result  = my_function_name()”.
Yes, we can swap the values of two variables using the temporary variable or by using tuple unpacking. For example, ‘a,b = b,a’
Yes, we can assign the variable conditionally in Python language using the ternary operators to assign values to variables based on certain conditions‘. For example, ‘ x= 10 if conditions else 20’
If you try to assign a value, that variable has not been declared yet. Then Python will give you a NameError.

Deploying Applications Over the Cloud Using Jenkins

Prashant Kumar Dey

Prashant Kumar Dey

Associate Program Director - Hero Vired

Ex BMW | Google

24 October, 7:00 PM (IST)

Limited Seats Left

Book a Free Live Class

left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.
Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

|

Sitemap

© 2024 Hero Vired. All rights reserved