🐍 Lesson 4: Python Operators & Expressions
Welcome to Lesson 4! Today you will learn about operators — the symbols Python uses to perform math, compare values, and control logic. Operators are everywhere in Python, and mastering them will make your coding faster and easier.
⭐ What You Will Learn in This Lesson
- How to perform arithmetic calculations
- How to compare values with comparison operators
- How to use logical operators to combine conditions
- Understanding assignment operators
- Where and why operators are used in Python
👥 Who Is This Lesson For?
- Beginners who want to perform calculations in Python
- Students learning Python logic and decision making
- Anyone building interactive or automated Python programs
➕ 1. Arithmetic Operators
These operators help you perform basic math:
- + : Addition
- - : Subtraction
- * : Multiplication
- / : Division
- // : Floor division (removes decimals)
- % : Remainder (modulo)
- ** : Exponent (power)
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.3333333333333335
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 1000
🔍 2. Comparison Operators
Used to compare two values. They return True or False.
- == : Equal to
- != : Not equal
- > : Greater than
- < : Less than
- >= : Greater or equal
- <= : Less or equal
age = 18
print(age == 18) # True
print(age > 20) # False
print(age != 18) # False
⚙️ 3. Logical Operators
Used to combine conditions and control logic:
- and : True if both conditions are True
- or : True if at least one condition is True
- not : Reverses the condition
x = 5
print(x > 2 and x < 10) # True
print(x > 10 or x == 5) # True
print(not(x == 5)) # False
📝 4. Assignment Operators
Used to assign values to variables and update them:
- = : Assign
- += : Add and assign
- -= : Subtract and assign
- *= : Multiply and assign
- /= : Divide and assign
x = 10
x += 5 # x = x + 5 → 15
x -= 3 # x = x - 3 → 12
🧠 Why Operators Matter
Operators are everywhere in Python. You will use them in:
- Math calculations
- Decision making
- Loops
- Functions
- Data processing
Mastering operators is essential to writing logic efficiently.
❌ Common Mistakes
- Using / instead of // when expecting whole numbers
- Mixing logical operators incorrectly (and/or/not)
- Forgetting to update variables when using assignment operators
- Confusing comparison == with assignment =
❓ Frequently Asked Questions (FAQ)
1. What is the difference between = and ==?
= assigns a value. == checks equality.
2. Can I combine multiple operators in one line?
Yes. Python follows operator precedence (order of operations) just like math.
3. What happens if I divide by zero?
Python will show an error ZeroDivisionError. Always check the divisor.
4. Can logical operators be used with numbers?
Yes, but it’s recommended to use them with boolean expressions for clarity.
🧪 Practice
- Create three variables: a, b, and c.
- Print:
- a + b
- a * c
- a > b
- a == c
- Create a program that checks:
- If someone is 18 or older
- If their age is between 13 and 19 (teenager)
🚀 What’s Next?
In the next lesson, you’ll learn about:
- Python control flow
- if statements
- Loops
- Making decisions in programs
Comments
Post a Comment