Operators in python are special symbols that carry out arithmetic or logical computation. The value that the operator operates on is called the operand. Operators are used to perform basic mathematical operations like addition, subtraction, multiplication, division & much more. Every programming language has it’s operators to perform different task.
Python divides the operators in the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
Arithmetic Operators
Arithmetic Operators are used for mathematical operations
Operator | Meaning | Example |
+ | Add two operands or unary plus | x + y +2 |
– | Subtract right operand from the left or unary minus | x – y -2 |
* | Multiply two operands | x * y |
/ | Divide left operand by the right one (always results into float) | x / y |
% | Modulus – remainder of the division of left operand by the right | x % y (remainder of x/y) |
// | Floor division – division that results into whole number adjusted to the left in the number line | x // y |
** | Exponent – left operand raised to the power of right | x**y (x to the power y) |
For example: –
x = 5
y = 4
print ('x + y =', x + y)
print ('x - y =', x - y)
print ('x * y =', x * y)
print ('x / y =', x/y)
print ('x // y =', x//y)
print ('x ** y =', x**y)
Output
'x + y =', 9
'x - y =', 1
'x * y =', 20
'x / y =', 1
'x // y =', 1
'x ** y =', 625
Comparison Operators
Comparison Operators are used to compare or decide the relation among them.
Operator | Meaning | Example |
> | Greater that – True if left operand is greater than the right | x > y |
< | Less that – True if left operand is less than the right | x < y |
== | Equal to – True if both operands are equal | x == y |
!= | Not equal to – True if operands are not equal | x != y |
>= | Greater than or equal to – True if left operand is greater than or equal to the right | x >= y |
<= | Less than or equal to – True if left operand is less than or equal to the right | x <= y |
For example: –
x = 101
y = 121
print ('x > y is', x>y)
print ('x < y is', x<y)
print ('x == y is', x==y)
print ('x != y is', x!=y)
print ('x >= y is', x>=y)
print ('x <= y is', x<=y)
Output
'x > y is', False
'x < y is', True
'x == y is', False
'x != y is', True
'x >= y is', False
'x <= y is', True
Logical Operators
Logical operators are used on conditional statements. Logical operators are used to check whether an expression is True or False.
Operator | Meaning | Example |
and | True if both the operands are true | x and y |
or | True if either of the operands is true | x or y |
not | True if operand is false (complements the operand) | not x |
For example
x = True
y = False
print ('x and y is', x and y)
print ('x or y is', x or y)
print ('not x is', not x)
Output
'x and y is', False
'x or y is', True
'not x is', False
Bitwise operators
Bitwise operators to perform Boolean logic on individual bits. Bitwise AND operator; Bitwise OR operator; Bitwise not operator; Bitwise XOR operator.
Operator | Meaning | Example |
& | Bitwise AND | x& y |
| | Bitwise OR | x | y |
~ | Bitwise NOT | ~x |
^ | Bitwise XOR | x ^ y |
>> | Bitwise right shift | x>>2 |
<< | Bitwise left shift | x<<2 |
For example:
a = 6
b = 3
print ('a=',a,':', bin(a),'b=',b,':', bin(b))
c = 0
c = a & b;
print ("result of AND is ", c,':', bin(c))
c = a | b;
print ("result of OR is ", c,':', bin(c))
c = a ^ b;
print ("result of EXOR is ", c,':', bin(c))
c = ~a;
print ("result of COMPLEMENT is ", c,':', bin(c))
c = a << 2;
print ("result of LEFT SHIFT is ", c,':', bin(c))
c = a >> 2;
print ("result of RIGHT SHIFT is ", c,':', bin(c))
Output
'a=', 6, ':', '0b110', 'b=', 3, ':', '0b11'
'result of AND is ', 2, ':', '0b10'
'result of OR is ', 7, ':', '0b111'
'result of EXOR is ', 5, ':', '0b101'
'result of COMPLEMENT is ', -7, ':', '-0b111'
'result of LEFT SHIFT is ', 24, ':', '0b11000'
'result of RIGHT SHIFT is ', 1, ':', '0b1'
Python Membership Operators
Python offers two membership operators to check or validate the membership of a value.
Operator | Meaning |
in | Evaluates to true if it finds a variable in the specified sequence and false otherwise. |
not in | Evaluates to true if it does not finds a variable in the specified sequence and false otherwise. |
For example:-
a = 5
b = 10
list = [1, 2, 3, 4, 5 ]
if ( a in list ):
print ("Line 1 - a is available in the given list")
else:
print ("Line 1 - a is not available in the given list")
if (b not in list):
print ("Line 2 - b is not available in the given list")
else:
print ("Line 2 - b is available in the given list")
Output
Line 1 - a is available in the given list
Line 2 - b is not available in the given list
Python Identity Operators
Python Identity Operators Example ; is, Evaluates to true if the variables on either side of the operator point to the same object and false otherwise.
Operator | Meaning |
is | Evaluates to true if the variables on either side of the operator point to the same object and false otherwise. |
not is | Evaluates to false if the variables on either side of the operator point to the same object and true otherwise. |
For example: –
a = 10
b = 10
print ('Line 1','a=',a ,':',id(a), 'b=',b ,':',id(b))
if ( a is b ):
print ("Line 2 - a and b have same identity")
else:
print ("Line 2 - a and b do not have same identity")
Output
'Line 1', 'a=', 10, ':', 20839436, 'b=', 10, ':', 20839436
Line 2 - a and b have same identity
Operators Precedence
The operator precedence in Python is listed below. It is in descending order (upper group has higher precedence than the lower ones).
Highest precedence to lowest precedence table
Operators | Description |
( ) | Parentheses (grouping) |
** | Exponentiation (raise to the power) |
~+ – | Complement, unary plus and minus (method names for the last two are +@ and -@) |
*/ % // | Multiply, divide, modulo and floor division |
+ – | Addition and subtraction |
>> << | Right and left bitwise shift |
& | Bitwise ‘AND’ |
^| | Bitwise exclusive `OR’ and regular `OR’ |
<= < > >= | Comparison operators |
< > == != | Equality operators |
= %= /= //= -= += *= **= | Assignment operators |
is, is not | Identity operators |
in, not in | Membership operators |
not or and | Logical operators |
NOTE : –
Learn Python Programming from Basics– Click here
Watch on YouTube – Click here
5 Tips to Learn python Faster – Click here