What are operators?
An operator in a programming language is a symbol that tells the compiler or interpreter to perform a specific mathematical, relational, or logical operation and produce the final result.
The operators in python are divided into the following groups:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Identify Operators
- Membership Operators
- Bitwise Operators
Let’s look at all the operator groups and operators belonging to a particular group.
Arithmetic Operators:
Arithmetic operators are used to performing the common mathematical operations on numbers.
Operator | Operator Name | Description | Syntax |
+ | Addition | Adds the two operands | a+b |
– | Subtraction | Subtracts the right operand from the left operand | a-b |
* | Multiplication | Multiplies two operands | a*b |
/ | Division | Divides the first operand and by second | a/b |
% | Modulus | Returns reminder when the first operand is divided by the second | a%b |
// | Floor division | Returns floor value of quotient when the first operand is divided by the second. | a//b |
** | Power | Returns first raised to power second. | a**b |
Code:
Python Code
a = 8
b = 4
# Addition
add = a+b
print(add)
# subtraction
sub = a-b
print(sub)
# Multiplication
mult = a*b
print(mult)
# Division
div = a/b
print(div)
# Modulus
mod = a % b
print(mod)
# Floor Divison
floor_div = a//b
print(floor_div)
# Power
pow = a**b
print(pow)
Output:
12
4
32
2.0
0
2
4096
Assignment Operators:
Assignment operators are used to assigning values to the variables.
Operator | Operator Name | Description | Syntax |
= | Equal to | Assign the value at the right side of the expression to the left side variable. | a = b+c |
+= | Add AND assignment | Adds the right operand with the left operand and assigns the result to the left operand. | a+=bOra = a +b |
-= | Subtract AND assignment | Subtracts the right operand from the left operand and assigns the result to the left operand. | a-=bOra = a -b |
*= | Multiply AND assignment | Multiply the right operand and left operand and assign the result to the left operand. | a*=bOra = a*b |
/= | Divide AND assignment | Divides the left operand with the right operand and assigns the result to the left operand. | a/=bOra = a/b |
//= | Floor DivideAND assignment | Divides the left operand with the right operand and assigns the floor of the result to the left operand. | a//=bOra = a//b |
%= | Modulus AND assignment | Assigns the Modulus to the left operand when the left operand is divided by the right operand. | a%=bOra = a%b |
<<= | Left Shift AND assignment | Compute the value of the left operand Bitwise left shift to the right operand times and assign the result to the left operand. | a<<=bOra = a<<b |
>>= | Right Shift AND assignment | Compute the value of the left operand Bitwise right shift to the right operand times and assign the result to the left operand. | a>>=bOra = a>>b |
&= | Bitwise AND assignment | Computes the value of bitwise AND operation on left, and right operands and assigns the result to the left operand. | a&=bOra = a&b |
|= | Bitwise OR assignment | Computes the value of bitwise OR operation on left, and right operands and assigns the result to the left operand. | a|=bOra = a|b |
^= | Bitwise XOR assignment | Computes the value of bitwise XOR operation on left, and right operands and assigns the result to the left operand. | a^=bOra = a^b |
**= | Exponent ANDassignment | Computes the value of left operand raised to power right operand and assigns the result to left operand. | a**=bOra = a**b |
Code:
Python Code
a = 8
b = 4
# Equal to
a = b
print(a)
# Add AND assignement
a += b
print(a)
# Add AND assignement
a -= b
print(a)
# Subtract AND assignement
a *= b
print(a)
# Divide AND assignement
a /= b
print(a)
# Floor Divide AND assignement
a //= b
print(a)
# Modulus AND assignement
a %= b
print(a)
x = 4
y = 3
# Left Shift AND assignement
x <<= y
print(x)
# Right shift AND assignement
x >>= y
print(x)
# Exponent AND assignment
x **= y
print(x)
Output:
4
8
4
16
4.0
1.0
1.0
32
4
64
Comparison Operators :
Comparison operators are used to comparing two values.
Operator | Operator Name | Description | Syntax |
== | Equal | Returns True if both operands are Equal | a == b |
!= | Not Equal | Returns True when both operands are not Equal | a !=b |
> | Greater than | Return True if the left operand is greater than the right operand | a > b |
< | Less than | Return True if the Left operand is Less than the right operand | a < b |
>= | Greater than or Equal to | Return True if the left operand is greater than or equal to the right operand | a >= b |
<= | Less than or Equal to | Return True if the Left operand is less than or equal to the right operand | a <= b |
Code:
Python Code
a = 40
b = 34
#a == b is False
print(a == b)
#a != b is True
print(a != b)
#a > b is True
print(a > b)
# a < is False
print(a < b)
#a >= b is True
print(a >= b)
#a <= b is False
print(a <= b)
Output:
False
True
True
False
True
False
Logical Operators :
Logical Operators are used to combining the conditional Statements.
Operator | Operator Name | Description | Syntax |
and | Logical AND | Returns True when both statements are True | S1 and S2 |
or | Logical OR | Returns True when any one of the statements is True | S1 or S2 |
not | Logical NOT | If the result is True, returns False. If the result is False, return True | not S1 |
NOTE: S1 and S2 are conditional Statements.
Code:
Python Code
a = 45
b = 353
# Logical AND
#a < 50 (True) , b > 350 (True)
# True and True = True
print(a < 50 and b > 350)
# Logical OR
#a > 30 (True), b < 350 (False)
# True | False = True
print(a > 30 or b < 350)
# logical Not
#a > 50 (False)
# not False = True
print(not (a > 50))
Output:
True
True
True
Identify Operators :
They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal do not imply that they are identical.
Operator | Description | Syntax |
is | Returns true if both variables are the same object | a is b |
Is not | Returns True if both variables are not the same object. | a is not b |
Code:
Python Code
x = 20
y = 30
z = x
# Example 1 :
# x and y are differnt objects
# x is not y = True
print(x is not y)
# z and x are same objects
# x is z = True
print(x is z)
Output:
True
True
Membership Operators :
These are used to check whether a value or variable is in sequence data types like list, tuple, or strings.
operator | Description | Syntax |
in | Returns true if the value or variable found in the Sequence | a in L |
Not in | Returns True if the value or variable is not found in the Sequence | a not in L |
NOTE: a is value or variable. L is the sequence
Code:
Python Code
L = [10, 20, 30, 40, 50]
x = 30
# checking variable x is present in L or not
if x in L:
print("x is present in the List L")
else:
print("x is not present in the List L")
# checking value 60 present in L or not
if 60 in L:
print("60 is present in List L")
else:
print("60 is not present in List L")
Output:
x is present in the List L
60 is not present in List L
BitWise Operators :
Bitwise acts on the bits of the number.
Operator | Operator Name | Description | Syntax |
& | Bitwise AND | Performs Bitwise AND operation on each bit of the numbers | a&b |
| | Bitwise OR | Performs Bitwise OR operation on each bit of the numbers | a|b |
^ | Bitwise XOR | Performs Bitwise XOR operation on each bit of the numbers | a^b |
~ | Bitwise NOT | Inverse the all the bits of Number | ~a |
<< | Left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off | a<<b |
>> | Right Shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off | a>>b |
Let a = 45 (101101) b = 60(111100) Let’s see how the Bitwise operators work on each bit.
a | b | a&b | a|b | a^b |
1 | 1 | 1 | 1 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 1 | 1 | 1 | 0 |
0 | 0 | 0 | 0 | 1 |
1 | 0 | 0 | 1 | 0 |
a&b (101100) = 44 a|b (111101) = 61 a^b (010010) = 17
Code:
Python Code
a = 45
b = 60
# Bitwise AND Operation
print("Bitwise AND operation on 45,60 yields :", a & b)
# Bitwise OR operation
print("\nBitwise OR operation on 45,60 yields : ", a | b)
# Bitwise XOR operation
print("\nBitwise XOR operation on 45,60 yields : ", a ^ b)
# Bitwise NOT operation
print("\nAfter reversing all the bits in 45, then it becomes : ", ~a)
# Left shift
print("\nUpon left shifting bits of 45 by 3 places then it becomes :", a << 3)
# Right Shift
print("\nUpon Right shifting bits of 60 by 3 places then it becomes :", b >> 3)
Output:
Bitwise AND operation on 45,60 yields: 44
Bitwise OR operation on 45,60 yields: 61
Bitwise XOR operation on 45,60 yields: 17
After reversing all the bits in 45, then it becomes: -46
Upon left shifting bits of 45 by 3 places then it becomes: 360
Upon Right shifting bits of 60 by 3 places then it becomes: 7
Special thanks to SaiSri Angajala for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article