Python Programming Textbook
📗 Module 1: Introduction to Python
...[Module 1 and Module 2 content remains unchanged]...
📗 Module 3: Operators & Expressions
➕ What Are Operators?
Operators are symbols that perform operations on variables and values. Python supports different
types of operators.
🔢 Types of Python Operators
1. Arithmetic Operators
Operator Name Example Result
+ Addition 5 + 2 7
- Subtraction 5 - 2 3
* Multiplication 5 * 2 10
/ Division 5 / 2 2.5
// Floor Division 5 // 2 2
% Modulus 5 % 2 1
** Exponent 2 ** 3 8
2. Comparison (Relational) Operators
Operator Meaning Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 5 > 3 True
< Less than 3 < 5 True
1
Operator Meaning Example Result
>= Greater than or equal 5 >= 5 True
<= Less than or equal 4 <= 5 True
3. Logical Operators
Operator Description Example Result
and True if both are true 5 > 3 and 4 > 2 True
or True if one is true 5 > 3 or 2 > 4 True
not Reverses the result not(5 > 3) False
4. Assignment Operators
Operator Example Same as
= x = 5 Assign 5 to x
+= x += 2 x = x + 2
-= x -= 2 x = x - 2
*= x *= 2 x = x * 2
/= x /= 2 x = x / 2
%= x %= 2 x = x % 2
5. Bitwise Operators (Advanced, brief intro only)
Used for binary operations:
• & (AND), | (OR), ^ (XOR), ~ (NOT), << (Left Shift), >> (Right Shift)
🌎 Real-Life Examples
# Discount calculator
price = 200
is_student = True
if is_student:
discount = price * 0.10
else:
2
discount = 0
final_price = price - discount
print("You pay:", final_price)
🚲 Practice Exercises
1. Write a program to calculate area and perimeter of a rectangle
2. Take two numbers and find which one is greater
3. Try all arithmetic operators with two inputs
4. Check if a number is divisible by both 3 and 5
🎯 Mini Project: Grade Calculator
marks = int(input("Enter your marks: "))
if marks >= 90:
grade = "A"
elif marks >= 75:
grade = "B"
elif marks >= 60:
grade = "C"
elif marks >= 40:
grade = "D"
else:
grade = "F"
print("Your Grade is:", grade)
🔄 Summary Table
Category Operators
Arithmetic +, -, *, /, %, //, **
Comparison ==, !=, >, <, >=, <=
Logical and, or, not
Assignment =, +=, -=, *=, /=, %=
3
✨ Pro Tips
• Use parentheses () to control operator precedence
• == is used for comparison, = is for assignment
• Use and , or , not for combining multiple conditions
Next Module: Control Flow: if-else and loops