Python Operators

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 NameDescriptionSyntax
+AdditionAdds the two operandsa+b
SubtractionSubtracts the right operand from the left operanda-b
*MultiplicationMultiplies two operandsa*b
/DivisionDivides the first operand and by seconda/b
%ModulusReturns reminder when the first operand is divided by the seconda%b
//Floor divisionReturns floor value of quotient when the first operand is divided by the second.a//b
**PowerReturns 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.

OperatorOperator NameDescriptionSyntax
=Equal toAssign the value at the right side of the expression to the left side variable.a = b+c
+=Add AND assignmentAdds the right operand with the left operand and assigns the result to the left operand.a+=bOra = a +b
-=Subtract AND assignmentSubtracts the right operand from the left operand and assigns the result to the left operand.a-=bOra = a -b
*=Multiply AND assignmentMultiply the right operand and left operand and assign the result to the left operand.a*=bOra = a*b
/=Divide AND assignmentDivides the left operand with the right operand and assigns the result to the left operand.a/=bOra = a/b
//=Floor DivideAND assignmentDivides the left operand with the right operand and assigns the floor of the result to the left operand.a//=bOra = a//b
%=Modulus AND assignmentAssigns the Modulus to the left operand when the left operand is divided by the right operand.a%=bOra = a%b
<<=Left Shift AND assignmentCompute 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 assignmentCompute 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 assignmentComputes 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 assignmentComputes 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 assignmentComputes the value of bitwise XOR operation on left, and right operands and assigns the result to the left operand.a^=bOra = a^b
**=Exponent ANDassignmentComputes 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.

OperatorOperator NameDescriptionSyntax
==EqualReturns True if both operands are Equala == b
!=Not EqualReturns True when both operands are not Equala !=b
>Greater thanReturn True if the left operand is greater than the right operanda > b
<Less thanReturn True if the Left operand is Less than the right operanda < b
>=Greater than or Equal toReturn True if the left operand is greater than or equal to the right operanda >= b
<=Less than or Equal toReturn True if the Left operand is less than or equal to the right operanda <= 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.

OperatorOperator NameDescriptionSyntax
andLogical ANDReturns True when both statements are TrueS1 and S2
orLogical ORReturns True when any one of the statements is TrueS1 or S2
notLogical NOTIf the result is True, returns False. If the result is False, return Truenot 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.

OperatorDescriptionSyntax
isReturns true if both variables are the same objecta is b
Is notReturns 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. 

operatorDescriptionSyntax
inReturns true if the value or variable found in the Sequencea in L
Not inReturns True if the value or variable is not found in the Sequencea 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. 

OperatorOperator NameDescription Syntax
&Bitwise ANDPerforms Bitwise AND operation on each bit of the numbersa&b
|Bitwise ORPerforms Bitwise OR operation on each bit of the numbersa|b
^Bitwise XORPerforms Bitwise XOR operation on each bit of the numbersa^b
~Bitwise NOTInverse the all the bits of Number~a
<<Left shiftShift left by pushing zeros in from the right and let the leftmost bits fall offa<<b
>>Right ShiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall offa>>b
Let a = 45 (101101) b = 60(111100) 
Let’s see how the Bitwise operators work on each bit.
aba&ba|ba^b
11110
01011
11110
11110
00001
10010
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