Assignment Operator in Python : Introduction
In programming, an assignment operator is a symbol that assigns the value on its right to the variable on its left. The most common assignment operator is the equals sign (`=`), but there are many variations used to perform specific operations while assigning values.
Basic Assignment Operator (`=`)
The simplest form of the assignment operator is `=`, which assigns the value of the right-hand operand to the left-hand operand.
variable = value
In this example, the value 10
is assigned to the variable x
. Now, x
holds the value 10
and can be used throughout the program.
Compound Assignment Operators
To simplify code and make it more concise, many programming languages offer compound assignment operators. These operators combine an arithmetic or bitwise operation with assignment.
Some common compound assignment operators include:
- Addition assignment (
+=
)- Adds the right-hand operand to the left-hand operand and assigns the result to the left-hand operand.
2. Subtraction assignment (-=
)
- Subtracts the right-hand operand from the left-hand operand and assigns the result to the left-hand operand.
3. Multiplication assignment (*=
)
Multiplies the left-hand operand by the right-hand operand and assigns the result to the left-hand operand.
4. Division assignment (/=
)
Divides the left-hand operand by the right-hand operand and assigns the result to the left-hand operand.
5. Modulus assignment (%=
)
Divides the left-hand operand by the right-hand operand and assigns the remainder to the left-hand operand.
6. Exponentiation assignment (**=
)
Raises the left-hand operand to the power of the right-hand operand and assigns the result to the left-hand operand.
7. Floor Division assignment (//=
)
Divides the left-hand operand by the right-hand operand, floors the result (removes the decimal part), and assigns it to the left-hand operand.
Bitwise Assignment Operators
Some assignment operators perform bitwise operations:
- Bitwise AND assignment (
&=
)- Performs a bitwise AND operation between the left and right operands and assigns the result to the left-hand operand.
2. Bitwise OR assignment (|=
)
Performs a bitwise OR operation between the left and right operands and assigns the result to the left-hand operand.
3. Bitwise XOR assignment (^=
)
Performs a bitwise XOR operation between the left and right operands and assigns the result to the left-hand operand.
4. Left shift assignment (<<=
)
Shifts the bits of the left-hand operand to the left by the number of places specified by the right-hand operand, and assigns the result to the left-hand operand.
5. Right shift assignment (>>=
)
Shifts the bits of the left-hand operand to the right by the number of places specified by the right-hand operand, and assigns the result to the left-hand operand.
Chained Assignment
In many programming languages, including Python, you can assign the same value to multiple variables in a single statement.
Here, 5
is assigned to a
, b
, and c
at the same time.
Unpacking Assignment
Some languages, like Python, also support unpacking assignment, where multiple values are assigned to multiple variables in one line.