05
Operators
Python Operators: From Storing Data to Making Decisions
What You Might Be Wondering
"I can write variables now. Why do I need operators next?"
Because programs don't just store data — they calculate, compare, and decide. Operators are how you express those actions.
One-Line Definition
Operators are symbols that perform calculations, comparisons, and logical decisions on your data.
Real-Life Analogy
Variables are ingredients. Operators are the cooking actions. Without actions, ingredients just sit there.
Minimal Working Example
price = 100
discount = 0.8
final_price = price * discount
print(final_price) # 80.0
1) Arithmetic Operators
a = 7
b = 3
print(a + b) # 10
print(a - b) # 4
print(a * b) # 21
print(a / b) # 2.333...
print(a // b) # 2
print(a % b) # 1
print(a ** b) # 343
2) Comparison Operators
print(5 == 5) # True
print(5 != 3) # True
print(5 > 3) # True
print(5 <= 2) # False
3) Logical Operators
is_member = True
has_coupon = False
print(is_member and has_coupon) # False
print(is_member or has_coupon) # True
print(not has_coupon) # True
4) Membership & Assignment Operators
skills = ["Python", "SQL", "Git"]
print("Python" in skills) # True
count = 10
count += 2
print(count) # 12
Quick Quiz (5 min)
- Check whether a number is odd or even (use
%). - Check if "is a member AND balance > 100" is true.
- Check whether a certain skill exists in the
skillslist.
Quiz Rubric & Grading Criteria
- Direction: write runnable code that covers the core requirements and edge cases from the prompt.
- Criterion 1 (Correctness): main flow produces correct results, key branches execute.
- Criterion 2 (Readability): clear variable names, no excessive nesting.
- Criterion 3 (Robustness): basic protection against empty values, type errors, or unexpected input.
Take-Home Task
Build a "discount calculator":
- Input: original price, discount rate
- Output: discounted price
- Check if the order qualifies for "free shipping over $50"
Acceptance Criteria
You can independently:
- Use all 5 categories of common operators
- Write basic business condition checks
- Understand and explain expression results
Common Errors & Debugging Steps (Beginner Edition)
- Error message looks like gibberish: read the last line for the error type (
TypeError,NameError, etc.), then trace back to the offending line. - Not sure what a variable holds: drop a temporary
print(variable, type(variable))to check. - Changed code but nothing happened: make sure you saved the file, you're running the right file, and your terminal environment (venv) is correct.
Common Misconceptions
-
Misconception: mixing up
=and==. -
Reality:
=assigns.==compares. -
Misconception: cramming complex conditions into one line.
-
Reality: break them into intermediate variables first — way easier to debug.