σμ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 AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμ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 AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
Python skill›Module 1 · Python foundations for quants›Topic 1.13·~30 minBeginner

Tuples and unpacking

Skill positionModule 1 of 11 · Topic 13 of 33
1.13 Tuples and unpacking
—Lab score— / 100Total XP—Streak—
Lecture0% read

3 min readCode examples: 4

  1. 01A fixed group of values
  2. 02Unpacking: give the parts names
  3. 03Taking the rest with *
  4. 04Unpacking in loops, returns and sorting
01

A fixed group of values

A tuple is like a list that cannot change after it is created. You make one with commas, usually wrapped in parentheses: ("AAPL", 184.25, 184.5). It suits a small record whose parts have a fixed meaning, such as one quote or one trade. You read it by index like a list, but assigning to a position raises a TypeError. A one-item tuple needs a trailing comma, (5,), because (5) is just the number 5.

Python
1
2
3
4
5
6
7
8
9
10
11
12
13quote = ("AAPL", 184.25, 184.5)

# Reading works like a list
print(quote[0], len(quote))  # AAPL 3

# Changing does not
try:
    quote[1] = 185.0
except TypeError as error:
    print("tuples are fixed:", error)

# One item needs a trailing comma
print(type((5,)), type((5)))  # <class 'tuple'> <class 'int'>
02

Unpacking: give the parts names

Unpacking assigns the parts of a tuple (or any sequence) to several names at once: symbol, bid, ask = quote. The number of names must match the number of items, otherwise Python raises a ValueError. Compared with quote[0], quote[1] and quote[2], the names say what each part means, so unpacking is usually clearer than indexing. It also gives the classic swap without a temporary variable: a, b = b, a.

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14# Unpack a quote into named parts
quote = ("AAPL", 184.25, 184.5)
symbol, bid, ask = quote
print(symbol, round(ask - bid, 2))  # AAPL 0.25

# Swap two values without a temporary
bid, ask = ask, bid
print(bid, ask)                     # 184.5 184.25

# The count must match
try:
    symbol, bid = quote
except ValueError as error:
    print("wrong count:", error)
03

Taking the rest with *

Put a * before one name to let it collect everything that is left, as a list: first, *rest = prices or first, *middle, last = prices. Only one starred name is allowed, and the plain names still need an item each. Use _ for a part you do not need, for example first, *_, last = prices.

Python
1
2
3
4
5
6
7
8# * collects what is left, as a list
prices = [184.0, 185.0, 183.2, 186.1]

first, *rest = prices
print(first, rest)         # 184.0 [185.0, 183.2, 186.1]

first, *_, last = prices
print(first, last)         # 184.0 186.1
04

Unpacking in loops, returns and sorting

Unpacking works anywhere a name is assigned: in a for loop (for symbol, price in quotes), in comprehensions, and on the result of a function that returns several values with return a, b, which really returns one tuple. Tuples also compare item by item, first against first and then, on a tie, second against second, so sorting a list of (price, symbol) tuples orders by price and breaks ties by symbol.

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14# Return several values, unpack them in the caller
def spread_and_mid(bid, ask):
    return ask - bid, (bid + ask) / 2

spread, mid = spread_and_mid(184.25, 184.5)
print(spread, mid)  # 0.25 184.375

# Tuples sort by the first item, then the second
quotes = [("AAPL", 184.5), ("MSFT", 410.2), ("GOOG", 171.0)]
by_price = sorted((price, symbol) for symbol, price in quotes)
print(by_price)     # [(171.0, 'GOOG'), (184.5, 'AAPL'), (410.2, 'MSFT')]

for price, symbol in by_price:
    print(symbol, price)
A tuple for a record, a list for a collection

Use a tuple when the position of each part has a meaning and the size is fixed, like (symbol, bid, ask). Use a list when you have many similar items and may add or remove some. Because a tuple cannot change, it can also be used where a list cannot, for example as a key in a dictionary, which is coming soon.

  • (a, b, c): a fixed group; read it, but do not change it.
  • x, y = pair: unpack into names; the counts must match.
  • first, *rest = items: the starred name collects the rest as a list.

Next up: dictionaries, which look values up by a name instead of a position.

Lab · what's graded

4 tasks · 100 pts
  • Unpack a quote with quote_stats25 pts
  • Take both ends with first_last25 pts
  • Rank quotes by price with ranked25 pts
  • Find the cheapest venue with best_ask25 pts
Explorer · lab-13-1
▾ lab-13-1
…
bash · in-browser
learner@quantcraft:~/lab-13-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

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