Operators in Python

An operator in Python is a symbol which performs some mathematical operations between operands. For example, an assignment operator(=) is used to store values in variables, an addition operator(+) is used to add numbers.

Python provides us several groups of operators. These are as follows:

  1. Arithmetic operators
  2. Comparison operators
  3. Bitwise operators
  4. Assignment operators
  5. Logical operators
  6. Identity operators
  7. Membership operators

Python Arithmetic Operators

Arithmetic operators are used to perform simple mathematical operations like addition, subtraction, multiplication, etc. Arithmetic operators are as follows:

Operator SymbolOperator NameExample
+Additiona + b
Subtractiona – b
*Multiplicationa * b
/Divisiona / b
%Modulusa % b
**Exponenta ** b
//Floor divisiona // b

Example 1: Arithmetic Operators in Python

# A program to demonstrate arithmetic operators in Python

a = 3
b = 2

# Addition operator
print('a + b = ', a+b)

# Subtraction operator
print('a - b = ', a-b)

# Multiplication operator
print('a * b = ', a*b)

# Division operator
print('a / b = ', a/b)

# Modulus operator
print('a % b = ', a%b)

# Exponent operator
print('a ** b = ', a**b)

# Floor division operator
print('a // b = ', a//b)

Output:

a + b =  5
a - b =  1
a * b =  6
a / b =  1.5
a % b =  1
a ** b =  9
a // b =  1

Python Comparison Operators

Comparison operators are used to compare two operands. The result of comparison is always True or False.

Operator SymbolOperator NameExample
==Equala == b
!=Not equala != b
<Less thana < b
>Greater thana > b
<=Less than or equal toa <= b
>=Greater than or equal toa >= b

Example 2: Comparison Operators in Python

# A sample program to demonstrate comparison operators in Python
a = 10
b = 20

# Equal operator 
print('a == b is: ', a==b)

# Not equal operator
print('a != b is: ', a!=b)

# Less than operator
print('a < b is: ', a<b)

# Greater than operator
print('a > b is: ', a>b)

# Less than or equal operator
print('a <= b is: ',a<=b)

# Greater than or equal operator
print('a >= b is: ', a>=b)

Output:

a == b is:  False
a != b is:  True
a < b is:  True
a > b is:  False
a <= b is:  True
a >= b is:  False

Python Bitwise Operators

Bitwise operators are used to perform bitwise operations(operations on bits). This is done by converting a given number into its binary equivalent and then performing bit by bit operations.

SymbolNameMeaningExample
&Bitwise ANDResult is 1 if both bits are 1a & b
|Bitwise ORResult is 1 if any of the two bits is 1a | b
~Bitwise NOTReverts the bit~ a
^Bitwise XORResult is 1 if both bits are not the samea ^ b
<<Left shiftShifts each bit towards left a << 1
>>Right shiftShifts each bit towards righta >> 1

Example 3: Bitwise Operators in Python

# A program to demonstrate bitwise operators in Python

a = 2 # Binary equivalent is: 1 0
b = 3 # Binary equivalent is: 1 1

# Bitwise AND
print('a & b is: ', a&b)

# Bitwise OR
print('a | b is: ', a|b)

# Bitwise NOT
print(' ~ a is: ', ~a)

# Bitwise XOR
print('a ^ b is: ', a^b)

# Left shift
print('a << 1 is: ', a<<1)  # Shifts each bit towards left by 1

# Right shift
print('a >> 1 is: ', a>>1) # Shifts each bit towards right by 1

Output:

a & b is:  2
a | b is:  3
 ~ a is:  -3
a ^ b is:  1
a << 1 is:  4
a >> 1 is:  1

Python Assignment Operators

Assignment operators are used to assign values to variables. Assignment is done using assignment operator ( = ). The assignment operator can also be used as a compound operator like +=, -=, etc.

# Simple assignment
a = 5  
# Compound assignment
a += 5 # same as  a = a + 5

Example 4: Assignment Operators in Python

Operator SymbolExampleEquivalent to
=a = 4a = 4
+=a += 4a = a + 4
-=a -= 4a = a – 4
*=a *= 4a = a * 4
/=a /= 4a = a / 4
%=a %= 4a = a % 4
**= a **= 4a = a ** 4
//=a //= 4a = a // 4
&=a &= 4a = a & 4
|=a |= 4a = a | 4
^=a ^= 4a = a ^ 4
>>=a >>= 4a = a >> 4
<<=a <<= 4a = a << 4

Python Logical Operators

Logical operators are used to perform logical operations on operands like logical AND, logical OR, etc. The result of the logical operation is always a Boolean(True or False) value.

Operator SymbolOperator NameExampleMeaning
andLogical ANDa>2 and b>3Returns True only if both
statements are true
orLogical ORa>2 or b>3Returns True if any of the statements
are true
notLogical NOTnot aResult is the reverse of the operand

Example 5: Logical operators in Python

# A program to demonstrate logical operators in Python

a = True
b = False

# Logical AND operator
print('a and b is: ', a and b)

# Logical OR operator
print('a or b is: ', a or b)

# Logical NOT operator
print('not a is: ', not a)

Output:

a and b is:  False
a or b is:  True
not a is:  False

Python Identity operators

Identity operators are used to check if two variables are equal or not. Here equal means not equal by value but whether both the variables have exactly the same object or not and also the same location in memory. If both the variables have the same address in the memory and also have the same object it simply means that they are equal otherwise not.

OperatorMeaningExample
is Returns True if both variables
are exactly the same object(have exactly the same address
in the memory)
a is b
is notReturns True if both the variables
are not exactly the same object(do not have
the same address in the memory)
a is not b

Example 6: Identity operators in Python

# A program to demonstrate identity operators in Python
a = [1, 2]
b = [1, 2]

print('a is b: ', a is b)
print('a is not b: ', a is not b)

print('id of a: ', id(a))
print('id of b: ', id(b))

Output:

a is b:  False
a is not b:  True
id of a:  139632750588464
id of b:  139632750591024

In the above example, both a and b have exactly the same value but they are not equal. This is because, both the variables have a different identity(or a different location in memory).


Membership Operators

Python membership operators are used to check the existence of a given variable in a given sequence(string, list, dictionary, etc). If the given variable exists in the sequence, the result is True, otherwise False. There are two membership operators which Python provides.

OperatorMeaningExample
inEvaluates to True if the given variable exists
in the sequence.
a in b
not inEvaluates to True only if the given variable does not
exists in the sequence.
a not in b

Example 7: Membership operators in Python

# A program to demonstrate membership operator in Python

a = 2
b = [1, 2, 3, 4, 5]

# The in operator
print('a in b is:  ', a in b)

# The not in operator
print('a not in b is:  ',a not in b)

Output:

a in b is:   True
a not in b is:   False