➕ Python Course in 4 to 6 Weeks – Lesson 5: Operators in Python
Welcome to Lesson 5 of our Python Course in 4 to 6 Weeks! In this lesson, we’ll explore Python operators – the tools that allow you to perform calculations, make comparisons, and add logic to your code.
✅ What You'll Learn
- Arithmetic Operators ( +, -, *, /, etc. )
- Comparison Operators ( ==, !=, >, <, etc. )
- Logical Operators ( and, or, not )
- Assignment Operators ( =, +=, -=, etc. )
- Examples and common mistakes
➗ Arithmetic Operators
a = 10
b = 3
print(a + b) # Addition
print(a - b) # Subtraction
print(a * b) # Multiplication
print(a / b) # Division
print(a % b) # Modulus (remainder)
print(a ** b) # Exponentiation
print(a // b) # Floor division
🔁 Assignment Operators
Used to assign values to variables and modify them:
x = 5
x += 2 # x = x + 2 → 7
x *= 3 # x = x * 3 → 21
x -= 1 # x = x - 1 → 20
🔍 Comparison Operators
Used to compare values. They return True
or False
:
a = 5
b = 8
print(a == b) # False
print(a != b) # True
print(a > b) # False
print(a < b) # True
🧠 Logical Operators
Used to combine multiple conditions:
age = 20
has_ID = True
print(age > 18 and has_ID) # True
print(age > 18 or has_ID) # True
print(not has_ID) # False
⚠️ Common Mistakes
- ❌ Confusing
=
(assignment) with==
(comparison) - ❌ Using quotes with numbers:
"5" + 3
→ TypeError - ❌ Division by zero:
10 / 0
→ ZeroDivisionError
🧪 Practice Challenge
Create a Python file named math_game.py
that:
- Asks the user for two numbers
- Prints the result of all arithmetic operations
- Compares the numbers and shows which is greater
📥 Tools You'll Need
- 🐍 Python: https://www.python.org/downloads/
- 📝 VS Code: https://code.visualstudio.com/
- 🌐 Replit: https://replit.com/languages/python3
📌 Final Words
Operators are essential for building logic, performing calculations, and controlling the flow of your programs. With a strong grasp of operators, you're now ready to dive into one of the most powerful tools in programming — conditional statements.
🔗 Coming Next:
Lesson 6: Conditional Statements (if, elif, else) – Making Decisions with Code 🧭