Facebook Page Twitter Page LinkedIn Page
× Python Tutorial Python Advanced Python Resources Python Libraries


Operators are used to perform operation on two operands. Operators in Python can be categorized as follows:

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

Arithmetic operators

Arithmetic operators are used to perform arithmetic operations on two operands.

OperatorNameDescription
+AdditionAdd two values
-SubtractionSubtract one value from another
*MultiplicationMultiply two values
/DivisionDivide one value by another
**Exponent / PowerReturns first operand raised to the power of second operand
%ModulusReturns remainder of division operation
//Floor divisionReturns quotient of division operation

Example

Assignment operators

Assignment operators are used to assign values of right hand side expression to left hand side operand.

Operator Expression Equivalent toExample
=a = 5a = 5Example
+=a += ba = a + b
-=a -= ba = a - b
*=a *= ba = a * b
/=a /= ba = a / b
**=a **= ba = a ** b
%=a %= ba = a % b
//=a //= ba = a // b
&=a &= ba = a & bMore Info
|=a |= ba = a | bMore Info
^=a ^= ba = a ^ bMore Info
>>=a >>= ba = a >> bMore Info
<<=a <<= ba = a << bMore Info

Comparison operators

Comparison operators are used to compare values of two operands. It returns true when values matches and false when values doen not match.

Operator Description
==Equal
!= Not equal
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Example

Logical operators

Logical operators are used to combine two or more conditions.

Operator Description
and Returns True when all conditions are true
orReturns True when any of the conditions is true
notReturns opposite boolean result

More Info

Identity operators

Identity operators are used to compare memory location of two objects.

Operator Description
is Returns true when both variables are the same object a is b
is not Returns true when both variables are not the same object a is not b

Membership operators

Membership operators are used to check the membership of element(s) in a sequence like lists, tuple etc.

Operator Description Example
in Returns True if element(s) is present in the object a in b
not in Returns True if element(s) is not present in the object a not in b

Bitwise operators:

Bitwise operators are used to perform bitwise operations on two operands.

OperatorNameDescriptionMore Info
& ANDReturns 1 if both bits at the same position in both operands are 1, else returns 0More Info
|ORReturns 1 if one of two bits at the same position in both operands is 1, else returns 0More Info
^XORReturns 1 if only one of two bits at the same position in both operands is 1, else returns 0More Info
~ NOTReverse all the bitsMore Info
>>Right shiftThe left operand is moved right by the number of bits present in the right operandMore Info
<<Left shiftThe left operand value is moved left by the number of bits present in the right operandMore Info

5