📖 Phase 1: Programming Fundamentals
📌 Topic 8: Operators
Operators are special symbols or keywords used to perform operations on variables and values.
Think of operators as tools that help you calculate, compare, assign values, or combine conditions in a program.
Why Do We Need Operators?
Operators help us:
• Perform mathematical calculations
• Compare values
• Assign values to variables
• Combine multiple conditions
• Make decisions in programs
1. Arithmetic Operators
Used for mathematical calculations.
Operators:
• + Addition: 10 + 5 = 15
• - Subtraction: 10 - 5 = 5
• * Multiplication: 10 * 5 = 50
• / Division: 10 / 5 = 2.0
• // Floor Division: 10 // 3 = 3
• % Modulus (Remainder): 10 % 3 = 1
• ** Exponent: 2 ** 3 = 8
Example:
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.333...
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 1000
2. Comparison Operators
Compare two values and always return True or False.
Operators:
• == Equal to
• != Not equal to
• > Greater than
• < Less than
• >= Greater than or equal to
• <= Less than or equal to
Example:
x = 10
y = 20
print(x == y) # False
print(x != y) # True
print(x < y) # True
print(x >= y) # False
3. Assignment Operators
Used to assign or update values.
x = 10
x += 5 # 15
print(x)
x *= 2 # 30
print(x)
x -= 4 # 26
print(x)
4. Logical Operators
Combine multiple conditions.
Operators:
• and Returns True if both conditions are true
• or Returns True if at least one condition is true
• not Reverses the result
Example:
age = 25
print(age > 18 and age < 60) # True
print(age < 18 or age > 60) # False
print(not(age > 18)) # False
5. Membership Operators
Check whether a value exists in a sequence.
Operators:
• in Value exists
• not in Value does not exist
Example:
fruits = ["Apple", "Banana", "Mango"]
print("Apple" in fruits) # True
print("Orange" not in fruits) # True
6. Identity Operators
Check whether two variables refer to the same object in memory.
Operators:
• is Same object
• is not Different objects
Example:
a = [1, 2]
b = a
c = [1, 2]
print(a is b) # True
print(a is c) # False
Common Beginner Mistakes
❌ Using = instead of == while comparing values
❌ Confusing / with //
❌ Forgetting that and requires both conditions to be True
Best Practices
✅ Use meaningful variable names
✅ Choose the correct operator for the task
✅ Use parentheses to make complex conditions easier to read
Key Takeaways
• Operators perform calculations, comparisons, and logical operations
• Arithmetic operators are used for math
• Comparison operators return True or False
• Assignment operators simplify updating variables
• Logical operators combine multiple conditions
• Membership and Identity operators help work with collections and objects
➡️ Double Tap ❤️ For More