σμ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.1·~45 minBeginner

Python syntax and your first program

Skill positionModule 1 of 11 · Topic 1 of 33
1.1 Python syntax and your first program
—Lab score— / 100Total XP—Streak—

How a Python program runs

A Python program is a text file made of statements. Python reads it from the top to the bottom and runs one statement at a time, with no boilerplate: the file main.py is the program. Press Run, or type python main.py in the terminal.

bid = 184.25
ask = 184.50
print(ask - bid)  # 0.25

Statements and expressions

An expression produces a value, like ask - bid or 2 * 3. A statement does something, like bid = 184.25 or print(bid). Each statement sits on its own line, and the end of the line ends the statement: no semicolons. When a statement is too long, wrap it in parentheses and continue on the next lines.

total_cost = (
    price * quantity
    + commission
    + exchange_fee
)

Comments and docstrings

Everything from # to the end of the line is a comment: Python ignores it. Use comments to explain why the code does something, not to repeat what it does. A string in triple quotes placed first in a file or function is a docstring, the official description that tools and help() show.

# Mid price: the fair value halfway between bid and ask.
mid = (bid + ask) / 2  # average of the two quotes

Indentation is part of the syntax

Where other languages use braces, Python uses indentation. A line ending in a colon opens a block, and the block is the indented lines underneath it. Indent with four spaces, and never mix tabs and spaces. Code at the left margin is top-level code.

def spread(bid, ask):
    # both lines below are the block: indented by four spaces
    width = ask - bid
    return width

print(spread(184.25, 184.50))  # back at the left margin: 0.25
Watch outA missing or extra space raises an IndentationError, and a missing colon raises a SyntaxError. Python stops before running anything, so read the line number in the message first. Set your editor to insert spaces when you press Tab.

Variables and assignment

name = value binds a name to a value. The = sign means assign, not equals: Python first computes the right-hand side, then attaches the name to the result. Names are case-sensitive, may contain letters, digits and underscores, cannot start with a digit, and cannot be a keyword such as if or for. Python style is snake_case, like last_price.

price = 184.25
quantity = 200
notional = price * quantity
price = 185.00      # rebinds price; notional is unchanged
quantity += 50      # shorthand for quantity = quantity + 50
print(notional)     # 36850.0

Arithmetic and operator precedence

  • +, - and * add, subtract and multiply.
  • / is true division and always gives a decimal result: 7 / 2 is 3.5.
  • // divides and rounds down; % gives the remainder: 7 // 2 is 3, 7 % 2 is 1.
  • ** raises to a power: 1.05 ** 10 compounds 5% ten times.

Python follows the maths order: ** first, then * and /, then + and -. So 2 + 3 * 4 is 14, while (2 + 3) * 4 is 20. When in doubt, add parentheses: they cost nothing and make the intent obvious. Underscores inside numbers, like 10_000, are ignored and make big numbers readable.

spread = ask - bid                   # 0.25
mid = (bid + ask) / 2                # 184.375
spread_bps = spread / mid * 10_000   # the spread in basis points

Showing results with print

print() writes values to the terminal, separated by spaces. Text in quotes is a string; the next topics cover strings and numbers in depth. Right now print is how you look inside your program: print a value, check it by hand, and then move on.

print("bid", bid, "ask", ask)
print("mid", mid)

In the lab you will write small price formulas using only what is on this page: assignment, arithmetic and parentheses. Next up: how Python stores numbers, and why 0.1 + 0.2 is not exactly 0.3.

Lab · what's graded

  • Compute the value of a position with notional25 pts
  • Find the mid price with midpoint25 pts
  • Measure the bid-ask gap with spread25 pts
  • Express the spread in basis points with spread_bps25 pts
Explorer · lab-1-1
▾ lab-1-1
…
bash · in-browser
learner@quantcraft:~/lab-1-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

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