Python Operators

6 minute read

Published:

Operators are the constructs that enable performing operations on operands(values and variables). The operators in python are represented by special symbols and keywords. The intentions of this blog is to familiarize with the various operators in Python.

Arithmetic Operators

These operators are used to perform mathematical operations ranging from addition, subtraction, multiplication, division to modulus, exponent, etc. Following table shows the arithmetic operators and it’s usage:

OperatorUsageDescription
+a + bAdd values on either side of the operator or unary plus
-a -bSubtract right hand operand from the left hand operand. Also unary negation
*a * bMultiply values on either side of the operator
/a / bDivides left hand operand by right hand operand
%a % bReturns the remainder from dividing left hand operand by right hand operand
**a ** bReturns Exponent – left operand raised to the power of right
//a //bFloor Division – The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity) −

Comparison Operators

The comparison operators are used to identify relation between operands on either side of the operator. These are also called relational operators. The values from the comparison operators is either True or False.

OperatorDescriptionUsage
==Returns True if the values on the either side of the operator is equal otherwise False.a == b
!=Returns True if the values on either sides of the operator is not equal to each other otherwise False.a != b
>Returns True if the value of the operand on the left of the operator is greater than the value on the right side of the operator.a >b
<Returns True if the value of the operand on the left of the operator is less than the value on the right side of the operator.a < b
>=Returns True if the value of the operand on the left of the operator is greater than or equal to the value on the right side of the operator.a >= b
<=Returns True if the value of the operand on the left of the operator is less than or equal to the value on the right side of the operator.a <= b

Assignment Operators

Assignment operators are used for assigning the value from the right operand of the operator to the left operand. Following is the various assignment operators in Python:

OperatorDescriptionUsageEquivalent to
=Assigns values from right side operands to left side operandc = a + bc = a + b
+=Adds the value of right operand to the value of left operand and assign the result to the left operandb += ab = b + a
-=Subtracts the value of right operand from the value of left operand and assign the result to left operandb -= ab = b – a
*=Multiplies the value of right operand with the value of the left operand and assigns the result to left operandb *= ab = b * a
/=Divides the value of the left operand with the value of the right operand and assigns the result to the left operandb /= ab = b / a
%=Assigns the remainder from dividing left hand operand by right hand operand to the left hand operandb %= ab = b % a
**=Assigns the value from the exponential operation to the left operand.b **= ab = b ** a
//=Performs floor division on operators and assign value to the left operandb //= a is equivalent tob = b // a

Python Logical Operators

Logical operators in python are used for conditional statements which evaluates to either true or false. AND, OR, NOT are the logical operators in python.

OperatorDescriptionUsage
andTrue if both sides of the operator is Truex and y
orTrue if either of the operand is Truex or y
notComplements the operandnot val

Membership Operator

These operators test for membership(presence) in a sequence such as string, list or tuple. Following are the membership operators:

OperatorDescriptionUsage
inTrue if the value/operand in the left of the operator is present in the sequence in the right of the operator.x in y
not inTrue if the value/operand in the left of the operator is not present in the sequence in the right of the operator.x not in y

Identity Operator

It is used to compare the memory location of two python objects .i.e both the operands refer to the same object.

OperatorDescriptionUsage
isTrue if both the operands refer to the same object.x is True
is notEvaluates to false if the variables on either side of the operator point to the same object and true otherwise.x is not True

Bitwise Operators

Bitwise operators work on bits of an operand hence the name.

>>> # Bitwise AND
...
>>> a = 3
>>> b = 4
>>> a = 3 # equivalent binary is 0011
>>> b = 4 # equivalent binary is 0100
>>> a & b
0
>>>
>>> # Bitwise OR
...
>>> a | b
7
>>> # 7 is equivalent to 0111 in binary
...
>>>
>>> # Bitwise NOT
...
>>> ~ a
-4
>>>
>>> # Bitwise XOR
...
>>> a ^ b
7
>>>
>>> # Bitwise right shift
...
>>> a >> 2
0
>>>
>>> # Bitwise left shift
...
>>> a << 2
12