Click for the Python operators (Part 1) tutorial.
How Many Types of Python Operators?
We already learned about Python operators in the previous tutorial. We also learned Python operators are divided into seven categories. We have completed the arithmetic operators in the previous tutorial. Now continue with the rest of the operators in Python.
Assignment Operators in Python
We use the assignment operator to assign values to variables, update values, or change values. The equal sign (=) is used in assignment operators. It worked with left and right operands. Mostly, the left operand is a variable and the right operand is a value or an expression.
NOTE: The assignment operator does not create expressions; it creates a kind of statement that returns a value.
There are several kinds of assignment operators in Python.
1: Basic Assignment Operator (=)
It is also called a basic assignment operator. We use the assignment operator to assign the right-side expression to the left-side operand. Or we can change, set, update, and delete the value of the right side. For example:
Code:
name = 'Python'
print(name)
Output:
Python
Here name
is a variable and we assign 'Python'
as value to this variable. We use the equal sign (=) to assign value. We can assign multiple values. For example:
Code:
name1, name2 = 'Python', 'Programming'
print(name1)
print(name2)
Output:
Python
Programming
So this is how we can assign multiple values with the help of an assignment operator.
2. Addition Assignment Operator (+=)
We can mix another operator with the assignment operator; for example, if we use the addition operator (+) with the assignment operator (=), then it is called the addition assignment operator (+=). With the help of the addition assignment operator, we can add the right side operand to the left side operand. And after the addition, the result will be assigned to the left side operand.
Syntax: num1 += num2 Here num1 and num2 are two operands. And when we use num1 += num2 this is the short for:num1 = num1 + num2 So it means add num1 with num2 and the result will be assigned to num1 . |
For example:
Code:
num1 = 10
num1 += 10
print(num1)
Output:
20
So here num1 += 10
means num1 = num1 + 10
And num1 = 10
so 10+10 = 20
is the output.
Another example:
Code:
num1 = 10
num2 = 15
num1 += num2
print(num1)
Output:
25
Same as above, num1 += num2
means num1 = num1 + num2
so we get 25 in the output.
Another example:
Code:
text1 = 'Python '
text2 = 'Programming'
text1 += text2
print(text1)
Output:
Python Programming
If we try to add two different kinds of data types, we will get an error:
Code:
text1 = 'Python'
num1 = 10
text1 += num1
print(text1)
Output:
TypeError: can only concatenate str (not "int") to str
Here we are trying to add two different kinds of data types. text1
is a string and num1 is an integer. To add different kinds of data types, we have to use type casting.
Code:
text1 = 'Python'
num1 = str(10)
text1 += num1
print(text1)
Output:
Python10
Here we changed the integer data type into a string data type. Click to learn about string type casting in Python.
3. Subtraction Assignment Operator (-=)
Just like addition, we can use subtraction (-) with the assignment operator (=). Same as addition, it will subtract the right side operand from the left side operand and assign the result to the left side operand.
Syntax: num1 -= num2 Here num1 and num2 are two operands. And when we use num1 -= num2 this is the short for:num1 = num1 – num2 So it means subtract num2 from num1 and the result will be assigned to num1 . |
For example:
Code:
num1 = 50
num2 = 10
num1 -= num2
print(num1)
Output:
40
Same as addition, num1 -= num2
means num1 = num1 - num2
. So we get 40 in the output. Another example:
Code:
num1 = 50
num2 = 60
num1 -= num2
print(num1)
Output:
-10
We get -10 in the output. Because num1 -= num2
means num1 = num1 - num2
so 50-60 = -10 is the result.
4. Multiplication Assignment Operator (*=)
We can use multiplication (*) with the assignment operator (=). We can multiply the right side operand with the left side operand and the result will be assigned to the left side operand.
Syntax: num1 *= num2 Here num1 and num2 are two operands. And when we use num1 *= num2 this is the short for:num1 = num1 * num2 So it means multiply num1 and num2 and the result will be assigned to num1 . |
For example:
Code:
num1 = 10
num1 *= 5
print(num1)
Output:
50
Same as above, here num1 *= 5
means num1 = num1 * 5
so we get 50 in the output. Another example:
Code:
num1 = 10
num2 = 5
num1 *= num2
print(num1)
Output:
50
Another example:
Code:
num1 = 10
num1 *= num1
print(num1)
Output:
100
So this is how we can use the multiple assignment operator.
5. Division Assignment Operator (/=)
Same as above, we can use the division assignment operator. The left side operand will be divided by the right side operand and the result will be assigned to the left side operand.
Syntax: num1 /= num2 Here num1 and num2 are two operands. And when we use num1 /= num2 this is the short for:num1 = num1 / num2 So it means subtract num1 by num2 and the result will be assigned to num1 . |
For example:
Code:
num1 = 4
num1 /= 2
print(num1)
Output:
2.0
Here we write, num1 /= 2
which means, num1 = num1 / 2
, so we get 2.0 in the output. We get the output in float data type because every time we use the division operator, the output always comes in float data type. It’s default. Another example:
Code:
num1 = 4
num2 = 7
num1 /= num2
print(num1)
Output:
0.5714285714285714
This is how we can use the division assignment operator.
6. Modulus Assignment Operator (%=)
We can use the modulus assignment operator. In modulus assignment operator, the interpreter did the modulus operation between the left and right operand, which means the left operand will be divided by the right operand and the remainder will be assigned to the left operand.
Syntax: num1 %= num2 Here num1 and num2 are two operands. And when we use num1 %= num2 this is the short for:num1 = num1 % num2 So it means divide num1 by num1 and the remainder will be assigned to num1 . |
For example:
Code:
num1 = 4
num1 %= 2
print(num1)
Output:
0
Here we write, num1 %= 2
which means, num1 = num1 % 2
so number 4 will be divided by number 2. And we get zero as the remainder. Another example:
Code:
num1 = 5
num2 = 2
num1 %= num2
print(num1)
Output:
1
Same as above, number 5 will be divided by number 2 and we get number 1 as the remainder. Another example:
Code:
num1 = 11
num2 = 20
num1 %= num2
print(num1)
Output:
11
Here our left operand (11) is smaller than the right operand (20). In such a situation, we always get the smaller number (left operand) as the remainder. So that’s why we get 11 in the output. Another example:
Code:
num1 = 110
num2 = 111
num1 %= num2
print(num1)
Output:
110
Same as the above example, here the left operand has a small number so we get number 110 in the output.
7. Floor Division Assignment Operator (//=)
Floor division assignment operator is similar to division assignment operator. The only difference between both of them is that the division assignment operator shows output in float data type, while the floor division assignment operator shows output in integer data type.
Syntax: num1 //= num2 Here num1 and num2 are two operands. And when we use num1 //= num2 this is the short for:num1 = num1 // num2 So it means divide num1 by num2 and the result will be assigned to num1 . |
For example:
Code:
num1 = 5
num1 //= 2
print(num1)
num1 = 5
num1 /= 2
print(num1)
Output:
2
2.5
We can see the difference between both operators. When we used floor division assignment, we got number 2 in the output. And when we used division assignment, we got number 2.5 in the output.
8. Exponentiation Assignment Operator (**=)
The exponentiation assignment operator is used to raise the left operand to the power of the right operand. And the output will be assigned to the left operand.
Syntax: num1 **= num2 Here num1 and num2 are two operands. And when we use num1 **= num2 this is the short for:num1 = num1 ** num2 So it means num1 will be raised to the power of num2 and the remainder will be assigned to num1 . |
For example:
Code:
num1 = 5
num1 **= 2
print(num1)
Output:
25
Here, num1 **= 2
means, num1 = num1 ** 2
, while num1 = 5
, so 5**2
, which means 5*5 = 25
. So we get 25 in the output. Another example:
Code:
num1 = 6
num2 = 3
num1 **= num2
print(num1)
Output:
216
So this is how we used the exponentiation assignment operator.
9. Bitwise AND Assignment Operator (&=)
Bitwise AND operation is done by bitwise AND operator. The bitwise AND operation is done on both operands, and the output will be assigned to the left operand.
Syntax: num1 &= num2 Here num1 and num2 are two operands. And when we use num1 &= num2 this is the short for:num1 = num1 & num2 So it means bitwise AND operation done with num1 and num2 and the result will be assigned to num1 . |
For example:
Code:
num1 = 5
num1 &= 3
print(num1)
Output:
1
Here first number 5 and number 3 are converted into bits, and then we do the AND operation. So this is how we used the bitwise AND assignment operator.
10. Bitwise OR Assignment Operator (|=)
Bitwise OR operation is done by bitwise OR operator. The bitwise OR operation is done on both operands, and the output will be assigned to the left operand.
Syntax: num1 |= num2 Here num1 and num2 are two operands. And when we use num1 |= num2 this is the short for:num1 = num1 | num2 So it means bitwise OR operation done with num1 and num2 and the result will be assigned to num1 . |
For example:
Code:
num1 = 3
num2 = 5
num1 |= num2
print(num1)
Output:
7
Here first number 3 and number 5 are converted into bits, and then we do the OR operation. So this is how we used the bitwise OR assignment operator.
11. Bitwise XOR Assignment Operator (^=)
We use this operator to do the bitwise XOR operation on the two operands.
Syntax: num1 ^= num2 Here num1 and num2 are two operands. And when we use num1 ^= num2 this is the short for:num1 = num1 ^ num2 So it means bitwise XOR operation done with num1 and num2 and the result will be assigned to num1 . |
For example:
Code:
num1 = 3
num2 = 5
num1 ^= num2
print(num1)
Output:
6
Here we do the bitwise XOR operation on both numbers.
12. Bitwise Right Shift Assignment Operator (>>=)
We use this operator to do the bitwise right shift operation on the two operands. And the output will be assigned to the left operand.
Syntax: num1 >>= num2 Here num1 and num2 are two operands. And when we use num1 >>= num2 this is the short for:num1 = num1 >> num2 So it means bitwise right shift operation done with num1 and num2 and the result will be assigned to num1 . |
For example:
Code:
num1 = 3
num2 = 5
num1 >>= num2
print(num1)
Output:
0
Here we do the bitwise right shift operation on both numbers.
13. Bitwise Left Shift Assignment Operator (<<=)
We use this operator to do the bitwise left shift operation on the two operands. And the output will be assigned to the left operand.
Syntax: num1 <<= num2 Here num1 and num2 are two operands. And when we use num1 <<= num2 this is the short for:num1 = num1 << num2 So it means bitwise left shift operation done with num1 and num2 and the result will be assigned to num1 . |
For example:
Code:
num1 = 3
num2 = 5
num1 <<= num2
print(num1)
Output:
96
Here we do the bitwise left shift operation on both numbers.
14. Walrus Operator (:=)
The walrus operator is a new kind of operator introduced in Python 3.8. With the help of the walrus operator, we can assign an expression as a value to a variable.
Syntax: x := expression Here x is a variable and we can use any expression. The symbol of walrus operator (:=) is similar to walrus animal; that’s why it’s named walrus operator. |
In a simple way, if we try to print;
Code:
print(x = 3)
Output:
TypeError: print() got an unexpected keyword argument 'x'
We get an error because x = 3
is an expression or, as we can say, we are creating a variable named x that contains the number 3 as a value. So if we want to print the value of this variable, we have to write like this:
Code:
x = 3
print(x)
Output:
3
Now if we use the walrus operator, then:
Code:
print(x := 3)
Output:
3
So with the help of the walrus operator, we can directly print the value of a variable. Same way, we can use the walrus operator with any function or loop in Python.
End of the Python operator (Part 2) tutorial
2 thoughts on “Python Operators (Part 2): How Many Types of Operators in Python?”