Posted in

Python Operators (Part 2): How Many Types of Operators in Python?

Python Operators Part 2
Python Operators Part 2

Click for the Python operators (Part 1) tutorial.

How Many Types of Python Operators?

Assignment Operators in Python

1: Basic Assignment Operator (=)
2. Addition Assignment Operator (+=)
3. Subtraction Assignment Operator (-=)
4. Multiplication Assignment Operator (*=)
5. Division Assignment Operator (/=)
6. Modulus Assignment Operator (%=)
7. Floor Division Assignment Operator (//=)
8. Exponentiation Assignment Operator (**=)
9. Bitwise AND Assignment Operator (&=)
10. Bitwise OR Assignment Operator (|=)
11. Bitwise XOR Assignment Operator (^=)
12. Bitwise Right Shift Assignment Operator (>>=)
13. Bitwise Left Shift Assignment Operator (<<=)
14. Walrus Operator (:=)

1: Basic Assignment Operator (=)

Code:
name = 'Python'
print(name)

Output:
Python
Code:
name1, name2 = 'Python', 'Programming'
print(name1)
print(name2)

Output:
Python
Programming

2. Addition Assignment Operator (+=)

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.
Code:
num1 = 10
num1 += 10
print(num1)

Output:
20
Code:
num1 = 10
num2 = 15
num1 += num2
print(num1)

Output:
25 
Code:
text1 = 'Python '
text2 = 'Programming'
text1 += text2
print(text1)

Output:
Python Programming
Code:
text1 = 'Python'
num1 = 10
text1 += num1
print(text1)

Output:
TypeError: can only concatenate str (not "int") to str
Code:
text1 = 'Python'
num1 = str(10)
text1 += num1
print(text1)

Output:
Python10

3. Subtraction Assignment Operator (-=)

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.
Code:
num1 = 50
num2 = 10
num1 -= num2
print(num1)

Output:
40
Code:
num1 = 50
num2 = 60
num1 -= num2
print(num1)

Output:
-10

4. Multiplication Assignment Operator (*=)

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.
Code:
num1 = 10
num1 *= 5
print(num1)

Output:
50
Code:
num1 = 10
num2 = 5
num1 *= num2
print(num1)

Output:
50
Code:
num1 = 10
num1 *= num1
print(num1)

Output:
100

5. Division Assignment Operator (/=)

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.
Code:
num1 = 4
num1 /= 2
print(num1)

Output:
2.0
Code:
num1 = 4
num2 = 7
num1 /= num2
print(num1)

Output:
0.5714285714285714

6. Modulus Assignment Operator (%=)

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.
Code:
num1 = 4
num1 %= 2
print(num1)

Output:
0
Code:
num1 = 5
num2 = 2
num1 %= num2
print(num1)

Output:
1
Code:
num1 = 11
num2 = 20
num1 %= num2
print(num1)

Output:
11
Code:
num1 = 110
num2 = 111
num1 %= num2
print(num1)

Output:
110

7. Floor Division Assignment Operator (//=)

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.
Code:
num1 = 5
num1 //= 2
print(num1)
num1 = 5
num1 /= 2
print(num1)

Output:
2
2.5

8. Exponentiation Assignment Operator (**=)

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.
Code:
num1 = 5
num1 **= 2
print(num1)

Output:
25
Code:
num1 = 6
num2 = 3
num1 **= num2
print(num1)

Output:
216

9. Bitwise AND Assignment Operator (&=)

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.
Code:
num1 = 5
num1 &= 3
print(num1)

Output:
1

10. Bitwise OR Assignment Operator (|=)

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.
Code:
num1 = 3
num2 = 5
num1 |= num2
print(num1)

Output:
7

11. Bitwise XOR Assignment Operator (^=)

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.
Code:
num1 = 3
num2 = 5
num1 ^= num2
print(num1)

Output:
6

12. Bitwise Right Shift Assignment Operator (>>=)

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.
Code:
num1 = 3
num2 = 5
num1 >>= num2
print(num1)

Output:
0

13. Bitwise Left Shift Assignment Operator (<<=)

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.
Code:
num1 = 3
num2 = 5
num1 <<= num2
print(num1)

Output:
96

14. Walrus Operator (:=)

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.
Code:
print(x = 3)

Output:
TypeError: print() got an unexpected keyword argument 'x'
Code:
x = 3
print(x)

Output:
3
Code:
print(x := 3)

Output:
3

End of the Python operator (Part 2) tutorial

Click for the Python operator (Part 3) tutorial

2 thoughts on “Python Operators (Part 2): How Many Types of Operators in Python?

Leave a Reply

Your email address will not be published. Required fields are marked *