View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

What are Assignment Operators in Python?

Updated on 04/06/20253,818 Views

In Python, assignment operators play a vital role in assigning values to variables. These operators not only store values but also perform operations before storing them. They combine arithmetic or bitwise operations with assignment, making the code more concise and efficient. In simple terms, an assignment operator updates the value of a variable based on the operation performed.

This article will guide you through the types of assignment operators in Python with detailed explanations, practical examples, and outputs. You will learn how each operator works, from basic arithmetic assignments like += to bitwise operations like &= and >>=.

Pursue software engineering & development courses from top universities!

What are Assignment Operators in Python?

In Python programming, assignment operators are used to assign values to variables. They store the result of an expression or a value directly into a variable. These operators simplify the code by combining operations with assignments.

The most basic assignment operator is the equal sign (=). It assigns the value on the right side of the expression to the variable on the left side. Python also supports compound assignment operators like +=, -=, *=, and more, which perform an operation and assign the result in a single step.

Read the Python Operators: Types, Syntax, and Implementation Guide to enhance your coding productivity.

Let’s look at a simple example to understand how assignment operators work in Python.

# Assigning an integer value to a variable
x = 10 # Basic assignment

# Using addition assignment operator
x += 5 # Equivalent to x = x + 5

# Display the result
print("The value of x is:", x)

Output:

The value of x is: 15

Example:

  • x = 10 assigns the value 10 to the variable x.
  • x += 5 increases the current value of x by 5. Internally, it performs x = x + 5.
  • Finally, print() outputs the updated value of x, which is 15.

Unlock a career at Big Four with the following courses: 

Types of Assignment Operators in Python

Below is a summary table listing each assignment operator in Python, along with its name, syntax, and description.

Operator

Name

Syntax

Description

=

Assignment Operator

a = b

This operator assigns the value on the right to the left operand.

+=

Addition Assignment Operator

a += b

This operator adds the left and right operands and then assigns the result to the left operand

-=

Subtraction Assignment Operator

a -= b

It subtracts the right operand from the left and then assigns the result to the left operand.

*=

Multiplication Assignment Operator

a *= b

This operator multiplies the left and right operands and then assigns the result to the left operand

/=

Division Assignment Operator

a /= b

This operator divides the left operand by the right and then assigns the result to the left operand.

%=

Modulus Assignment Operator

a %= b

It finds the remainder of the division and assigns it to the left operand.

**=

Exponentiation Assignment Operator

a **= b

This operator raises the left operand to the power of the right and assigns the result to the left operand.

//=

Floor Division Assignment Operator

a //= b

This operator performs floor division and assigns the integer quotient to the left operand.

&=

Bitwise AND Assignment Operator

a &= b

It performs the bitwise AND on the left and right operands and then assigns the result to the left operand.

|=

Bitwise OR Assignment Operator

a=a|b

This operator performs the bitwise OR operation on left and right operands and assigns the result to the left operand.

^=

Bitwise XOR Assignment Operator

a ^= b

This operator performs the bitwise XOR on the left and right operands and then assigns the result to the left operand.

>>=

Bitwise Right Shift Assignment Operator

a >>= b

Itr right-shifts the left operand by the number of bits specified by the right operand and then assigns the result to the left operand.

<<=

Bitwise Left Shift Assignment Operator

a <<= b

This operator left-shifts the left operand by the number of bits specified by the right operand and then assigns the result to the left operand.

The first 7 are arithmetic-related (arithmetic operators) and generally easier to understand, while the last 5 are bitwise operators

Let’s explore each of these assignment operators in Python in detail.

Addition Assignment Operator

The addition assignment operator += is one of the most commonly used assignment operators in Python. It adds the value of the right operand to the left operand and then assigns the result back to the left operand. This operator simplifies code by combining addition and assignment into a single expression.

Let’s see how this operator works with a simple example:

# Initialize variable a with value 10
a = 10

# Add 5 to a using the addition assignment operator
a += 5

# Print the updated value of a
print(a)

Output:

15

Explanation:

  • Initially, a holds the value 10.
  • The expression a += 5 adds 5 to the current value of a (which is 10).
  • Then, the result 15 is assigned back to a.
  • Finally, print(a) outputs the updated value, which is 15.

This operator is helpful when you want to increment a variable’s value without writing the variable name twice. It improves code readability and reduces errors.

Subtraction Assignment Operator

The subtraction assignment operator -= subtracts the value of the right operand from the left operand. Then, it assigns the result back to the left operand. This operator helps simplify code when decreasing a variable’s value.

