The Complete Guide To Python Operators – Types, Advantages, & More
Did you know that there are almost 8 million Python developers? So, what makes Python this popular? It is because of the features like creating complex applications and readable syntaxes that it offers. It also makes a lot of functions easier for the developers.
One such feature of Python is the Python operator. They are some specific symbols used for a variety of computational performances. In this blog, we will cover everything about these operators in Python, along with descriptive examples. Let’s begin.
What is Operator in Python?
The operator in Python is a specific symbol or keyword used for various computational performances. They change and manipulate data, values, and variables according to the developer’s requirements. They are used in combination or singly to create advanced logic in Python.
Types of Operators in Python
The following are the types of operators in Python.
1. Arithmetic Operators
This operator is used to perform all mathematical operations like addition, subtraction, multiplication, division, modulo, and more. The table below explains all its procedures descriptively.
Operator | Operation | Example |
+ | Addition | 5+6=11 |
– | Subtraction | 6-4=2 |
* | Multiplication | 3*5=15 |
/ | Division | 6/2=3 |
// | Floor Division | 10//3=3 |
% | Modulo | 5%=1 |
** | Power | 4**2=16 |
Given below is an example of the arithmetic operator.
a = 4
b = 3
# addition
print (‘Sum: ‘, a + b)
# subtraction
print (‘Subtraction: ‘, a – b)
# multiplication
print (‘Multiplication: ‘, a * b)
# division
print (‘Division: ‘, a / b)
# floor division
print (‘Floor Division: ‘, a // b)
# modulo
print (‘Modulo: ‘, a % b)
# a to the power b
print (‘Power: ‘, a ** b)
The example mentioned above will give the following output:
Sum: 7
Subtraction: 3
Multiplication: 12
Division: 1.3
Floor Division: 1
Modulo: 1
Power: 64
2. Assignment Operators
This operator in Python assigns specific values to the variables. The table below explains it in detail:
Operator | Operation | Example |
= | Assignment Operator | a = 4 |
+= | Addition Assignment | a += 2 # a = a + 2 |
-= | Subtraction Assignment | a -= 4 # a = a – 4 |
*= | Multiplication Assignment | a *= 6 # a = a * 6 |
/= | Division Assignment | a /= 7 # a = a / 7 |
%= | Remainder Assignment | a %= 14 # a = a % 14 |
**= | Exponent Assignment | a **= 5 # a = a ** 5 |
Given below is an example of the assignment operator.
# assign 3 to a
a = 3
# assign 6 to b
b = 6
# assign the sum of a and b to a
a += b # a = a + b
print(a)
# Output: 9
In the example mentioned above, the addition assignment is used to deduce the sum of the variables. Hence received the output, 9.
3. Comparison Operators
This operator is used to compare two values or variables in Python to return the boolean values, i.e., either true or false. The following table will explain this operator in detail:
Operation | Operation | Example |
== | Indicates equal to | 4 == 2 gives the result False |
!= | Not equal to | 4 != 2 gives the result True |
> | Used for greater than | 4 > 2 gives the result True |
< | Less than | 4 < 2 gives the result False |
>= | Indicates greater than or equal to | 4 >= 2 gives the result True |
<= | Indicates less than or equal to | 4 <= 2 gives the result False |
Given below is an example of the comparison operator.
a = 4
b = 3
# equal to operator
print(‘a == b =’, a == b)
# not equal to operator
print(‘a != b =’, a != b)
# greater than operator
print(‘a > b =’, a > b)
# less than operator
print(‘a < b =’, a < b)
# greater than or equal to operator
print(‘a >= b =’, a >= b)
# less than or equal to operator
print(‘a <= b =’, a <= b)
The output for these examples would look something like this:
a == b = False
a != b = True
a > b = True
a < b = False
a >= b = True
a <= b = False
You can learn more about operators in Python through this comprehensive Python course.
4. Logical Operators
This operator in Python is used to make a specific decision. It addresses whether the expression used is true or false. There are three types of logical operators: AND, OR, and NOT. The table below explains its operations in detail:
Operator | Operation | Example |
AND | This operator gives a true expression when both the operands are true. | a AND b |
OR | This operator gives a true expression when at least one operand is true. | a OR b |
NOT | This operator gives a true expression when the operand is false. It gives false when the operand is true. | NOT aNOT b |
Given below is an example of a logical operator.
# logical AND
print(True and True) # True
print(True and False) # False
# logical OR
print(True or False) # True
# logical NOT
print(not True) # False
5. Bitwise Operators
This operator in Python operates by bit. It works on the operands in strings of binary digits. They consist of AND, OR, NOT, XOR, right shift, and left shift operators. The following table explains this Python operator in detail:
Operator | Operation | Example |
& | Bitwise AND | a & b = 0 (0000 0000) |
| | Bitwise OR | a | b = 14 (0000 1110) |
~ | Bitwise NOT | ~a = ~b (1111 0101) |
^ | Bitwise XOR | a ^ b = 14 (0000 1110) |
>> | Bitwise right shift | a >> 2 = 2 (0000 0010) |
<< | Bitwise left shift | a << 2 = 40 (0010 1000) |
6. Identity Operators
This is a special operator in Python and is used to check the location of the values. It uses “is” and “is not” to identify the same. The following table explains it in detail:
Operator | Operation | Example |
is | It gives true expression if the operands are identical. | a is true |
is not | It gives true expression if the operands are not identical. | a is not true |
Given below is an example of the identical operator.
a1 = 2
b1 = 2
a2 = ‘Hey’
b2 = ‘Hey’
a3 = [1,2,3]
b3 = [1,2,3]
print(a1 is not b1) # prints False
print(a2 is b2) # prints True
print(a3 is b3) # prints False
In the example above, a1 and b1 have the same value as an integer and are identical. The same case is followed in a2 and b2. However, a3 and b3 are unidentical, as they are strings.
7. Membership Operators
This is another example of a special operator in Python. It is used to test the sequence in value or variable. It uses “in” and “not in” to test the sequence of the same. The following table shows a descriptive example of this operator:
Operator | Operation | Example |
in | It gives a true expression if the value or variable is found in the sequence. | 2 in a |
not in | It gives a true expression if the value or variable is not found in the sequence. | 2 not in a |
Given below is an example of the membership operator.
a = ‘Hello’
b = {1:’x’, 2:’y’}
# check if ‘H’ is present in a string
print(‘H’ in a) # prints True
# check if ‘hello’ is present in a string
print(‘hello’ not in a) # prints True
# check if ‘1’ key is present in b
print(1 in b) # prints True
# check if ‘x’ key is present in b
print(‘x’ in b) # prints False
In the above example, H is in a, but Hello isn’t. Similarly, 1 is the key, and ‘x’ is the value of b. Hence, the output results ‘x’ in b as False.
Advantages and Disadvantages of Python Operator
The advantages and disadvantages of these operators are explained in the comparison table below.
Advantages | Disadvantages |
It allows concise coding. | If they are overused, the codes can become unreadable. |
It is easy to use and understand. | It may give unexpected results sometimes. |
It saves time from writing codes again and again. | It may have different meanings in different contexts. |
Conclusion
Python operators are a great way to create complex applications. They allow developers to write clear and concise codes that are easy to use and understand. Different types of operators help save time by reusing already created codes. Therefore, it is essential to have a clear understanding of these operators if you are a developer.