Home / Blog / Python Operators | Types of Operators in Python
operators in python

Python Operators | Types of Operators in Python

Types of Operators in Python

We have different types of operators in Python to perform operations on values and variables in Python.

There are 7 types of operators used in almost all programming languages.

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

Assignment Operators

Assignment operators will work for us when we have to assign values to variables.

OperatorsExampleAlternative
=x=5x=5
+=x+=5x=x+5
-=X-=5x=x-5
*=x*=5x=x*5
/=x/=5x=x/5
%=X %=5x=x%5
//=x//=5x=x//5
**=x**=3x=x**5
&=X &=5x=x&5
!=x! =5x=x|5
^=x^=5x=x^5
>>=x=x>>=5x=x>>5
<<=x<<=5x=x<<5

Arithmetic Operators

We can apply arithmetic operators on numeric values to perform some mathematical operations:

OperatorsNameExample
+Additionx+ y
Subtractionx- y
*Multiplicationx* y
/Divisionx/ y
%Modulusx% y
**Exponentiationx** y
//Floor Divisionx//y

Comparison Operator

We can compare two values by using comparison operators.

OperatorsNameExample
==Equal operatorx== y
!=Not operatorx! =  y
Greater operatorx> y
Less than operatorx< y
>=Less than or equal to the operatorx>= y
<=Less than or equal to operatorx<= y

Logical Operators

OperatorDescriptionExample
andReturn True if both statements are truex<20 and x<50
orReturns True if one of the statements is truex<20 or x<10
notReverse the result, returns False if the result is truenot (x<5 and x<10)

Identity Operators

Identity operators are used to compare the objects, not if they are equal, but if they are the same object, with the same memory location:

OperatorDescriptionExample
isReturns true if both variables are the same objectx is y
is notReturns true if both variables are not the same objectx is not y

Membership Operators

Membership operators are used to test if a sequence is presented in an object:

OperatorDescriptionExample
inReturns True if a sequence with the specified value is present in the objectx in y
 not inReturns True if a sequence with the specified value is not present in the objectx not in y

Bitwise Operators

Bitwise operators are used to compare (binary) numbers:

OperatorsNameDescription
&ANDSets each bit to 1 if both bits are 1
|ORSet each bit to 1 if one of two bits is 1.
^XORSet each bit to 1 if only one of two bits is 1.
~NOTInvert all the bits
<< Zero fill left shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off.
>> Signed right shiftZero-fill left shift

Author: TCF Editorial

Copyright The Cloudflare.

Post navigation