σμ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.6·~30 minBeginner

for loops and range

Skill positionModule 1 of 11 · Topic 6 of 33
1.6 for loops and range
—Lab score— / 100Total XP—Streak—

Repeating a block for each item

A for loop runs its indented block once for every item of a collection, and gives each item a name for that pass. Here the collection is a list, written with square brackets and commas (lists get a topic of their own later). The syntax mirrors if: a colon at the end of the first line, and an indented block under it.

prices = [184.25, 184.5, 184.0]
for price in prices:
    print(price)  # 184.25, then 184.5, then 184.0

range: counting without a list

range(n) produces the whole numbers 0 up to n - 1: the stop value is not included, which is why range(5) has five numbers. range(start, stop) begins where you say, and range(start, stop, step) skips by step, which may be negative to count down. Use it when you need to repeat something a number of times, or to walk through positions.

# range counts, and stops before the stop value
print(list(range(5)))          # [0, 1, 2, 3, 4]
print(list(range(2, 10, 3)))   # [2, 5, 8]
print(list(range(3, 0, -1)))   # [3, 2, 1]

The accumulator pattern

Most loops follow one pattern: start a variable before the loop, update it inside the loop, use it after. total += price * quantity is shorthand for total = total + price * quantity. Here each item of the list is a pair, and the loop line names both halves at once (the next topics explain this unpacking properly).

# Accumulate a total over the trades
trades = [(184.25, 200), (185.0, 100), (183.5, 50)]
total = 0.0
for price, quantity in trades:
    total += price * quantity
print(total)  # 64525.0

Looping over positions

Sometimes you need to compare each item with the one before it. Loop over the positions with range(1, len(prices)): it starts at 1, so prices[i - 1] is always valid. len() gives the number of items, and prices[i] picks the item at position i, counting from 0.

# Compare each price with the one before it
prices = [184.0, 184.5, 184.25, 185.0]
ups = 0
for i in range(1, len(prices)):
    if prices[i] > prices[i - 1]:
        ups += 1
print(ups)  # 2

Building a list as you go

To collect one result per pass, start with an empty list [] and call .append(value) inside the loop. Price ladders, running totals and signals per day are all built this way. The list is returned (or used) after the loop ends.

# Build a list of prices, one per tick level
start, tick = 184.0, 0.25
ladder = []
for level in range(4):
    ladder.append(start + level * tick)
print(ladder)  # [184.0, 184.25, 184.5, 184.75]
The loop variable stays behindAfter the loop, the loop variable still holds its last value, and a variable you set inside the loop exists afterwards too. If the collection is empty the block never runs, so a variable only created inside it would not exist: set it up before the loop.

Next up: while loops, break and continue, for repeating until a condition changes instead of for a fixed number of items.

Lab · what's graded

  • Add up the value of many trades with total_notional25 pts
  • Lay out price levels with price_ladder25 pts
  • Count the days the price rose with count_up_days25 pts
  • Keep a running total with running_total25 pts
Explorer · lab-6-1
▾ lab-6-1
…
bash · in-browser
learner@quantcraft:~/lab-6-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-6-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