Assignment Operators in Python

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.

Assignment Operators in Python
Assignment Operators in Python

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
Python

Now, if you want to update your age after two years, you can do it like this:

age = age + 2
print(age)  # Output: 19
Python

However, 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
Python

Here, += 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'
Python

2. += (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
Python

3. -= (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
Python

4. *= (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
Python

5. /= (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...
Python

6. //= (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
Python

7. %= (Modulus and Assign)

Calculates the remainder and assigns it back.

money = 14000000  # 14000000
money %= 3  # Equivalent to money = money % 3
print(money)  # Output: 2
Python

8. **= (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
Python

9. &= (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>
Python

10. |= (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)
Python

11. ^= (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)
Python

12. >>= (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)
Python

13. <<= (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)
Python

Note
When using assignment operators, remember that the variable always comes first. For example, in the expression age -= 2, it means age = age - 2 “subtract 2 from age,” not age = 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!

Assignment Operators in Python

Assign values to variables

1 / 4

What are assignment operators in Python used for?

Bitwise AND assignment

2 / 4

What does the operator ‘a &= b’ do?

Floor divide and assign

3 / 4

Which assignment operator performs floor division and then assigns the result back to the variable?

Division returns float

4 / 4

What is the result type when using the ‘/=’ operator in Python?

Your score is

The average score is 0%

0%

Leave a Reply

Your email address will not be published. Required fields are marked *