Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
Assignment operators in Python are used to assign values to variables. In addition to the basic assignment operator, Python supports various compound assignment operators that perform an operation and assign the result to a variable in a single step.
In this tutorial, we will explore the different assignment operators in Python and how to use them.
=
):The basic assignment operator is used to assign a value to a variable.
a = 10 print(a) # Output: 10
Compound assignment operators perform an arithmetic operation and assign the result to the variable. They are a shorthand for performing an operation and then assigning the result to the same variable.
2.1. Addition assignment (+=
):
The addition assignment operator adds the right operand to the left operand and assigns the result to the left operand.
a = 10 a += 5 # Equivalent to a = a + 5 print(a) # Output: 15
2.2. Subtraction assignment (-=
):
The subtraction assignment operator subtracts the right operand from the left operand and assigns the result to the left operand.
a = 10 a -= 5 # Equivalent to a = a - 5 print(a) # Output: 5
2.3. Multiplication assignment (*=
):
The multiplication assignment operator multiplies the left operand by the right operand and assigns the result to the left operand.
a = 10 a *= 5 # Equivalent to a = a * 5 print(a) # Output: 50
2.4. Division assignment (/=
):
The division assignment operator divides the left operand by the right operand and assigns the result to the left operand.
a = 10 a /= 5 # Equivalent to a = a / 5 print(a) # Output: 2.0
2.5. Floor division assignment (//=
):
The floor division assignment operator divides the left operand by the right operand, rounds the result down to the nearest integer, and assigns the result to the left operand.
a = 10 a //= 3 # Equivalent to a = a // 3 print(a) # Output: 3
2.6. Modulus assignment (%=
):
The modulus assignment operator calculates the remainder when the left operand is divided by the right operand and assigns the result to the left operand.
a = 10 a %= 3 # Equivalent to a = a % 3 print(a) # Output: 1
2.7. Exponentiation assignment (**=
):
The exponentiation assignment operator raises the left operand to the power of the right operand and assigns the result to the left operand.
a = 2 a **= 3 # Equivalent to a = a ** 3 print(a) # Output: 8
In conclusion, assignment operators in Python provide a convenient way to assign values to variables and perform operations on them. The compound assignment operators allow you to perform arithmetic operations and update the value of a variable in a single step, resulting in cleaner and more efficient code.
Using = operator for variable assignment in Python:
=
operator is used for variable assignment in Python. It assigns the value on the right to the variable on the left.x = 10
Compound assignment operators in Python:
+=
adds the right operand to the left operand and assigns the result to the left operand.x = 5 x += 3 # Equivalent to x = x + 3
Assigning values to variables in Python:
=
operator, and the assigned value can be of any data type.name = "John" age = 25
Multiple assignment and unpacking in Python:
a, b, c = 1, 2, 3 tuple_values = (4, 5, 6) x, y, z = tuple_values # Unpacking
Chaining assignment operators in Python:
x = y = z = 10
In-place modification with assignment operators:
myList = [1, 2, 3] myList += [4, 5] # In-place modification
Common mistakes and pitfalls with assignment in Python:
=
instead of ==
for comparison, misunderstanding mutable and immutable objects, or assuming variables are copied when passed to functions.# Incorrect comparison if x = 10: # Should be if x == 10: print("x is 10")
=
instead of ==
) for comparison.