Let’s understand this with a practical example:

# Initialize variable b with value 20
b = 20

# Subtract 7 from b using the subtraction assignment operator
b -= 7

# Print the updated value of b
print(b)

Output:

13

Explanation:

  • Initially, b has the value 20.
  • The expression b -= 7 subtracts 7 from b’s current value.
  • The result, 13, is assigned back to b.
  • When print(b) runs, it shows the updated value 13.

Using -= makes your code more concise. It avoids repeating the variable name and clearly expresses the intent to subtract and update the variable in one step.

Must explore the Precedence of Operators in Python article!

Multiplication Assignment Operator

The multiplication assignment operator *= multiplies the left operand by the right operand. Then, it assigns the resulting value back to the left operand. This operator helps you update a variable’s value by multiplying it efficiently.

Check out this simple example to see how it works:

# Initialize variable c with value 6
c = 6

# Multiply c by 4 using the multiplication assignment operator
c *= 4

# Print the updated value of c
print(c)

Output:

24

Explanation:

  • The variable c starts with the value 6.
  • The expression c *= 4 multiplies c by 4.
  • The result, 24, is assigned back to c.
  • Printing c shows the new value, which is 24.

This operator reduces code length and keeps your intent clear. Instead of writing c = c * 4, you simply use c *= 4. It is very useful in loops and calculations where the variable is updated repeatedly.

Division Assignment Operator

The division assignment operator /= divides the left operand by the right operand. Then, it assigns the resulting value back to the left operand. This operator simplifies the process of updating a variable by dividing its current value.

Let's look at an example to understand its usage:

# Initialize variable c with value 6
c = 6

# Multiply c by 4 using the multiplication assignment operator
c *= 4

# Print the updated value of c
print(c)

Output:

6.0

Example:

  • The variable d is initially set to 30.
  • The expression d /= 5 divides d by 5.
  • The result, 6.0, is assigned back to d. Note that division in Python always results in a float.
  • The print(d) statement outputs the updated value, which is 6.0.

Using /= makes your code cleaner and easier to read. Instead of writing d = d / 5, you write d /= 5. It is very useful in calculations where you need to adjust the value by dividing it multiple times.

Also read about the Identity Operator in Python to improve your coding skills!

Modulus Assignment Operator

The modulus assignment operator %= divides the left operand by the right operand. Then, it assigns the remainder of this division back to the left operand. This operator is helpful when you want to update a variable with the remainder value.

Let's see a clear example to understand how %= works:

# Initialize variable e with value 17
e = 17

# Find remainder when e is divided by 5 using modulus assignment operator
e %= 5

# Print the updated value of e
print(e)

Output:

2

Explanation:

  • The variable e starts with the value 17.
  • The expression e %= 5 calculates the remainder of 17 divided by 5.
  • Since 17 divided by 5 is 3 with a remainder of 2, the value 2 is assigned back to e.
  • When print(e) is executed, it outputs 2.

Using the %= operator allows you to write more concise code instead of e = e % 5. It is especially useful in scenarios like cycling through indices or checking divisibility in loops.

Exponentiation Assignment Operator

The exponentiation assignment operator **= raises the left operand to the power of the right operand. It then assigns this calculated value back to the left operand. This operator simplifies updating a variable by exponentiation.

Let's explore this operator with a practical example:

# Initialize variable f with value 4
f = 4

# Raise f to the power of 3 using exponentiation assignment operator
f **= 3

# Print the updated value of f
print(f)

Output:

64

Explanation:

  • The variable f starts with the value 4.
  • The expression f **= 3 raises 4 to the power of 3 (4 × 4 × 4).
  • The result, 64, is assigned back to f.
  • When printed, the output shows 64.

Using the **= operator makes your code concise and easy to read. Instead of writing f = f ** 3, you can simply use f **= 3. This operator is very useful in mathematical and scientific computations.

Go through the Memory Management in Python article to speed up development time!

Floor Division Assignment Operator

The floor division assignment operator //= divides the left operand by the right operand. It then assigns the integer part (floor value) of the quotient back to the left operand. This operator is useful when you want the division result without the decimal part.

Let’s see how it works with an example:

# Initialize variable g with value 15
g = 15

# Perform floor division of g by 4 and assign the result back to g
g //= 4

# Print the updated value of g
print(g) #

Output:

3

