Skip to content
Home ยป Operators in Python

Operators in Python

Here is a complete, clear, and exam-oriented explanation of Operators in Python, suitable for BCA/MCA/B.Tech students.


Operators in Python

Operators are special symbols used to perform operations on variables and values.
Python supports several types of operators for arithmetic, comparison, logic, assignment, and more.

An operand is a value on which the operator acts:

a = 10   # a is operand
b = 5    # b is operand

Types of Operators in Python

Python provides the following major categories:

  1. Arithmetic Operators
  2. Relational (Comparison) Operators
  3. Logical Operators
  4. Assignment Operators
  5. Bitwise Operators
  6. Membership Operators
  7. Identity Operators

Let’s discuss each in detail with examples.


1. Arithmetic Operators

Used for basic mathematical operations.

OperatorMeaningExampleResult
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 52.0
%Modulus (remainder)10 % 31
**Exponent2 ** 38
//Floor division10 // 33

Example:

a = 10
b = 3
print(a + b)
print(a / b)
print(a // b)

2. Relational (Comparison) Operators

Used to compare two values. They return True or False.

OperatorMeaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Example:

print(10 > 5)   # True
print(10 == 5)  # False

3. Logical Operators

Used to combine conditional statements.

OperatorMeaning
andTrue if both are True
orTrue if at least one is True
notReverses boolean

Example:

x = 10
print(x > 5 and x < 20)  # True
print(x > 15 or x == 10) # True
print(not(x == 10))      # False

4. Assignment Operators

Used to assign values to variables.
Also used for shorthand operations.

OperatorMeaningExample
=Assigna = 5
+=Add and assigna += 3 โ†’ a = a + 3
-=Subtract and assigna -= 3
*=Multiply and assigna *= 3
/=Divide and assigna /= 2
//=Floor divide and assigna //= 2
%=Modulus and assigna %= 5
**=Power and assigna **= 2

Example:

a = 10
a += 5   # a = 15

5. Bitwise Operators

Work at the binary (bit) level.

OperatorMeaning
&Bitwise AND
``
^Bitwise XOR
~Bitwise NOT
<<Left shift
>>Right shift

Example:

a = 5      # 0101
b = 3      # 0011
print(a & b)  # 1  โ†’ 0001
print(a | b)  # 7  โ†’ 0111

6. Membership Operators

Used to test whether a value is in a sequence (string, list, tuple, etc.).

OperatorMeaning
inTrue if value is found
not inTrue if value is not found

Example:

nums = [1, 2, 3]
print(2 in nums)        # True
print(5 not in nums)    # True

7. Identity Operators

Used to compare memory locations of two objects.

OperatorMeaning
isTrue if both variables refer to the same object
is notTrue if they do not refer to the same object

Example:

a = [1, 2, 3]
b = a
c = [1, 2, 3]

print(a is b)     # True (b refers to same object)
print(a is c)     # False (different objects with same value)

8. Operator Precedence (Order of Evaluation)

Python evaluates operators in a specific order:

  1. Parentheses ()
  2. Exponent **
  3. Unary operators +x, -x, ~x
  4. Multiplication, Division, Modulus, Floor division *, /, %, //
  5. Addition, Subtraction +, -
  6. Bitwise shifts <<, >>
  7. Bitwise AND, OR, XOR
  8. Comparison operators
  9. Logical NOT
  10. Logical AND
  11. Logical OR
  12. Assignment operators

9. Examples Combining Operators

result = 10 + 5 * 2
# multiplication first: 5 * 2 = 10
# then addition: 10 + 10 = 20
print(result)

Summary

CategoryExamples
Arithmetic+, -, *, /, %, **, //
Comparison==, !=, >, <, >=, <=
Logicaland, or, not
Assignment=, +=, -=, *=, etc.
Bitwise&,
Membershipin, not in
Identityis, is not