Home » Python Tutorial » Python Operators

Python Operators


Reading Time - 2 mins

Since we have covered all the built-in data types of python, in this tutorial we can look into the operators we can perform on the variables using the python operators.

In Python, the operators are divided into the following operators

Types of Operators in Python

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators

Arithmetic Operators in Python

Arithmetic operators are used for performing arithmetic operations on variables like addition, subtraction, multiplication, division, etc.

OperatorNameExample
+Additionx+y
Subtractionx-y
*Multiplicationx*y
/Divisionx/y
%Modulus(Returns the remainder of the division operation)x%y
**Exponential Operatorx**y
//Floor division operator(Result of the division operation without decimal points)x//y

Examples:

x=10
y=5
print(x+y)
print(x-y)
print(x*y)
print(x/y)
print(x%y)
print(x//y)

Assignment Operators in Python

Assignment operators are used for assigning values to variables.

OperatorExample
=x=5
+=x+=5 same as x=x+5
-=x-=5 same as x=x-5
*=x*=5 same as x=x*5
/=x/=5 same as x=x/5
//=x//=5 same as x=x//5
%=x%=5 same as x=x%5
&=x&=5 same as x=x&5
|=x|=5 same as x=x|5
^=x^=5 same as x=x^5
>>=x>>=5 same as x=x>>5
<<=x<<=5 same as x=x<<5

In the above tables, these ‘&’,’|’,’^’,’>>’,'<<‘ are bitwise operators.

x=10
print(x)
x+=5
print(x)
x-=5
print(x)
x*=5
print(x)
x/=5
print(x)
x//=5
print(x)
x%=5
print(x)
x=10
x&=5
print(x)
x!=5
print(x)
x^=5
print(x)
x<<=5
print(x)
x>>=5
print(x)

Comparison Operators in Python

As the name implies, comparison operators are used for comparing two values.

READ  Python Dictionary
OperatorNameExample
==Equal tox==5
!=Not equal tox!=5
>Greater thanx>5
<Lesser thanx<5
>=Greater than or equal tox>=5
<=Lesser than or equal tox<=5

As we have covered Arithmetic, Assignment, and comparison operators above, now we can cover the remaining operators below.

x=10
print(x==5)
print(x!=5)
print(x>5)
print(x<5)
print(x>=5)
print(x<=5)

Logical Operators in Python

Logical operators are used for combining two conditional statements.

OperatorDescriptionExample
andReturns True only if all the conditions are Trueif x>=5 and x<=10
orReturns True if one of the two conditions is Trueif x<5 or x<10
notReverses the result i.e Returns True if the result of the conditions is False and vice versaif not (if x>=5 and x<=10)

Examples of logical operators are covered in the “If Else” tutorial.

x=10
print(x>=5 and x<=10)
print(x<5 or x<10)
print(not(x>=5 and x<=10))

Identity Operators in Python

Identity operators are used for comparing objects, not if they are equal. It checks for, whether the objects are the same and shares the same memory location.

OperatorDescriptionExample
isReturn True if both are variables of the same objectx is y
is notReturns True if both are not variables of the same objectx is not y

An example is the same as the one we have seen for deep and shallow copy in the “Lists” tutorial.

l=[1,2,3]
l1=l.copy()
l2=l
print(l1 is not l)
print(l2 is l)

Membership Operators in Python

We have already covered this in the “Lists” tutorial. It checks whether the object is present in a sequence.

OperatorDescriptionExample
inReturn True if the object is present in the sequence1 in [1,2,3]
not inReturns True if the object is not present in the sequence4 not in [1,2,3]
l=[1,2,3]
print(1 in l)
print(1 not in 1)

Bitwise Operators in Python

Bitwise operators are used for comparing binary numbers.

READ  Python Lambda
OperatorNameExample
&ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if one of the two bits is 1
^XORSets each bit to 1 if only one of the two bits is 1
~NOTInverts all the bits
>>SIGNED RIGHT SHIFTShifts right by pushing copies of the leftmost bit from the left, and let the rightmost bits fall off
<<ZERO LEFT SHIFTShifts left by pushing zeros in from the right and let the leftmost bits fall off
x=5
print(x&5)
print(x|5)
print(x^5)
print(~x)
print(x>>5)
print(x<<5)

Reference

Reference1

Python Basic Syntax

Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions