13
SepAssignment Operators in Python
What is an Assignment Operator in Python?
Assignment Operators in Python are used to assign values to the variables. "=" is the fundamental Python assignment operator. They require two operands to operate upon. The left side operand is called a variable, and the right side operand is the value. The value on the right side of the "=" is assigned to the variable on the left side of "=". The associativity is from right to left.
Read More: Top 50+ Python Interview Questions and Answers. |
Python supports mixed arithmetic; the two operands may be of different types. However, the type of left operand changes to the operand on the right if it is more expansive.
In this Python tutorial, we'll understand Python programming assignment operators with examples and augmented assignment operators in Python.
Before going in-depth about assignment operators, you must know about operators in Python. If you haven't visited the Operators in Python tutorial, refer to What are Operators in Python - Types of Operators in Python ( With Examples ).
Types of Assignment Operators in Python
There are three types of assignment operators in Python:
1. Simple Python Assignment Operator (=)
This assigns the value on the right-hand side (RHS) to the variable on the left-hand side (LHS). You can use a literal, another variable, or an expression in the assignment statement.
Example of Simple Python Assignment Operator
x = 100
y = 50
z = x + y
print(z)
In this example, we have assigned the value of the sum of x and y to the variable z using the assignment operator (=).
Output
150
2. Augmented Assignment Operators in Python
In addition to the "=" operator, you can combine arithmetic and bitwise operators with the = symbol to form a cumulated or augmented assignment operator.
1. Augmented Arithmetic Assignment Operators in Python
There are seven combinations of arithmetic operators with the assignment operator "=." The below table gives a glimpse of them.
Read More: Arithmetic Operators in Python |
Operator | Operator Name |
+= | Addition Assignment Operator |
-= | Subtraction Assignment Operator |
*= | Multiplication Assignment Operator |
/= | Division Assignment Operator |
%= | Modulus Assignment Operator |
//= | Floor Division Assignment Operator |
**= | Exponentiation Assignment Operator |
We'll see all these operators in detail in the upcoming sections.
2. Augmented Bitwise Assignment Operators in Python
There are five combinations of bitwise operators with the assignment operator "=." The below table gives a glimpse of them.
Read More: Bitwise Operators in Python |
Operator | Operator Name |
&= | Bitwise AND Assignment Operator |
|= | Bitwise OR Assignment Operator |
^= | Bitwise XOR Assignment Operator |
>>= | Bitwise Right Shift Assignment Operator |
<<= | Bitwise Left Shift Assignment Operator |
We'll see all these operators in detail in the upcoming sections.
Augmented Arithmetic Assignment Operators in Python
1. Augmented Addition Operator (+=)
It adds the right operand to the left operand and assigns the result to the left operand.
Example of Augmented Addition Operator in Python
x = 38
y = 56
# x = x + y
x += y
print(x)
In this example, we have assigned the value of the sum of x and y to the variable x using the assignment operator (+=).
Output
94
2. Augmented Subtraction Operator (-=)
It subtracts the right operand from the left operand and assigns the result to the left operand.
Example of Augmented Subtraction Operator in Python
x = 38
y = 23
# x = x - y
x -= y
print(x)
In this example, we have assigned the value of the difference between x and y to the variable x using the assignment operator (-=).
Output
15
3. Augmented Multiplication Operator (*=)
It multiplies the right operand with the left operand and assigns the result to the left operand.
Example of Augmented Multiplication Operator in Python
x = 56
y = 2
# x = x * y
x *= y
print(x)
In this example, we have assigned the value of the product of x and y to the variable x using the assignment operator (*=).
Output
112
4. Augmented Division Operator (/=)
It divides the left operand with the right operand and assigns the result to the left operand.
Example of Augmented Division Operator in Python
x = 67
y = 7
# x = x / y
x /= y
print(x)
In this example, we have assigned the value of the result of dividing x by y to the variable x using the assignment operator (/=).
Output
9.571428571428571
5. Augmented Modulus Operator (%=)
It calculates the modulus of two operands and assigns the result to the left operand.
Example of Augmented Modulus Operator in Python
x = 56
y = 3
# x = x % y
x %= y
print(x)
In this example, we have assigned the value of the modulus of x by y to the variable x using the assignment operator (%=).
Output
2
6. Augmented Floor division Operator (//=)
It divides the left operand with the right operand and assigns the floor value to the left operand.
Example of Augmented Floor Division Operator in Python
x = 63
y = 4
# x = x // y
x //= y
print(x)
In this example, we have assigned the floor value of the division of x by y to the variable x using the assignment operator (//=).
Output
15
7. Augmented Exponent Operator (**=)
It calculates the exponent(raise power) value of the left operand raised to the right operand and assigns the floor value to the left operand.
Example of Augmented Exponent Operator in Python
x = 5
y = 4
# x **= x y
x **= y
print(x)
In this example, we have assigned the exponent value of x raised to y to the variable x using the assignment operator (**=).
Output
625
Augmented Bitwise Assignment Operators in Python
1. Augmented Bitwise AND (&=)
It performs the bitwise AND operation on the left and right operands and assigns the result to the left operand.
Example of Augmented Bitwise AND Operator in Python
x = 2
y = 6
# x = x & y
x &= y
print(x)
In this example, we have assigned the result of x & y to the variable x using the assignment operator (&=).
Output
2
2. Augmented Bitwise OR (|=)
It performs the bitwise OR operation on the left and right operands and assigns the result to the left operand.
Example of Augmented Bitwise OR Operator in Python
x = 21
y = 7
# x = x | y
x |= y
print(x)
In this example, we have assigned the result of x | y to the variable x using the assignment operator (|=).
Output
23
3. Augmented Bitwise XOR (^=)
It performs the bitwise XOR operation on the left and right operands and assigns the result to the left operand.
Example of Augmented Bitwise XOR Operator in Python
x = 215
y = 5
# x = x ^ y
x ^= y
print(x)
In this example, we have assigned the result of x ^ y to the variable x using the assignment operator (^=).
Output
210
4. Augmented Bitwise Right Shift (>>=)
It performs the bitwise right shift operation on the left and right operands and assigns the result to the left operand.
Example of Augmented Bitwise Right Shift Operator in Python
x = 25
y = 5
# x = x >> y
x >>= y
print(x)
In this example, we have assigned the result of x >>= y to the variable x using the assignment operator (>>=).
Output
0
5. Augmented Bitwise Left Shift (<<=)
It performs the bitwise left shift operation on the left and right operands and assigns the result to the left operand.
Example of Augmented Bitwise Left Shift Operator in Python
x = 35
y = 8
# x = x << y
x <<= y
print(x)
In this example, we have assigned the result of x <<= y to the variable x using the assignment operator (<<=).
Output
8960
Walrus Operator in Python
Python 3.8 introduced the walrus operator (:=) to write an assignment expression. Assignment expressions have a return value that is automatically assigned to a variable.
Syntax of an Assignment Expression
(variable := expression)
Example of Walrus Operator in Python
numbers = [10, 20, 30, 40, 50]
total = 0
while (num := numbers.pop(0)) != 0:
total += num
print(f'Current number: {num}, Running total: {total}')
if not numbers:
break
print(f'Final total: {total}')
The ":=" operator assigns the value of numbers.pop(0) to the variable num and evaluates it in the Python while loop condition.
Output
Current number: 10, Running total: 10
Current number: 20, Running total: 30
Current number: 30, Running total: 60
Current number: 40, Running total: 100
Current number: 50, Running total: 150
Final total: 150
Also explore following Python operators: |
Summary
Assignment operators are critical fundamental operators that are useful for various operations. They are capable of performing assignment, bitwise, and shift operations. To learn Python effectively, you need to master these operators.