Operators in Python

operators in python

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

OperatorMeaningExample
+Add two operands or unary plusx + y +2
Subtract right operand from the left or unary minusx – y -2
*Multiply two operandsx * y
/Divide left operand by the right one (always results into float)x / y
%Modulus – remainder of the division of left operand by the rightx % y (remainder of x/y)
//Floor division – division that results into whole number adjusted to the left in the number linex // y
**Exponent – left operand raised to the power of rightx**y (x to the power y)
This is the table for the arithmetic operators present in python

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.

OperatorMeaningExample
Greater that – True if left operand is greater than the rightx > y
Less that – True if left operand is less than the rightx < y
==Equal to – True if both operands are equalx == y
!=Not equal to – True if operands are not equalx != y
>=Greater than or equal to – True if left operand is greater than or equal to the rightx >= y
<=Less than or equal to – True if left operand is less than or equal to the rightx <= y
These are the comparison operators present in python

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.

OperatorMeaningExample
andTrue if both the operands are truex and y
orTrue if either of the operands is truex or y
notTrue if operand is false (complements the operand)not x
These are the Logical operators present in python

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.

OperatorMeaningExample
&Bitwise ANDx& y
|Bitwise ORx | y
~Bitwise NOT~x
^Bitwise XORx ^ y
>> Bitwise right shiftx>>2
<< Bitwise left shiftx<<2
These are the Bitwise operators present in python

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.

OperatorMeaning
inEvaluates to true if it finds a variable in the specified sequence and false otherwise.
not inEvaluates to true if it does not finds a variable in the specified sequence and false otherwise.
These are the Python’s Membership Operators

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.

OperatorMeaning
isEvaluates to true if the variables on either side of the operator point to the same object and false otherwise.
not isEvaluates to false if the variables on either side of the operator point to the same object and true otherwise.
These are the Python’s Identity Operators

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

OperatorsDescription
( )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 notIdentity operators
in, not inMembership operators
not or andLogical operators
highest precedence to lowest precedence table in python

NOTE : –

Learn Python Programming from Basics– Click here
Watch on YouTube – Click here
5 Tips to Learn python Faster – Click here