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.25Statements 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 quotesIndentation 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.25IndentationError, 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.0Arithmetic and operator precedence
+,-and*add, subtract and multiply./is true division and always gives a decimal result:7 / 2is3.5.//divides and rounds down;%gives the remainder:7 // 2is3,7 % 2is1.**raises to a power:1.05 ** 10compounds 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 pointsShowing 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