Explanation:

  • Initially, g holds the value 15.
  • The expression g //= 4 divides 15 by 4 resulting in 3.75.
  • The floor division operator // takes only the integer part, which is 3.
  • This integer value 3 is then assigned back to g.
  • Printing g shows the value 3.

The floor division assignment operators in Python are helpful when you need integer division without rounding errors. They make the code cleaner by combining division and assignment in one step.

Bitwise AND Assignment Operator

The bitwise AND assignment operator &= performs a bitwise AND operation between the left and right operands. Then, it assigns the resulting value to the left operand. This operator is useful when you want to update a variable by keeping only the bits that are set in both operands.

Let’s understand this with an example:

# Initialize variable h with binary value 14 (decimal)
h = 14 # binary: 1110

# Perform bitwise AND with 10 (binary 1010) and assign the result back to h
h &= 10 # binary: 1010

# Print the updated value of h
print(h)

Output:

10

Explanation:

  • Initially, h is 14 which is 1110 in binary.
  • The expression h &= 10 applies bitwise AND between 1110 (14) and 1010 (10).
  • Bitwise AND compares each bit of both numbers and returns 1 only if both bits are 1.
  • The result of 1110 & 1010 is 1010 in binary, which equals 10 in decimal.
  • This value 10 is assigned back to h.
  • Printing h shows the updated value 10.

This assignment operator in Python is efficient for bit-level manipulation and can simplify code where bitwise filtering or masking is required.

Refer to the Inheritance in Python article to efficiently implement an important OOPS concept.

Bitwise OR Assignment Operator

The bitwise OR assignment operator |= performs a bitwise OR operation between the left and right operands. Then, it assigns the result back to the left operand. This operator helps to set bits in a number by combining the bits of both operands.

Let’s see an example to understand how it works:

# Initialize variable x with the decimal value 10 (binary 1010)
x = 10 # binary: 1010
# Perform bitwise OR with 4 (binary 0100) and assign the result to x
x |= 4 # binary: 0100
# Print the updated value of x
print(x)

Output:

14

Explanation:

  • Initially, x is 10, which is 1010 in binary.
  • The expression x |= 4 applies bitwise OR between 1010 (10) and 0100 (4).
  • Bitwise OR compares each bit of both numbers and returns 1 if either bit is 1.
  • The result of 1010 | 0100 is 1110 in binary, which equals 14 in decimal.
  • This new value 14 is assigned back to x.
  • Printing x displays the updated value 14.

This operator is useful when you want to enable certain bits without affecting others, often used in flags and bitmask operations.

Bitwise XOR Assignment Operator

The bitwise XOR assignment operator ^= applies a bitwise exclusive OR operation between the left and right operands. It then assigns the resulting value back to the left operand. This operator is useful when you want to toggle bits selectively.

Let’s examine an example to understand how it works:

# Initialize variable x with the decimal value 10 (binary 1010)
x = 10 # binary: 1010

# Perform bitwise XOR with 6 (binary 0110) and assign the result to x
x ^= 6 # binary: 0110

# Print the updated value of x
print(x)

Output:

12

Explanation:

  • Initially, x is 10, represented as 1010 in binary.
  • The expression x ^= 6 performs a bitwise XOR between 1010 (10) and 0110 (6).
  • Bitwise XOR returns 1 only if the corresponding bits differ; otherwise, it returns 0.
  • The result of 1010 ^ 0110 is 1100 in binary, which equals 12 in decimal.
  • The updated value 12 is then assigned back to x.
  • Printing x displays the new value 12.

This operator helps in toggling bits: if a bit is 1, XOR flips it to 0; if it is 0, XOR flips it to 1.

Read Comments in Python to write cleaner, modular code.

Bitwise Right Shift Assignment Operator

The bitwise right shift assignment operator >>= shifts the bits of the left operand to the right by the number of positions specified by the right operand. It then assigns the shifted value back to the left operand. This operator effectively divides the number by 2 raised to the power of the shift count, discarding any remainder.

Let's look at a practical example to see how this operator works:

# Initialize variable x with decimal value 40 (binary 101000)
x = 40 # binary: 101000

# Right shift bits of x by 3 positions and assign the result back to x
x >>= 3 # shift right by 3 bits

# Print the updated value of x
print(x)

Output:

5

Explanation:

  • Initially, x is 40, which is 101000 in binary.
  • The operator x >>= 3 shifts the bits of x to the right by 3 positions.
  • Shifting right by 3 bits removes the three least significant bits, effectively dividing 40 by 2^3 (which is 8).
  • Binary 101000 shifted right by 3 becomes 000101, which equals 5 in decimal.
  • The result 5 is assigned back to x.
  • Printing x outputs the updated value 5.

