Choosing what runs
So far every line ran, top to bottom. An if statement runs a block of code only when a condition is True. Add elif ("else if") for further conditions and else for everything left over. Python checks them in order and runs only the first block whose condition is true. Each condition line ends with a colon.
position, target = 100, 250
if target > position:
print("buy", target - position) # buy 150
elif target < position:
print("sell", position - target)
else:
print("hold")Indentation is the structure
The lines indented under the if are the block that belongs to it; the first line back at the left margin is outside. Use four spaces per level, always the same way. Python has no braces: the indentation is the structure, so a wrong indent changes what the program does.
# The indented lines are the block that belongs to the if
price = 184.5
if price > 184:
print("above 184") # above 184
print("still inside the if")
print("always runs") # back at the left marginOrder matters: the first match wins
Because only the first true branch runs, an elif ladder can use thresholds: each branch only has to say what is new. A fee schedule is a classic case. Put the narrowest or most specific condition first, otherwise a broad one above it swallows the case.
# elif branches are checked top to bottom; the first True wins
monthly_volume = 3_500_000
if monthly_volume < 1_000_000:
rate = 0.0010
elif monthly_volume < 10_000_000:
rate = 0.0007
else:
rate = 0.0004
print(rate) # 0.0007Return early
Inside a function, return ends the function on the spot. That lets you handle each case with its own if and return, with no else and no deep nesting; the last line is the default. Code written this way stays flat and reads like a list of rules.
# Early returns keep a function flat
def side(position, target):
if target > position:
return "buy"
if target < position:
return "sell"
return "hold"
print(side(100, 250), side(250, 100), side(100, 100)) # buy sell holdA one-line choice
When an if only picks one of two values, a conditional expression is shorter: value_if_true if condition else value_if_false. It is an expression, so it can sit inside an f-string or a return. Use it for simple choices, and a normal if as soon as there is more to do.
# A one-line choice between two values
cash = 40.0
status = "ok" if cash >= 0 else "overdrawn"
print(status) # okis_open is already a boolean, write if is_open: and if not is_open:. if is_open == True: says the same thing in a clumsier way. The same goes for if x: on numbers and text: it tests for "not zero" and "not empty".- Two outcomes:
if/else, or a one-line conditional expression. - Several ordered ranges: an
elifladder, narrowest condition first. - Special cases and bad input: early
returns, then the normal case last.
Next up: for loops and range, to repeat a block of code once for each item.
Lab · what's graded
- Decide to buy, sell or hold with
order_side25 pts - Pick the fee rate for a volume with
fee_tier25 pts - Keep a value between two limits with
clamp25 pts - Turn two averages into a signal with
crossover_signal25 pts