When you use a calculator, you press buttons for addition, subtraction, or multiplication. In Python, operators perform these calculations in code. Operators allow us to manipulate data, compare values, and make logical decisions.
💡 Analogy: Operators are like traffic signs—they tell Python what action to take when processing data.
Understand and use arithmetic operators in Python.
Use comparison operators to compare values.
Apply logical operators to make decisions in programs.
Arithmetic operators – Symbols used to perform mathematical calculations (+, -, *, /, %).
Comparison operators – Symbols used to compare values (==, !=, >, <, >=, <=) and return True or False.
Logical operators – Operators (and, or, not) used to combine multiple conditions in decision-making.
Order of operations – The rules that define which mathematical operations are performed first (e.g., multiplication before addition).
Modulus operator (%) – Returns the remainder of a division operation (10 % 3 returns 1). In 10 % 3, we divide 10 by 3. 3 fits three times into 10 (3 × 3 = 9), and 1 is left over. The modulus operator gives us this leftover number. So, 10 % 3 gives 1 because 10 divided by 3 leaves a remainder of 1. This is a nice and simple technique for testing if a number is even or odd.
Examples
x = 10
y = 5
print(x + y) # Addition: 15
print(x > y) # Comparison: True
print(x > 5 and y < 10) # Logical: True
print(10 % 3) # Modulus: Returns 1
Using = instead of == for comparison: if x = 5: ❌ is incorrect, use if x == 5: ✅.
Forgetting parentheses in complex operations: 10 + 5 * 2 evaluates as 10 + (5 * 2), not (10 + 5) * 2.
🔗 Practice on W3Schools: Operators Tutorial – Learn how to use arithmetic and logical operators in Python.
🔗 Try It Online: Run Your Code Here – Test your Python code instantly.
1️⃣ Basic Challenge:
Write a program that takes two numbers and prints their sum (addition), difference, and product (multiplication).
2️⃣ Extended Challenge:
Modify your program to also print whether the first number is greater than the second.
3️⃣ Advanced Challenge:
Write a program that asks the user for two numbers and checks if the first is greater than, less than, or equal to the second.
Write the example from this lesson. Make sure to include:
✅ Two variables storing numbers
✅ Arithmetic operations using these numbers
✅ A print() statement showing the results
Read the example code carefully before writing!