Lessons

Python Basics

Python Variables

Operators in Python

Conditional Statements in Python

Python Lists

Python Tuples

Python Sets

Python Dictionaries

Loops in Python

Python Arrays and Functions

Conclusion

Assignment Operators in Python

Python Assignment Operators

Assignment operators in Python are used to assign values to variables. They can also be used to perform operations on variables and assign the result back to the same variable. Here's an overview of the various assignment operators and their usage.

Basic Assignment Operator (=)

The = operator is used to assign a value to a variable.

Example:

python
1
2
x = 5
print(x)  # Output: 5

Compound Assignment Operators

Compound assignment operators combine an operation with assignment, allowing you to modify a variable and update its value in a single step.

1. Addition Assignment (+=)

Adds a value to a variable and assigns the result to the variable.

Example:

python
1
2
3
x = 5
x += 3  # Same as: x = x + 3
print(x)  # Output: 8

2. Subtraction Assignment (-=)

Subtracts a value from a variable and assigns the result to the variable.

Example:

python
1
2
3
x = 5
x -= 3  # Same as: x = x - 3
print(x)  # Output: 2

3. Multiplication Assignment (*=)

Multiplies a variable by a value and assigns the result to the variable.

Example:

python
1
2
3
x = 5
x *= 3  # Same as: x = x * 3
print(x)  # Output: 15

4. Division Assignment (/=)

Divides a variable by a value and assigns the result to the variable.

Example:

python
1
2
3
x = 9
x /= 3  # Same as: x = x / 3
print(x)  # Output: 3.0

5. Modulus Assignment (%=)

Applies the modulus operation to a variable and assigns the result to the variable.

Example:

python
1
2
3
x = 10
x %= 3  # Same as: x = x % 3
print(x)  # Output: 1

6. Floor Division Assignment (//=)

Performs floor division and assigns the result to the variable.

Example:

python
1
2
3
x = 10
x //= 3  # Same as: x = x // 3
print(x)  # Output: 3

7. Exponentiation Assignment (**=)

Raises a variable to the power of a value and assigns the result to the variable.

Example:

python
1
2
3
x = 2
x **= 3  # Same as: x = x ** 3
print(x)  # Output: 8

Bitwise Assignment Operators

These operators perform bitwise operations and assign the result back to the variable.

8. Bitwise AND Assignment (&=)

Performs a bitwise AND operation and assigns the result to the variable.

Example:

python
1
2
3
x = 5  # Binary: 101
x &= 3  # Binary: 011, Result: 001
print(x)  # Output: 1

9. Bitwise OR Assignment (|=)

Performs a bitwise OR operation and assigns the result to the variable.

Example:

python
1
2
3
x = 5  # Binary: 101
x |= 3  # Binary: 011, Result: 111
print(x)  # Output: 7

10. Bitwise XOR Assignment (^=)

Performs a bitwise XOR operation and assigns the result to the variable.

Example:

python
1
2
3
x = 5  # Binary: 101
x ^= 3  # Binary: 011, Result: 110
print(x)  # Output: 6

11. Right Shift Assignment (>>=)

Shifts the bits of the variable to the right and assigns the result to the variable.

Example:

python
1
2
3
x = 8  # Binary: 1000
x >>= 2  # Shift 2 bits right, Result: 0010
print(x)  # Output: 2

12. Left Shift Assignment (<<=)

Shifts the bits of the variable to the left and assigns the result to the variable.

Example:

python
1
2
3
x = 2  # Binary: 0010
x <<= 2  # Shift 2 bits left, Result: 1000
print(x)  # Output: 8

13. The Walrus Operator (:=)

Introduced in Python 3.8, the walrus operator (:=) allows you to assign a value to a variable as part of an expression.

Example:

python
1
print(x := 3)  # Output: 3

The walrus operator is especially useful in conditional statements or loops where assignment is needed within the expression.

Frequently Asked Questions