Paramclasses Academy - Blog

Tutorials Details

WHAT ARE OPERATORS IN PYTHON?

WHAT ARE OPERATORS IN PYTHON?

October 16, 2026

1. WHAT ARE OPERATORS IN PYTHON?

WHAT IS AN OPERATOR?

An operator is a symbol or keyword that performs an operation on values or variables.

WHY DO WE USE OPERATORS?

Operators help us:
1. Perform mathematical calculations.
2. Compare two values.
3. Make decisions in programs.
4. Assign and update values.
5. Check whether an item exists in a collection.
6. Check whether two variables refer to the same object.

Example:

a = 10
b = 5

print(a + b)

Output:
15

Here, + is an operator that adds two values.

==================================================
2. TYPES OF OPERATORS IN PYTHON
==================================================

Python has several categories of operators:

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

==================================================
3. ARITHMETIC OPERATORS
==================================================

WHAT ARE ARITHMETIC OPERATORS?

Arithmetic operators perform mathematical calculations.

OPERATORS:

+   Addition
-   Subtraction
*   Multiplication
/   Division
//  Floor Division
%   Modulus
**  Exponentiation

SYNTAX:

result = value1 operator value2

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

EXPLANATION:

+ adds two values.
- subtracts the second value from the first.
* multiplies values.
/ performs division.
// performs floor division.
% returns the remainder.
** raises a value to a power.

REAL-TIME EXAMPLE: SHOPPING BILL

price = 500
quantity = 3

total = price * quantity

print("Total Bill:", total)

OUTPUT:

Total Bill: 1500

REAL-TIME USE:

Calculate product prices, quantities, discounts, totals, and other numeric values.

IMPORTANT:

When using negative numbers, floor division rounds down to the next lower integer.

Example:

