Python Operators

In Python programming, operators are used to perform operations on operands (i.e. variables and values).

Python categorizes the operators in the following groups:

  1. Arithmetic operators
  2. Assignment operators
  3. Relational operators
  4. Logical operators
  5. Identity operators
  6. Membership operators
  7. Bit-wise operators
 

1. Arithmetic Operators:

These operators are used with numeric values to perform common mathematical operations.

Table 1: Arithmetic operators in Python
OperatorMeaningExample
+Add two operandsx + y
Subtract right operand from the leftx – y
*Multiply two operandsx * y
/Divide left operand by the right one (always results into float)x / y
%Modulus – remainder of the division of left operand by the rightx % y
//Floor division – division that results into whole number adjusted to the left in the number linex // y
**Exponent – left operand raised to the power of rightx**y
 

Example 1

# Arithmetic operators in Python
x = 27
y = 4
print(x+y) # addition
print(x-y) # subtraction
print(x*y) # multiplication
print(x/y) # division (float)
print(x%y) # modulus
print(x//y) # division (int)
print(x**y) # exponent
Output:
31
23
108
6.75
3
6
531441

 

 

2. Assignment Operators:

These operators are used to assign values to variables.

Table 2: Assignment operators in Python
OperatorExampleEquivalent to
=x = 5x = 5
+=x += 5x = x + 5
-=x -= 5x = x – 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 5x = x % 5
//=x //= 5x = x // 5
**=x **= 5x = x ** 5
&=x &= 5x = x & 5
|=x |= 5x = x | 5
^=x ^= 5x = x ^ 5
>>=x >>= 5x = x >> 5
<<=x <<= 5x = x << 5

 

Example 2

# Assignment operators in Python
x = 5
x = x + 5 # x += 5
print(x)
x = x - 5 # x -= 5
print(x)
x = x * 5 # x *= 5
print(x)
x = x / 5 # x /= 5
print(x)
x = x % 5 # x %= 5
print(x)
Output:
10
5
25
5.0
0.0

 

 

3. Relational Operators:

These operators are used to compare two values.

Table 3: Relational operators in Python
OperatorMeaningExample
>Greater that – True if left operand is greater than the rightx > y
<Less that – True if left operand is less than the rightx < y
==Equal to – True if both operands are equalx == y
!=Not equal to – True if operands are not equalx != y
>=Greater than or equal to – True if left operand is greater than or equal to the rightx >= y
<=Less than or equal to – True if left operand is less than or equal to the rightx <= y
 

Example 3

# Relational operators in Python
a = 10
b = 5
print(a>b) # True
print(a<b) # False
print(a==b) # False
print(a!=b) # True
print(a>=b) # True
print(a<=b) # False
Output:
True
False
False
True
True
False

 

 

4. Logical Operators:

These operators are used to combine conditional statements.

Table 4: Logical operators in Python
OperatorMeaningExample
andTrue if both the operands are truex and y
orTrue if either of the operands is truex or y
notTrue if operand is false (complements the operand)not x
 

Example 4

# Logical operators in Python
print("Logical AND : ", (10>5) and (9<7)) # False
print("Logical OR : ", (10>5) or (9<7))  # True
print("Logical NOT : ", not(9<7))         # True

Output:

Logical AND : False
Logical OR : True
Logical NOT : True

 

 

5. Identity Operators:

These operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location.

Table 5: Identity operators in Python
OperatorMeaningExample
isTrue if the operands are identical (refer to the same object)x is True
is notTrue if the operands are not identical (do not refer to the same object)x is not True

 

Example 5

# Identity operators in Python
print("Identity operators:-")
x = True
print(x is True) # True
print(x is not True) # False
Output:
Identity operators:-
True
False

 

 

6. Membership Operators:

These operators are used to test if a sequence is present in an object.

Table 6: Membership operators in Python
OperatorMeaningExample
inTrue if value/variable is found in the sequence5 is x
not inTrue if value/variable is not found in the sequence5 not in x

Example 6

# Membership operators in Python
print("Membership operators:-")
arr = [10, 20, 30, 40, 50]
x = 40
print(x in arr) # True
x = 60
print(x not in arr) # True
Output:
Membership operators:-
True
True

 

 

7. Bit-wise Operators:

These operators are used to compare binary numbers.

For example, in the table below, let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)

Table 7: Bit-wise operators in Python
OperatorMeaningExample
&Bit-wise ANDx& y = 0 (0000 0000)
|Bit-wise ORx | y = 14 (0000 1110)
~Bit-wise NOT~x = -11 (1111 0101)
^Bit-wise XORx ^ y = 14 (0000 1110)
>>Bit-wise right shiftx>> 2 = 2 (0000 0010)
<<Bit-wise left shiftx<< 2 = 40 (0010 1000)​

 

Example 7

# Bit-wise operators in Python
x = 10
y = 4
print(x & y) # Bit-wise AND: 0
print(x | y) # Bit-wise OR: 14
print(~x) # Bit-wise NOT: -11
print(x ^ y) # Bit-wise XOR: 14
print(x>>2) # Bit-wise right shift: 2
print(x<<2) # Bit-wise left shift: 40
Output:
0
14
-11
14
2
40