DocsStation
DSA with Python

Python Operators

By csestudy247 Updated December 2, 2026 4 min read
4 min read

Introduction

Operators are special symbols or keywords used to perform operations on variables and values in Python.

For example:

a = 10
b = 5

print(a + b)

Output:

15

Here, + is an arithmetic operator that adds two numbers.

Python provides several categories of operators:

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

Let’s understand each type with examples.

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

OperatorDescriptionExample
+Addition10 + 5 = 15
-Subtraction10 - 5 = 5
*Multiplication10 * 5 = 50
/Division10 / 5 = 2.0
//Floor Division10 // 3 = 3
%Modulus (remainder)10 % 3 = 1
**Exponentiation2 ** 3 = 8

Example

a = 10
b = 3

print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)

Output:

13
7
30
3.3333333333333335
3
1
1000

2. Relational (Comparison) Operators

Relational operators compare two values and return a Boolean result: True or False.

OperatorDescriptionExample
==Equal to10 == 10
!=Not equal to10 != 5
>Greater than10 > 5
<Less than5 < 10
>=Greater than or equal to10 >= 10
<=Less than or equal to5 <= 10

Example

a = 10
b = 5

print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)

Output:

False
True
True
False
True
False

3. Logical Operators

Logical operators are used to combine or modify Boolean conditions.

Python provides three logical operators:

OperatorDescription
andReturns True when both conditions are True
orReturns True when at least one condition is True
notReverses the Boolean result

Example: and Operator

a = 10
b = 5

print(a > 5 and b < 10)

Output:

True

Both conditions are True, so the result is True.

Example: or Operator

a = 10
b = 5

print(a < 5 or b < 10)

Output:

True

At least one condition is True.

Example: not Operator

a = True

print(not a)

Output:

False

The not operator reverses the Boolean value.

4. Bitwise Operators

Bitwise operators perform operations on the binary representation of integers.

OperatorNameDescription
&ANDSets a bit to 1 when both bits are 1
^XORSets a bit to 1 when the bits are different
|ORSets a bit to 1 when at least one bit is 1
~NOTInverts the bits
<<Left ShiftShifts bits to the left
>>Right ShiftShifts bits to the right

Example

a = 5
b = 3

print(a & b)
print(a | b)
print(a ^ b)
print(~a)
print(a << 1)
print(a >> 1)

Output:

1
7
6
-6
10
2

Explanation

The binary representation of the numbers is:

5 = 0101
3 = 0011

For the AND operation:

  0101
& 0011
------
  0001

The binary result 0001 is equal to decimal 1.

5. Assignment Operators

Assignment operators are used to assign values to variables and update their values.

OperatorExampleEquivalent Expression
=a = 10Assigns 10 to a
+=a += 5a = a + 5
-=a -= 5a = a - 5
*=a *= 5a = a * 5
/=a /= 5a = a / 5
//=a //= 5a = a // 5
%=a %= 5a = a % 5
**=a **= 2a = a ** 2

Example

a = 10

a += 5
print(a)

a -= 3
print(a)

a *= 2
print(a)

Output:

15
12
24

6. Membership Operators

Membership operators check whether a value exists in a sequence, such as a string, list, or tuple.

Python provides two membership operators:

  • in: Returns True if the value exists in the sequence.
  • not in: Returns True if the value does not exist in the sequence.

Example: in Operator

numbers = [10, 20, 30, 40]

print(20 in numbers)
print(50 in numbers)

Output:

True
False

Example: not in Operator

fruits = ["apple", "banana", "mango"]

print("orange" not in fruits)
print("apple" not in fruits)

Output:

True
False

7. Difference Between Python Operators

Operator CategoryMain Purpose
ArithmeticPerforms mathematical calculations
RelationalCompares two values
LogicalCombines or reverses conditions
BitwisePerforms operations on binary bits
AssignmentAssigns or updates variable values
MembershipChecks whether a value exists in a sequence

8. Practice Questions (MCQs)

Q1. Which operator is used for exponentiation in Python?

A. //
B. **
C. %
D. *

Correct Answer: B. **

Q2. What is the output of 10 // 3?

A. 3.33
B. 1
C. 3
D. 10

Correct Answer: C. 3

Q3. Which operator checks whether two values are equal?

A. =
B. !=
C. ==
D. >=

Correct Answer: C. ==

Q4. Which logical operator returns True only when both conditions are True?

A. or
B. not
C. and
D. in

Correct Answer: C. and

Q5. What is the output of the following code?

print(5 & 3)

A. 7
B. 6
C. 1
D. 0

Correct Answer: C. 1

Q6. Which operator checks whether an element exists in a list?

A. is
B. in
C. ==
D. not

Correct Answer: B. in

Q7. What is the output of the following code?

a = 10
a += 5
print(a)

A. 10
B. 5
C. 15
D. 50

Correct Answer: C. 15

Scroll to Top