Operators in Python






Like every other programming language, Python has numerous operators. Some of those operators have similar functionalities in most
programming languages. Operators can be categorized according to their functionality and the data type of the expression. These can be
categorized as below.

  • Arithmetic Operator
  • Relational Operator
  • Logical Operator
  • Assignment Operator
  • Bitwise Operator
  • Membership Operator
  • Identity Operator

Now let's look into detail of each types of operators in Python.


Arithmetic Operators:

These operators are used in performing arithmetic operation like addition, subtraction, multiplication and division. These are also binary
operator means it perform on two operands.

Operator Symbol Description Usage Example (in IDLE)
Addition + Adds two numbers or variables >>>5+10
15
OR
(if a=5 and b= 10)
>>> a + b
15
Subtraction - Subtract two numbers or variables >>>10 - 5
5
OR
(if a=5 and b= 10)
>>> a - b
5
Multiplication * Multiply two numbers or variabless >>>10 * 5
50
OR
(if a=5 and b= 10)
>>> a * b
50
Division / Divide first operand by the second
operand
>>>10 / 5
2
OR
(if a=50 and b= 10)
>>> a / b
5
Modulo % Divide first operand by the second
Operand and return the remainder
>>>10 % 5
0
OR
(if a=50 and b= 8)
>>> a % b
2
Floor Division // Divide first operand by the second
Operand and return the whole number
without the decimal value.
>>>22 // 7
3
OR
(if a = 22 and b = 7)
>>> a // b
3
Exponent ** Raise the first operand to the power
of second
>>>3 ** 3
27
OR
(if a = 4 and b = 2)
>>> a ** b
16


Relational Operators:

These are the operators which compare the values of two operand and check the relation between them. These are also binary operator.

Operator Symbol Description Usage Example (in IDLE)
Equal to == Check two operand and return "True"
if they are equal else will return "False"
>>>3 == 3
True
OR
(if a=4 and b= 2)
>>> a == b
False
Not Equal to != Check two operand and return "True"
if they are not equal else will return
"False"
>>>3 != 3
False
OR
(if a=4 and b= 2)
>>> a != b
True
Greater than > Check if the left operand is greater than
right operand and return "True" else return
"False
>>>10 > 5
True
OR
(if a=8 and b= 18)
>>> a > b
False
Less than < Check if the left operand is smaller than
right operand and return "True" else
return "False
>>>10 < 5
False
OR
(if a=8 and b= 18)
>>> a < b
True
Greater than
or Equal to
>= Check if the left operand is greater than
or equal to the right operand and
return "True" else return "False
>>>5 >= 5
True
OR
(if a=8 and b= 18)
>>> a >= b
False
Less than
or Equal to
<= Check if the left operand is smaller than
or equal to the right operand and
return "True" else return "False
>>>5 <= 5
True
OR
(if a=28 and b= 18)
>>> a <= b
False


Logical Operators:

Like other language logical operator are also supported by the Python.

Operator Symbol Description Usage Example (in IDLE)
Logical AND and Return "True" only if both the left and right
operand value are true else will return "False"
(if a= true and b=false)
>>>a and b
False
OR
(if a= true and b=true)
>>> a and b
True
Logical OR or Return "True" if any of the left or right
operand value are true else will return "False"
(if a= true and b=false)
>>>a or b
True
OR
(if a= false and b=false)
>>> a or b
False
Logical NOT NOT Return the reverse of the logical state of the
Operator.
(if a= true and b= false)
>>>not a
False
OR
>>> not b
True

Logical operators works on the basis of a guided principle known as Truth Table. Below is the truth table for the same.

Left operand (a) Right operand (b) a and b a or b NOT a NOT b
True False FALSE TRUE FALSE TRUE
False True FALSE TRUE TRUE FALSE
True True TRUE TRUE FALSE FALSE
False False FALSE FALSE TRUE TRUE


Bitwise Operator:

Bitwise operator are often used whenever there is the need of bit by bit calculation. For further explanation let us take two variables
a and b having values "127" and "111" respectively. Then their binary representation will be as below.
a = 0111 1111
b = 0110 1111
Now in the example section we will check the result of different types of bitwise operator on these variables.

