Python 3 – Assignment Operators Example
In this article, we will explore the different types of assignment operators used in Python 3. Assignment operators are used to assign values to variables. In Python 3, there are several assignment operators, such as the equal operator, plus equal operator, minus equal operator, multiplication equal operator, division equal operator, modulus equal operator, and exponent equal operator.
Equal Operator
The equal operator is used to assign a value to a variable. The equal symbol (=) is used for this operation in Python. Here is an example:
# Assigning values to variables using equal operator
a = 5
b = "Hello"
Plus Equal Operator
The plus equal operator (+=) is used to add the right operand to the left operand and then assign the result to the left operand. Here is an example:
# Addition using plus equal operator
a = 5
a += 3
print(a) # Output: 8
Minus Equal Operator
The minus equal operator (-=) is used to subtract the right operand from the left operand and then assign the result to the left operand. Here is an example:
# Subtraction using minus equal operator
a = 10
a -= 4
print(a) # Output: 6
Multiplication Equal Operator
The multiplication equal operator (*=) is used to multiply the right operand with the left operand and then assign the result to the left operand. Here is an example:
# Multiplication using multiplication equal operator
a = 3
a *= 5
print(a) # Output: 15
Division Equal Operator
The division equal operator (/=) is used to divide the left operand by the right operand and then assign the result to the left operand. Here is an example:
# Division using division equal operator
a = 9
a /= 3
print(a) # Output: 3.0
Modulus Equal Operator
The modulus equal operator (%=) is used to find the remainder of the division operation between the left operand and the right operand, and then assign the result to the left operand. Here is an example:
# Modulus using modulus equal operator
a = 15
a %= 4
print(a) # Output: 3
Exponent Equal Operator
The exponent equal operator (**=) is used to perform exponentiation between the left operand and the right operand, and then assign the result to the left operand. Here is an example:
# Exponentiation using exponent equal operator
a = 2
a **= 3
print(a) # Output: 8
In conclusion, assignment operators in Python 3 are used to assign values to variables, and there are several types of assignment operators, such as the equal operator, plus equal operator, minus equal operator, multiplication equal operator, division equal operator, modulus equal operator, and exponent equal operator. These operators help to simplify the way we perform operations on variables in Python.