Quant500 Academy

2. Variables, data types and operators

Level 2: Python from Scratch for Finance

In Python, creating a variable is as simple as assigning it a value with the = sign: there is no need to declare its type beforehand, because Python is a dynamically typed language — the type is inferred automatically from the value you assign.

price = 182.52
ticker = "AAPL"
in_portfolio = True

Variable names can use letters, numbers and underscores, but they must start with a letter, and by convention they are written in lowercase. There is a handful of reserved words in the language — class, import, return, lambda, among others — that you cannot use as variable names, because Python needs them for its own syntax.

Even though you do not declare it explicitly, every variable has a type, and you can check it with the type() function:

type(price)         # <class 'float'>
type(ticker)        # <class 'str'>
type(in_portfolio)  # <class 'bool'>

The fundamental types you will use constantly in quantitative finance are four: int for whole numbers (for example, the number of shares you buy), float for decimal numbers (price, return), bool for true/false (is the position open?), and str for text (the ticker, the name of the strategy).

An important detail: if you reassign a variable with a value of another type, Python does not complain — it simply changes the variable's type on the fly.

x = 10      # x is an int
x = 10.5    # now x is a float, with no error at all

This is convenient, but it is also a source of subtle bugs: if your function expects a number of shares (int) and you mistakenly pass it a float such as 10.5, Python will not warn you, and buying 'half a share' may make no sense depending on the broker you work with.

To convert explicitly between types you use casting: int(), float(), str(), bool().

price_text = "182.52"
price_number = float(price_text)   # 182.52, now it really is a number you can compute with

shares = 10.9
whole_shares = int(shares)         # 10 - int() truncates, it does not round

Watch out for that last detail: int() does not round to the nearest integer, it truncates the decimal part. int(10.9) gives 10, not 11. If you really need to round, the correct function is round().

The arithmetic operators in Python are the ones you expect — +, -, *, / — plus two that tend to surprise people coming from other languages: // is integer division (it gives you the quotient without decimals) and ** is exponentiation (not ^, which in Python means something else).

capital = 10000
share_price = 182.52

shares_i_can_buy = capital // share_price   # integer division: how many whole shares I can afford
compound_return = 1.08 ** 5   # ** is exponentiation: 1.4693, the multiplier on capital after 5 years at 8% per year

The comparison operators (>, <, >=, <=, ==, !=) always return a boolean, and they are the basis of any entry or exit rule in a strategy: current_price > moving_average is, quite literally, the core of a trading signal.

The rest of Quant500

What you are learning, applied to the index every day:

The model portfolio, weekly in your inbox

Every Saturday, its week in one minute: how it went, the best and the worst, and which insiders bought with their own money. Plus, the day the model is rebuilt, which stocks come in and which go out. A simulation, not advice. At most one email a week, no ads, no account; one-click unsubscribe. Made by one person: Pol Gracia.

A model portfolio with simulated results, not a personalized recommendation. Your email is used for this only: privacy.

Behind the Academy and the rest of Quant500 there is one person: Pol Gracia Serrahima.