Skip to content

Python Programming - Operators

Python operators are special symbols used to perform specific operations on one or more operands.

The variables, values, or expressions can be used as operands. For example, Python’s addition operator (+) is used to perform addition operations on two variables, values, or expressions.

Python operators are categorized in the following categories −

Operator Types

  • Arithmetic Operators
  • Assignment Operators
  • Comparison (Relational) Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators

Python Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, etc. The following table contains all arithmetic operators with their symbols, names, and examples (assume that the values of a and b are 10 and 20, respectively).

OperatorNameExample
+Additiona + b = 30
-Subtractiona - b = -10
*Multiplicationa * b = 200
/Divisionb / a = 2
%Modulusb % a = 0
**Exponenta ** b = 10**20
//Floor Division9 // 2 = 4
a = 7
b = 4
c = a + b
print(f"The sum of {a} and {b} is: {c}")

Python Assignment operators are used to assign values to variables. Following is a table which shows all Python assignment operators.

OperatorExampleSame As
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
//=x //= 3x = x // 3
**=x **= 3x = x ** 3
&=x &= 3x = x & 3
^=x ^= 3x = x ^ 3
>>=x >>= 3x = x >> 3
<<=x <<= 3x = x << 3
:=print(x := 3)x = 3 then print(x)
a = 4-2 # Assign 4-2 in a
print(a)
b = 6
# b += 3 # Increment the value of b by 3 and then assign it to b
b *= 3 # Multiplies the value of b by 3 and then assign it to b
print(b)

Comparison or Relational operators are used to compare two values.

OperatorNameExample
==Equalx == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y
d = 5 == 5
print(d)
# Output: True

Python logical operators are used to combine two or more conditions and check the final result. There are following logical operators supported by Python language.

OperatorDescriptionExample
andReturns True if both statements are truex < 5 and x < 10
orReturns True if one of the statements is truex < 5 or x < 4
notReverse the resultnot(x < 5 and x < 10)
# Truth table of 'or'
print("True or False is ", True or False)
print("True or True is ", True or True)
print("False or True is ", False or True)
print("False or False is ", False or False)
# Truth table of 'and'
print("True and False is ", True and False)
print("True and True is ", True and True)
print("False and True is ", False and True)
print("False and False is ", False and False)
print(not(True))
print(not(False))

Python Bitwise operator works on bits and performs bit by bit operation. These operators are used to compare binary numbers.

OperatorNameDescriptionExample
&ANDSets each bit to 1 if both bits are 1x & y
|ORSets each bit to 1 if one of two bits is 1x | y
^XORSets each bit to 1 if only one of two bits is 1x ^ y
~NOTInverts all the bits~x
<<Zero fill left shiftShift left by pushing zeros in from the right and letting leftmost bits fall offx << 2
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, letting rightmost bits fall offx >> 2
a = 10 # 1010 in binary
b = 4 # 0100 in binary
print(f"a & b = {a & b}") # 1010 & 0100 = 0000 (0)
print(f"a | b = {a | b}") # 1010 | 0100 = 1110 (14)
print(f"a ^ b = {a ^ b}") # 1010 ^ 0100 = 1110 (14)
print(f"~a = {~a}") # Inverts the bits of 1010, gives -(a+1), i.e., -11
print(f"a << 1 = {a << 1}") # Left shifts bits of 1010 to 10100 (20)
print(f"a >> 1 = {a >> 1}") # Right shifts bits of 1010 to 0101 (5)

Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples.

OperatorDescriptionExample
inReturns True if a sequence with the specified value is present in the objectx in y
not inReturns True if a sequence with the specified value is not present in the objectx not in y
my_list = [1, 2, 3, 4, 5]
my_string = "Hello, Akash"
print(6 in my_list)
print("Akash" in my_string)
print(10 not in my_list)
print("Python" not in my_list)

Identity 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.

OperatorDescriptionExample
isReturns True if both variables are the same objectx is y
is notReturns True if both variables are not the same objectx is not y
x = 5
y = 5
z = [1, 2, 3]
w = [1, 2, 3]
print(x is y) # True, since x and y point to the same object in memory
print(z is w) # False, since z and w are different objects, even though their contents are the same
print(x is not y) # False, because x and y are the same object
print(z is not w) # True, since z and w are different objects

Practice Questions on Python Operators

Task: Write a Python program that takes two integers a and b, and a list lst of integers. Using only operators and perform the following:

  1. Perform all arithmetic operations between a and b and store the results.
  2. Update the value of a using at least three different assignment operators.
  3. Compare a and b using all six comparison operators and store the results as booleans.
  4. Use logical operators (and, or, not) with the comparison results.
  5. Perform bitwise operations on a and b.
  6. Check membership of a and b in the list lst.

Output: Print all the results in a clean, labeled format (e.g., Sum: 30, a in lst: True, etc.).


a = 10
b = 20
lst = [5, 10, 15, 20, 25]