Assignment Operators in Python
Assignment Operators in Python

Assignment Operators in Python

Assignment operators are crucial in Python, helping to assign values to variables and perform operations simultaneously. These operators are commonly used in everyday programming tasks and allow developers to streamline their code. This guide will cover all the assignment operators in Python with simple explanations and examples for better understanding.

What Are Assignment Operators?

Assignment operators in Python are used to assign values to variables. The most basic one is the equals sign (=), but Python provides several compound assignment operators that allow you to perform an operation and assign the result at the same time.

Basic Assignment Operator (=)

The = operator is the simplest assignment operator. It assigns the value on the right side to the variable on the left.

Syntax:

Python
variable = value

Example:

Python
x = 10
print(x)  # Output: 10

In this example, the value 10 is assigned to the variable x.

Compound Assignment Operators

In addition to the basic assignment operator (=), Python offers several compound assignment operators that combine arithmetic operations with assignment. These operators modify the value of a variable by performing an operation and then updating the variable with the result.

Add and Assign (+=)

The += operator adds the value on the right to the variable on the left and assigns the result back to the variable.

Syntax:

Python
variable += value

Example:

Python
x = 5
x += 3  # Equivalent to x = x + 3
print(x)  # Output: 8

In this example, 3 is added to x, and the result (8) is stored back in x.

Subtract and Assign (- =)

The -= operator subtracts the value on the right from the variable on the left and assigns the result back to the variable.

Syntax:

Python
variable -= value

Example:

Python
x = 10
x -= 4  # Equivalent to x = x - 4
print(x)  # Output: 6

In this example, 4 is subtracted from x, and the result (6) is stored in x.

Multiply and Assign (*=)

The *= operator multiplies the value on the right by the variable on the left and assigns the result back to the variable.

Syntax:

Python
variable *= value

Example

Python
x = 7
x *= 2  # Equivalent to x = x * 2
print(x)  # Output: 14

Here, x is multiplied by 2, and the result (14) is stored in x.

Divide and Assign (/=)

The /= operator divides the variable on the left by the value on the right and assigns the result back to the variable.

Syntax:

Python
variable /= value

Example:

Python
x = 20
x /= 4  # Equivalent to x = x / 4
print(x)  # Output: 5.0

In this case, x is divided by 4, and the result (5.0) is stored in x.

Modulus and Assign (%=)

The %= operator calculates the remainder when the variable on the left is divided by the value on the right, then assigns the result back to the variable.

Syntax:

Python
variable %= value

Example:

Python
x = 15
x %= 4  # Equivalent to x = x % 4
print(x)  # Output: 3

Here, 15 % 4 equals 3, so the result (3) is stored in x.

Floor Divide and Assign (//=)

The //= operator performs floor division (division that rounds down to the nearest whole number) and assigns the result back to the variable.

Syntax:

Python
variable //= value

Example:

Python
x = 17
x //= 3  # Equivalent to x = x // 3
print(x)  # Output: 5

In this example, 17 divided by 3 is 5 using floor division, so x is updated to 5.

Exponent and Assign (**=)

The **= operator raises the variable on the left to the power of the value on the right and assigns the result back to the variable.

Syntax:

Python
variable **= value

Example:

Python
x = 2
x **= 3  # Equivalent to x = x ** 3
print(x)  # Output: 8

In this case, x is raised to the power of 3, so the result (8) is stored in x.

Bitwise AND and Assign (&=)

The &= operator performs a bitwise AND operation between the variable and the value, then assigns the result back to the variable.

Syntax:

Python
variable &= value

Example:

Python
x = 5  # 5 is 0101 in binary
x &= 3  # 3 is 0011 in binary; result is 0001
print(x)  # Output: 1

Bitwise OR and Assign (|=)

The |= operator performs a bitwise OR operation between the variable and the value, then assigns the result back to the variable.

Syntax:

Python
variable |= value

Example:

Python
x = 5  # 5 is 0101 in binary
x |= 3  # 3 is 0011 in binary; result is 0111
print(x)  # Output: 7

Bitwise XOR and Assign (^=)

The ^= operator performs a bitwise XOR operation between the variable and the value, then assigns the result back to the variable.

Syntax:

Python
variable ^= value

Example:

Python
x = 5  # 5 is 0101 in binary
x ^= 3  # 3 is 0011 in binary; result is 0110
print(x)  # Output: 6

Bitwise Shift Left and Assign (<<=)

The <<= operator shifts the bits of the variable to the left by the specified number of positions and assigns the result back to the variable.

Syntax:

Python
variable <<= value

Example:

Python
x = 5  # 5 is 0101 in binary
x <<= 1  # Shift left by 1; result is 1010
print(x)  # Output: 10

Bitwise Shift Right and Assign (>>=)

The >>= operator shifts the bits of the variable to the right by the specified number of positions and assigns the result back to the variable.

Syntax:

Python
variable >>= value

Example:

Python
x = 5  # 5 is 0101 in binary
x >>= 1  # Shift right by 1; result is 0010
print(x)  # Output: 2

Summary of Assignment Operators

OperatorDescriptionExampleEquivalent To
=Assigns value to a variablex = 5N/A
+=Adds and assignsx += 3x = x + 3
-=Subtracts and assignsx -=2x = x – 2
*=Multiplies and assignsx *= 4x = x * 4
/=Divides and assignsx /= 2x = x / 2
%=Modulus and assignsx %= 3x = x % 3
//=Floor division and assignsx //= 2x = x // 2
**=Exponent and assignsx **= 2x = x ** 2
&=Bitwise AND and assignsx &= 3x = x & 3
|=Bitwise OR and assignsx |= 3x = x | 3
^=Bitwise XOR and assignsx ^= 3x = x ^ 3
<<=Shifts bits left and assignsx <<= 1x = x << 1
>>=Shifts bits right and assignsx >>= 1x = x >> 1

Summary

Assignment operators in Python make it easier to perform arithmetic or bitwise operations while updating the value of a variable in one step. Understanding

these operators will help you write more efficient and concise code. Whether you’re working with simple arithmetic or bitwise operations, assignment operators provide a powerful and flexible way to handle variable updates.

By practicing these operators in various programming scenarios, you’ll quickly become familiar with how they work and when to use them.


Discover more from lounge coder

Subscribe to get the latest posts sent to your email.

Leave a Reply

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

Discover more from lounge coder

Subscribe now to keep reading and get access to the full archive.

Continue reading