🚦 Lesson 4: Control Flow (Decision Making)
⏱️ Estimated time: 25 minutes | Difficulty: Beginner
The Power of Iff
Control flow allows your program to execute different blocks of code based on specific conditions. It's the logic of your script!
Basic Math Operations
Python can do all kinds of math for you. Here are the basic operators:
+ Addition
result = 5 + 3
print(result) # 8
- Subtraction
result = 10 - 4
print(result) # 6
* Multiplication
result = 6 * 7
print(result) # 42
/ Division
result = 20 / 4
print(result) # 5.0
// Floor Division
result = 20 // 3
print(result) # 6
% Modulus (Remainder)
result = 20 % 3
print(result) # 2
Order of Operations
Python follows the same order as math class: PEMDAS/BODMAS
result = 2 + 3 * 4
print(result) # 14 (not 20!)
result = (2 + 3) * 4
print(result) # 20
Working with Real Numbers
price = 19.99
tax = price * 0.08
total = price + tax
print("Item:", price)
print("Tax:", tax)
print("Total:", total)
Output:
Item: 19.99
Tax: 1.5992
Total: 21.5892
Shortcut Operators
Long Way:
points = 10
points = points + 5
Shortcut:
points = 10
points += 5
Similarly:
x = 10
x -= 3 # 7
x *= 2 # 14
x /= 2 # 7.0
Real-World Example: Calculating a Tip
bill_amount = 45.50
tip_percentage = 20
tip = bill_amount * (tip_percentage / 100)
total_with_tip = bill_amount + tip
print("Bill: $", bill_amount)
print("Tip (20%):", tip)
print("Total: $", total_with_tip)
Output:
Bill: $ 45.5
Tip (20%): 9.1
Total: $ 54.6