For working professionals
For fresh graduates
More
13. Print In Python
15. Python for Loop
19. Break in Python
23. Float in Python
25. List in Python
27. Tuples in Python
29. Set in Python
53. Python Modules
57. Python Packages
59. Class in Python
61. Object in Python
73. JSON Python
79. Python Threading
84. Map in Python
85. Filter in Python
86. Eval in Python
96. Sort in Python
101. Datetime Python
103. 2D Array in Python
104. Abs in Python
105. Advantages of Python
107. Append in Python
110. Assert in Python
113. Bool in Python
115. chr in Python
118. Count in python
119. Counter in Python
121. Datetime in Python
122. Extend in Python
123. F-string in Python
125. Format in Python
131. Index in Python
132. Interface in Python
134. Isalpha in Python
136. Iterator in Python
137. Join in Python
140. Literals in Python
141. Matplotlib
144. Modulus in Python
147. OpenCV Python
149. ord in Python
150. Palindrome in Python
151. Pass in Python
156. Python Arrays
158. Python Frameworks
160. Python IDE
164. Python PIP
165. Python Seaborn
166. Python Slicing
168. Queue in Python
169. Replace in Python
173. Stack in Python
174. scikit-learn
175. Selenium with Python
176. Self in Python
177. Sleep in Python
179. Split in Python
184. Strip in Python
185. Subprocess in Python
186. Substring in Python
195. What is Pygame
197. XOR in Python
198. Yield in Python
199. Zip in Python
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!
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:
Unlock a career at Big Four with the following courses:
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.
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:
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.
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:
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!
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:
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.
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:
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!
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:
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.
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:
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!
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:
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.
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:
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.
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:
This operator is useful when you want to enable certain bits without affecting others, often used in flags and bitmask operations.
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:
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.
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:
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.
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:
This operator is often used to perform fast multiplication by powers of two, especially in low-level programming or bit manipulation tasks.
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.
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.
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.
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.
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.
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.
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.
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.
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 //=.
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.
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.
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.
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.
Take our Free Quiz on Python
Answer quick questions and assess your Python knowledge
Author|900 articles published
Previous
Next
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
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.