This operator is commonly used for efficient division by powers of two when working with binary data or low-level programming.

Also explore the String Split in Python article to develop efficient Python projects.

Bitwise Left Shift Assignment Operator

The bitwise left shift assignment operator <<= shifts the bits of the left operand to the left by the number of positions given by the right operand. Then, it assigns the shifted value back to the left operand. This operation effectively multiplies the number by 2 raised to the power of the shift count.

Let’s understand this operator with an example:

# Initialize variable y with decimal value 5 (binary 00000101)
y = 5 # binary: 00000101

# Left shift bits of y by 2 positions and assign the result back to y
y <<= 2 # shift left by 2 bits

# Print the updated value of y
print(y)

Output:

20

Explanation:

  • Initially, y is 5, which is 00000101 in binary.
  • The operator y <<= 2 shifts the bits of y to the left by 2 positions.
  • Shifting left by 2 bits adds two zeros at the right end, multiplying 5 by 2^2 (which is 4).
  • Binary 00000101 shifted left by 2 becomes 00010100, equal to 20 in decimal.
  • The new value 20 is assigned back to y.
  • Printing y outputs the updated value 20.

This operator is often used to perform fast multiplication by powers of two, especially in low-level programming or bit manipulation tasks.

Conclusion

Assignment Operators in Python are fundamental tools for updating variable values efficiently. They combine arithmetic and bitwise operations with assignment to simplify code writing. This leads to cleaner and more readable programs.

Knowing how to use different assignment operators in Python helps you write concise and effective code. Each operator has a unique function, allowing you to perform tasks like addition, subtraction, and bitwise manipulation directly while assigning values.

FAQs

1. What are Assignment Operators in Python?

Assignment Operators in Python are special symbols used to assign values to variables. They simplify code by combining arithmetic or bitwise operations with assignment. This makes modifying variable values faster and more readable.

2. How does the += operator work in Python?

The += operator adds the right operand’s value to the left operand variable and then assigns the result back to that variable. It helps shorten code by replacing expressions like a = a + b with a += b.

3. What is the Walrus Operator in Python and how is it used?

The Walrus Operator (:=) allows you to assign a value to a variable as part of an expression. It helps reduce redundant code by combining assignment and evaluation in one step, improving efficiency in loops and conditions.

4. Can Assignment Operators in Python be used with strings?

Yes, many assignment operators work with strings. For example, the += operator concatenates strings by adding the right string to the left one, making it easier to append text to existing variables.

5. What is the difference between = and += operators?

The = operator assigns a value directly to a variable, while += adds the right operand’s value to the existing variable before assignment. The += operator is a shorthand for a = a + b.

6. Are bitwise assignment operators important in Python?

Yes, bitwise assignment operators like &=, |=, and ^= perform bit-level operations while assigning results to variables. These operators are crucial when dealing with low-level data manipulation or optimizing certain algorithms.

7. Can I use multiple assignment operators in a single Python statement?

Python does not support chaining multiple assignment operators like a += b -= c. Each operation must be written as a separate statement to avoid syntax errors and maintain clear, readable code.

8. Does the division assignment operator ( /= ) always return a float?

Yes, in Python 3, the /= operator performs true division, which means the result is always a float, even if both operands are integers. For integer division, use the floor division assignment operator //=.

9. How does the modulus assignment operator work?

The modulus assignment operator %= divides the left operand by the right operand and assigns the remainder back to the left operand. It’s useful for tasks like checking divisibility or cycling through values.

10. Are Assignment Operators in Python limited to numbers only?

No, assignment operators work with various data types, including numbers, strings, lists, and sets. Their behavior adapts depending on the data type, enabling flexible and powerful value updates.

11. What happens if I use the exponentiation assignment operator with negative numbers?

The **= operator raises the left operand to the power of the right operand, even if the base is negative. The result follows standard mathematical rules, and Python handles negative powers correctly.

12. How can using Assignment Operators in Python improve code readability?

Assignment operators reduce code length and complexity by combining arithmetic or bitwise operations with assignment. This leads to cleaner, easier-to-understand code, which improves maintenance and reduces errors during development.

image

Take our Free Quiz on Python

Answer quick questions and assess your Python knowledge

right-top-arrow
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Explore Our Free Software Tutorials

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918068792934

Disclaimer

1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.

2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.