Assignment operators are fundamental tools, that allow you to Assign values to variables. These operators not only perform calculations but also directly assign the result of these calculations to a variable. So let’s explore the concept of assignment operator, their syntax, and various types with examples to help you understand.
Table of Contents
What Are Assignment Operators?
Assignment operators in Python are used to assign values to variables. Beyond the basic = operator (equals to), they combine arithmetic operations, bitwise operations, and more with assignment, making your code more concise and readable. They are used to perform operations like addition, subtraction, multiplication, etc., while simultaneously assigning the result back to the variable.
For example, if you want to update your age variable after two years, you could do it the traditional way:
age = 17
print(age) # Output: 17
PythonNow, if you want to update your age after two years, you can do it like this:
age = age + 2
print(age) # Output: 19
PythonHowever, Python offers a shortcut to make this easier. Instead of writing age = age + 2
, you can use the +=
operator:
age += 2
print(age) # Output: 21
PythonHere, +=
performs the addition and directly updates the variable in one line. This way, you can increment the value of age without repeating the variable name, making your code easier to read.
Similarly, Python provides assignment operators for different kind of operations:
1. =
Operator
The simplest form of assignment. It assigns a value to a variable.
age = 17 # Assigns 17 to the variable 'age'
Python2. +=
(Add and Assign)
Adds a value to the variable and assigns the result back to the variable.
age += 2 # Equivalent to age = age + 2
print(age) # Output: 19
Python3. -=
(Subtract and Assign)
Subtracts a value from the variable and assigns the result back.
age -= 2 # Equivalent to age = age - 2
print(age) # Output: 17
Python4. *=
(Multiply and Assign)
Multiplies the variable by a value and assigns the result back.
money = 7000000 # 7000000
money *= 2 # Equivalent to money = money * 2
print(money) # Output: 14000000
Python5. /=
(Divide and Assign)
Divides the variable by a value and assigns the result back. The result is a float.
money = 14000000 # 14000000
money /= 3 # Equivalent to money = money / 3
print(money) # Output: 4666666.666...
Python6. //=
(Floor Divide and Assign)
Performs floor division and assigns the integer result back.
money = 14000000 # 14000000
money //= 3 # Equivalent to money = money // 3
print(money) # Output: 4 million
Python7. %=
(Modulus and Assign)
Calculates the remainder and assigns it back.
money = 14000000 # 14000000
money %= 3 # Equivalent to money = money % 3
print(money) # Output: 2
Python8. **=
(Exponentiation and Assign)
Raises the variable to the power of a value and assigns the result back.
value = 2
value **= 3 # Equivalent to value = value ** 3
print(value) # Output: 8
Python9. &=
(Bitwise AND and Assign)
Performs a bitwise AND operation and assigns the result back.
a = 5 # Binary: 0101
b = 3 # Binary: 0011
a &= b # Equivalent to a = a & b
print(a) # Output: 1 (Binary: 0001)<br>
Python10. |=
(Bitwise OR and Assign)
Performs a bitwise OR operation and assigns the result back.
a = 5 # Binary: 0101
b = 3 # Binary: 0011
a |= b # Equivalent to a = a | b
print(a) # Output: 7 (Binary: 0111)
Python11. ^=
(Bitwise XOR and Assign)
Performs a bitwise XOR operation and assigns the result back.
a = 5 # Binary: 0101
b = 3 # Binary: 0011
a ^= b # Equivalent to a = a ^ b
print(a) # Output: 6 (Binary: 0110)
Python12. >>=
(Right Shift and Assign)
Shifts the bits of the variable to the right by a specified number of places and assigns the result back.
num = 8 # Binary: 1000
num >>= 2 # Equivalent to num = num >> 2
print(num) # Output: 2 (Binary: 0010)
Python13. <<=
(Left Shift and Assign)
Shifts the bits of the variable to the left by a specified number of places and assigns the result back.
num = 2 # Binary: 0010
num <<= 2 # Equivalent to num = num << 2
print(num) # Output: 8 (Binary: 1000)
PythonNote
When using assignment operators, remember that the variable always comes first. For example, in the expressionage -= 2
, it meansage = age - 2
“subtract 2 from age,” notage = 2 - age
“subtract age from 2.” This principle applies to all assignment operations in Python.
Why Use Assignment Operators?
Assignment operators simplify syntax and make your code more readable. Instead of writing lengthy expressions, you can achieve the same result with fewer lines of code. They’re especially useful in scenarios where variables need to be updated frequently, such as in loops or calculations.
Summary
Python provides a variety of assignment operators, including:
=
: Assign+=
: Add and assign-=
: Subtract and assign*=
: Multiply and assign/=
: Divide and assign//=
: Floor divide and assign%=
: Modulo and assign**=
: Exponentiation and assign
These operators make coding faster, cleaner, and more efficient. So, the next time you are working on calculations, remember these shortcuts!