Python Operators

An operator is a symbol that performs an operation on values and variables. You can use the following operator types in Python:

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

Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations like addition, subtraction, multiplication, division, etc.

OperatorDescriptionFormatExample
+Additiona + b3 + 5 = 8
Subtractiona – b10 – 7 = 3
*Multiplicationa * b3 * 4 = 12
/Divisiona / b21 / 7 = 3
&Modulusa % b 23 % 7 = 2
**Exponenta ** b2 * 4 = 16
//Floor Division – Rounds down to nearest whole numbera // b9 // 2 = 4
-9 // 2 = -5

Comparison Operators

Comparison operators are used to compare two values and return a boolean result (true/false).

OperatorDescriptionFormatExample
==Equalitya == b5 == 5
!=Inequalitya != b3 != 5
>Greater thana > b5 > 3
<Less tahna < b3 < 5
>=Greater than or equal toa >= b5 >= 3
<=Less than or equal toa <= b3 <= 5

In Python 2, both != and <> are used as Inequality Operator (a != b) (a <> b).
In Python 3, <> is removed, only != is used for inequality comparison.

Assignment Operators

Assignment operators are used to assign right-side expressions to left-side variables.

OperatorDescriptionFormatEquivalent to
=Assignmenta = ba = b
+=Addition and Assignmenta += ba = a + b
-=Subtraction and Assignmenta -= ba = a – b
*=Multiplication and Assignmenta *= ba = a * b
/=Division and Assignmenta /= ba = a / b
%=Modulus and Assignmenta %= ba = a % b
**=Exponent and Assignmenta **= ba = a ** b
=//Floor Division and Assignmenta //= ba = a // b

Logical Operators

There are 3 logical operators in Python: AND, OR and NOT. These operators are mainly used in conditional statements.

OperatorDescriptionFormatExample
ANDIf both the expression are true, then returns TRUEcondition1
AND contidion2
(6 > 5) AND (5 > 4)
returns TRUE
ORIf at least one of the expressions is true, then returns TRUEcondition1
OR contidion2
(5 > 4) OR (4 > 5)
returns TRUE
NOTIf condition is true, returns FALSE
If condition is false, returns TRUE
NOT(condition)NOT(5 > 4)
returns FALSE

Bitwise Operators

Bitwise operators are used to perform operations on individual bits in a number.

OperatorDescriptionFormat
& (AND)If both bits are 1, resulting bit will be 11010 & 0101 = 1111
| (OR)If either of the bits is 1, resulting bit will be 11010 | 1001 = 1011
^ (XOR)If only one of the bits is 1, resulting bit will be 11010 ^ 1001 = 0011
~ (Bitwise Inversion)This operator calculates ones’ complement of the number~1100 = -1101
<< (Left shift)Each bit in a number is shifted to left0001101 << 2 = 0110100
(Right shift)Each bit in a number is shifted to right0110100 >> 2 = 0001101

Bitwise Inversion of a number x is calculated as -(x+1). For instance, bitwise inversion of 151 (10010111) is -152 (-10011000).

Membership Operators

Membership operators are used to check the appearance of a value within a sequence like lists, strings or tuples. There are two membership operators in Python: IN, NOT IN

For the examples below consider prime list is defined as:

primes = [2, 3, 5, 7, 11]
OperatorDescriptionFormatExample
INReturns TRUE if the value appears in the sequencea in bprint(7 in primes)
returns TRUE
NOT INReturns TRUE if the value does not appear in the sequencea not in bprint(4 not in primes)
returns TRUE

Identity Operators

Identity operators are used to check whether two objects refer to the same object. There are two identity operators in Python: IS, IS NOT

For the examples below consider the code below. Both var1 and var2 have the same content but they are distinct objects in memory. var3, on the other hand, refers to the same object as var1.

var1 = ["Hello", "World"]
var2 = ["Hello", "World"]
var3 = var1

print(var1 is var2)
print(var1 is var3)
OperatorDescriptionFormatExample
ISReturns TRUE if both operands refer to the same objecta is b var1 is var2 returns FALSE
var1 is var3 returns TRUE
IS NOTReturns TRUE if the operands are different objectsa is not bvar1 is not var2 returns TRUE

You can also use identity operators to check the type of an object.
Since the type of var1 is list, following code prints TRUE.

print(type(var1) is list)