print(-7 // 2)

Output:

-4

==================================================
4. COMPARISON OPERATORS
==================================================

WHAT ARE COMPARISON OPERATORS?

Comparison operators compare two values and return True or False.

OPERATORS:

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

SYNTAX:

value1 comparison_operator value2

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

REAL-TIME EXAMPLE: STUDENT PASS OR FAIL

marks = 75

print(marks >= 40)

OUTPUT:

True

EXPLANATION:

If marks are at least 40, the comparison returns True.
Otherwise, it returns False.

REAL-TIME EXAMPLE: PRODUCT PRICE CHECK

product_price = 1500
budget = 2000

if product_price <= budget:
    print("Product is within budget")
else:
    print("Product is over budget")

OUTPUT:

Product is within budget

IMPORTANT:

=  assigns a value.
== compares two values.

Example:

age = 20
print(age == 20)

Output:

True

==================================================
5. LOGICAL OPERATORS
==================================================

WHAT ARE LOGICAL OPERATORS?

Logical operators combine conditions or reverse a condition.

OPERATORS:

and
or
not

A. AND OPERATOR

Returns True when both conditions are True.

Example:

age = 25
has_id = True

print(age >= 18 and has_id)

Output:

True

B. OR OPERATOR

Returns True when at least one condition is True.

Example:

is_weekend = False
is_holiday = True

print(is_weekend or is_holiday)

Output:

True

C. NOT OPERATOR

Reverses a Boolean condition.

Example:

is_active = True

print(not is_active)

Output:

False

REAL-TIME EXAMPLE: LOGIN VALIDATION

username = "admin"
password_correct = True

if username == "admin" and password_correct:
    print("Login successful")
else:
    print("Login failed")

OUTPUT:

Login successful

REAL-TIME EXAMPLE: DISCOUNT ELIGIBILITY

purchase_amount = 1500
is_member = False

if purchase_amount >= 1000 or is_member:
    print("Discount eligible")
else:
    print("Discount not eligible")

OUTPUT:

Discount eligible

==================================================
6. ASSIGNMENT OPERATORS
==================================================

WHAT ARE ASSIGNMENT OPERATORS?

Assignment operators assign values to variables or update their current values.

OPERATORS:

=    Assign
+=   Add and assign
-=   Subtract and assign
*=   Multiply and assign
/=   Divide and assign
//=  Floor divide and assign
%=   Modulus and assign
**=  Power and assign

EXAMPLE:

a = 10
print(a)

a += 5
print(a)

a -= 3
print(a)

a *= 2
print(a)

OUTPUT:

10
15
12
24

EXPLANATION:

a += 5 is equivalent to a = a + 5.
a -= 3 is equivalent to a = a - 3.
a *= 2 is equivalent to a = a * 2.

REAL-TIME EXAMPLE: WALLET BALANCE

balance = 1000

balance += 500
print("After deposit:", balance)

balance -= 200
print("After payment:", balance)

OUTPUT:

After deposit: 1500
After payment: 1300

==================================================
7. MEMBERSHIP OPERATORS
==================================================

WHAT ARE MEMBERSHIP OPERATORS?

Membership operators check whether a value exists in a collection or sequence.

OPERATORS:

in
not in

EXAMPLE:

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

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

OUTPUT:

True
False
True

EXPLANATION:

in returns True when the item is found.
not in returns True when the item is not found.

REAL-TIME EXAMPLE: PRODUCT SEARCH

products = ["Laptop", "Mobile", "Tablet"]

if "Laptop" in products:
    print("Product available")
else:
    print("Product unavailable")

OUTPUT:

Product available

REAL-TIME USE:

Check available products, allowed options, usernames in a collection, or keywords in text.

==================================================
8. IDENTITY OPERATORS
==================================================

WHAT ARE IDENTITY OPERATORS?

Identity operators check whether two references point to the same object in memory.

OPERATORS:

is
is not

EXAMPLE:

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

print(a is b)
print(a is c)
print(a == c)

OUTPUT:

True
False
True

EXPLANATION:

a is b is True because b refers to the same list as a.
a is c is False because c is a different list object.
a == c is True because both lists have equal contents.

IMPORTANT DIFFERENCE:

== compares values for equality.
is checks object identity.

REAL-TIME EXAMPLE: CHECKING NONE

result = None

if result is None:
    print("No result available")

OUTPUT:

No result available

Use is and is not when checking for None.

==================================================
9. BITWISE OPERATORS
==================================================

WHAT ARE BITWISE OPERATORS?

Bitwise operators perform operations on the individual bits of integers.

OPERATORS:

&   Bitwise AND
|   Bitwise OR
^   Bitwise XOR
~   Bitwise NOT
<<  Left shift
>>  Right shift

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:

5 in binary is 0101.
3 in binary is 0011.

Bitwise AND (&) keeps bits that are 1 in both values.
Bitwise OR (|) keeps bits that are 1 in either value.
Bitwise XOR (^) keeps bits that differ.
Bitwise NOT (~) inverts the integer's bits.
Left shift (<<) shifts bits to the left.
Right shift (>>) shifts bits to the right.

REAL-TIME USE:

Bitwise operators are useful in low-level programming, binary data processing, and flag-based settings.

Beginners can focus on arithmetic, comparison, logical, and assignment operators first.

==================================================
10. OPERATOR PRECEDENCE
==================================================

WHAT IS OPERATOR PRECEDENCE?

Operator precedence determines which operation is evaluated first in an expression.

GENERAL ORDER:

1. Parentheses: ()
2. Exponentiation: **
3. Unary operators: +x, -x, ~x
4. Multiplication, division, floor division, modulus
5. Addition and subtraction
6. Bitwise shifts
7. Bitwise AND, XOR, and OR
8. Comparisons and membership/identity tests
9. Logical NOT
10. Logical AND
11. Logical OR

Note: Some operators share precedence levels, and exponentiation has special associativity rules. Use parentheses when clarity matters.

EXAMPLE 1:

result = 2 + 3 * 4

print(result)

OUTPUT:

14

EXPLANATION:

Multiplication occurs before addition.

3 * 4 = 12
2 + 12 = 14

EXAMPLE 2:

result = (2 + 3) * 4

print(result)

OUTPUT:

20

EXPLANATION:

Parentheses are evaluated first.

2 + 3 = 5
5 * 4 = 20

==================================================
11. REAL-TIME PROJECT: BILL CALCULATOR
==================================================

SCENARIO:

A shop needs to calculate the total price, discount, and final bill.

PROGRAM:

price = 1000
quantity = 3
discount_percent = 10

subtotal = price * quantity
discount = subtotal * discount_percent / 100
final_bill = subtotal - discount

print("Product Price:", price)
print("Quantity:", quantity)
print("Subtotal:", subtotal)
print("Discount:", discount)
print("Final Bill:", final_bill)

OUTPUT:

Product Price: 1000
Quantity: 3
Subtotal: 3000
Discount: 300.0
Final Bill: 2700.0

OPERATORS USED:

* Multiplication
/ Division
- Subtraction

==================================================
12. REAL-TIME PROJECT: ELIGIBILITY CHECKER
==================================================

SCENARIO:

A training institute checks whether a student qualifies for admission.

RULES:

The student must score at least 60 marks.
The student must have submitted the required documents.

PROGRAM:

marks = 75
documents_submitted = True

if marks >= 60 and documents_submitted:
    print("Admission eligible")
else:
    print("Admission not eligible")

OUTPUT:

Admission eligible

OPERATORS USED:

>= Comparison operator
and Logical operator

==================================================
13. REAL-TIME PROJECT: SHOPPING DISCOUNT
==================================================

SCENARIO:

A shop provides a discount when the purchase amount is at least 2000 or the customer is a member.

PROGRAM:

amount = 2500
is_member = False

if amount >= 2000 or is_member:
    print("Discount available")
else:
    print("No discount")

OUTPUT:

Discount available

OPERATOR USED:

or

==================================================
14. COMMON BEGINNER MISTAKES
==================================================

MISTAKE 1: CONFUSING = WITH ==

Incorrect comparison:

if age = 18:
    print("Eligible")

Correct:

if age == 18:
    print("Eligible")

MISTAKE 2: USING AND WHEN OR IS NEEDED

and requires both conditions to be True.
or requires at least one condition to be True.

MISTAKE 3: CONFUSING == WITH is

== compares values.
is compares object identity.

MISTAKE 4: DIVISION BY ZERO

Example:

result = 10 / 0

This raises ZeroDivisionError.

Safer example:

divisor = 0

if divisor != 0:
    print(10 / divisor)
else:
    print("Cannot divide by zero")

OUTPUT:

Cannot divide by zero

MISTAKE 5: IGNORING OPERATOR PRECEDENCE

Example:

print(10 + 2 * 3)

Output:

16

Use parentheses when you want a different evaluation order.

==================================================
15. PRACTICE PROGRAMS
==================================================

PROGRAM 1: ARITHMETIC CALCULATOR

a = 20
b = 6

Print the sum, difference, product, division, floor division, and remainder.

PROGRAM 2: COMPARISON CHECKER

age = 21

Check whether age is equal to 18, greater than 18, and less than 18.

PROGRAM 3: PASS OR FAIL

marks = 35

If marks are at least 40, print "Pass".
Otherwise, print "Fail".

Expected output:

Fail

PROGRAM 4: LOGIN CHECK

username = "admin"
password_correct = True

Print "Login successful" only when both conditions are True.

PROGRAM 5: WALLET BALANCE

balance = 500

Add 300 to the balance.
Subtract 100 from the balance.
Print the final balance.

Expected output:

700

PROGRAM 6: PRODUCT AVAILABILITY

products = ["Laptop", "Mobile", "Tablet"]

Check whether "Mobile" is in the list.

Expected output:

True

PROGRAM 7: DISCOUNT CALCULATOR

price = 2000
discount_percent = 15

Calculate the discount amount and final price.

Expected output:

Discount: 300.0
Final Price: 1700.0

PROGRAM 8: ELIGIBILITY CHECKER

age = 22
has_license = True

Print "Can drive" if the age is at least 18 and the person has a license.

Expected output:

Can drive

==================================================
16. MODULE 3 QUIZ
==================================================

Q1. Which operator performs multiplication?

A. +
B. *
C. %

Answer: B. *

Q2. Which operator checks equality?

A. =
B. ==
C. !=

Answer: B. ==

Q3. Which logical operator requires both conditions to be True?

A. or
B. not
C. and

Answer: C. and

Q4. What is the result of 10 % 3?

A. 3
B. 1
C. 0

Answer: B. 1

Q5. Which operator checks membership?

A. in
B. is
C. ==

Answer: A. in

Q6. What is the output of 2 + 3 * 4?

A. 20
B. 14
C. 24

Answer: B. 14

Q7. Which operator checks object identity?

A. ==
B. is
C. in

Answer: B. is

Q8. What is the output?

balance = 100
balance += 50
print(balance)

A. 100
B. 50
C. 150

Answer: C. 150

Q9. What does not True return?

A. True
B. False
C. None

Answer: B. False

Q10. Which operator performs floor division?

A. /
B. //
C. %

Answer: B. //

==================================================
17. MODULE 3 ASSIGNMENT
==================================================

Complete these tasks independently:

1. Create a calculator using arithmetic operators.
2. Compare two numbers using comparison operators.
3. Check whether a student has passed.
4. Create a login eligibility checker.
5. Update a wallet balance using assignment operators.
6. Check whether a product exists in a list.
7. Calculate a shopping bill after discount.
8. Build an admission eligibility checker.
9. Practise bitwise operators using integers.
10. Explain the difference between == and is.
11. Demonstrate operator precedence using parentheses.
12. Handle division by zero safely.

==================================================
18. MODULE 3 SUMMARY
==================================================

In this module, you learned:

1. What operators are.
2. Arithmetic operators.
3. Comparison operators.
4. Logical operators.
5. Assignment operators.
6. Membership operators.
7. Identity operators.
8. Bitwise operators.
9. Operator precedence.
10. Real-time billing and eligibility programs.
11. Common mistakes and safe calculations.
12. Practice programs and quizzes.