σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademySkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
Skills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
Python skill›Module 1 · Python foundations for quants›Topic 1.5·~30 minBeginner

Conditionals: if, elif, else

Skill positionModule 1 of 11 · Topic 5 of 33
1.5 Conditionals: if, elif, else
—Lab score— / 100Total XP—Streak—

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 margin

Order 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.0007

Return 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 hold

A 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)  # ok
Do not compare a boolean to TrueIf is_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 elif ladder, 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
Explorer · lab-5-1
▾ lab-5-1
…
bash · in-browser
learner@quantcraft:~/lab-5-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-5-1 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes

Log in to run your code.

Every lecture is free to read. Create an account or log in to run code, run tests and submit this lab.

  • In-browser IDE with a bash terminal — nothing to install
  • Instant grading, XP and your place on the skill
Create an accountLog in
OR
Continue with GitHub