Operator Symbol Description Usage Example (in IDLE)
Bitwise AND & This will return 1 only if bits of both operand
will have value 1 at the same bit position else
it returns 0.
>>>a & b
111
Bitwise OR | This will return 1 if bits of any of the operand
have the value 1 at the same bit position else
it returns 0.
>>>a | b
127
Exclusive OR (XOR) ^ This will return 1 if bits of the two operand are
different else it will return 0 if bits of two
operand are same.
>>>a^b
16
Ones Compliment ~ This will flip each bits of the operand and
negate the value.
>>> ~b
-111
Left Shift << This will change each bits of the left operand in
the left direction by the number of times as
mentioned in right operand.
>>> a<<2
-111

(binary equivalent is
0001 1111 1100)
Right Shift >> This will change each bits of the left operand in
the right direction by the number of times as
mentioned in right operand.
>>> a>>2
31

(binary equivalent is
0001 1111)

Below is the truth table for the bitwise operator:

Left operand (a) Right operand (b) a & b a | b a^b
1 0 0 1 1
0 1 0 1 1
1 1 1 1 0
0 0 0 0 0


Assignment Operators:

Below are the different types of assignment operator as supported by the Python.

Operator Symbol Description Usage Example (in IDLE)
assign value = Assign the value of right operand to the
left operand.
>>> a = 90
>>> a
90
add and assign += Adds the values of the left operand and
right operand and assign the result to the
left operand.
>>> a = 100
>>> a += 20
>>> a
120
subtract and assign -= Subtract the values of the left operand and
right operand and assign the result to the
left operand.
>>> a = 100
>>> a -= 20
>>> a
80
multiply and assign *= Multiply the values of the left operand and
right operand and assign the result to the
left operand.
>>> a = 100
>>> a *= 2
>>> a
200
divide and assign /= Divide the value of the left operand by the value of
right operand and assign the result to the
left operand.
>>> a = 100
>>> a /= 2
>>> a
50
modulo and assign %= Divide the value of the left operand by the value of
right operand and assign the remainder to the
left operand.
>>> a = 16
>>> a %= 3
>>> a
1
floor divide and assign //= Do the floor division and assign the result to the
right operand.
>>> a = 50
>>> a //= 6
>>> a
8
exponent and assign **= Do the exponential calculation and assign the
result to the left operand.
>>> a = 4
>>> a **= 2
>>> a
16


Membership Operators:

This operator is used to test the membership of a data in a sequence like list, tuples or string.

Operator Symbol Description Usage Example (in IDLE)
In in Checks the occurance of a variable in the sequence and return
"True" if variable is found else return "False"
>>> x= "House in the Street"
>>> y= "House"
>>> z= "Flat"
>>> y in x
True
>>> z in x
False
Not In not in Checks the occurance of a variable in the sequence and return
"True" if variable is not present else return "False"
>>> x= "House in the Street"
>>> y= "House"
>>> z= "Flat"
>>> y not in x
False
>>> z not in x
True


Identity Operators:

This operator checks and compare the memory location of the two variables.

Operator Symbol Description Usage Example (in IDLE)
Is is Checks the identity of the variables and return
"True" in case of same identity else return "False"
>>> x = "test"
>>> id(x)
31716544
>>> id("test")
31716544
>>> x is "test"
True
>>> x*2
'testtest'
>>> x*2 is "test"
False
Not Is is not Checks the identity of the variables and return
"True" in case of different identity else return "False"
>>> x = "test"
>>> id(x)
31716544
>>> id("test")
31716544
>>> x is not "test"
False


Operator's Precedence:

Operator precedence defines the order in which the mathematical expression will be evaluated. An operator with a higher precedence
will be evaluated first and the operator with the lowest precedence will operate last.The below table shows the operator arrangement
according to their precedence.

Precedence Operator description Operator
Highest Exponential **
Complement, Unary plus minus ~, +, -
Arithmetic (Except add and subtract) *, /, %, //
Add and Subtract +, -
Bitwise shift operator >>, <<
Bitwise AND operator &
OR and XOR operator |, ^
Relational operators -1 <= , =>, > , <
Relational operators -2 ==, !=
Assignment operators =, +=, -=, *=, /=, %=, **=
Identity operators is, is not
Membership operators in, not in
Lowest Logical operators and, or, not