Python Lesson 6 – Control Flow (if / elif / else)
In this lesson, we learned how to use conditional statements to control the flow of a program. These are
essential for making decisions in financial models (e.g., profit vs loss logic).
Basic Syntax:
price = 160
buy_price = 150
if price > buy_price:
print("Profitable trade.")
elif price == buy_price:
print("Break even.")
else:
print("Losing trade.")
- `if`: Runs only if the condition is True. - `elif`: Checked only if previous conditions are False. You can
have multiple elifs. - `else`: Runs if no other conditions were True. - Each condition ends with a colon `:`
and is followed by an indented block of code.
Finance Example – ROI Classification
buy_price = 100
current_price = 120
profit = current_price - buy_price
return_pct = profit / buy_price # decimal return, e.g. 0.20 = 20%
if return_pct > 0.10:
print("Big winner!")
elif return_pct > 0:
print("Small profit.")
else:
print("Losing trade.")
■ Here `return_pct` is a decimal (e.g. 0.20 = 20%). If you stored return_pct as a whole number (20
instead of 0.20), you would compare against 10 instead of 0.10.
Mini Project – Trade Classification
1. Ask the user for their buy price and current price (use input()).
2. Calculate ROI as a decimal return.
3. Print one of the following messages:
- 'Big winner!' if ROI > 10%
- 'Small profit.' if ROI > 0%
- 'Losing trade.' otherwise
■ In finance, control flow allows you to classify outcomes, filter strategies, and handle edge cases like
break-even trades.