Learn the three syntax rules that let you read and write Python scripts in finance work. Watch the video above, and we cover it in detail with worked examples.
Why Python reads like English
Python was designed to be readable. That is not a selling point; it is the entire point. Guido van Rossum, who created Python in 1989, believed that code is read far more often than it is written. A script you write on Monday gets read by you on Friday, by a colleague next month, and maybe by someone else in a year's time. If it reads like English, all of those people can understand it.
This matters in finance more than anywhere else. You work under pressure, with tight deadlines and regulation breathing down your neck. You do not have time to decode cryptic syntax. You need to read a script, understand what it does, spot the error, fix it, and move on. Python lets you do that.
Most programming languages use heavy punctuation and bracket syntax to mark the structure of code. Python uses white space. That single choice makes it fundamentally more readable.
Consider this rough pseudocode in a typical language:
if (balance > threshold) { debit_account(balance); } else { log_error(); }
Now the Python version of the same logic:
if balance > threshold:
debit_account(balance)
else:
log_error()
The Python version reads like English. There are no brackets to parse, no semicolons to remember, no noise. The structure is obvious from indentation alone.
This matters. It means the barrier to reading code is much lower than in other languages. You can pick up a script, understand what it does, and modify it, often without formal training.
The three syntax rules you need
Follow these three patterns and you can read almost anything in Python.
Rule 1: Variables hold values
A variable is a container. You put a value into it, and you can use that value later by typing the variable's name.
account_balance = 1500000
threshold = 1000000
That is it. You created two variables. The first holds the number 1500000. The second holds the number 1000000. You can use these names anywhere in your script and Python will substitute the value.
Variables can hold numbers, text, lists, dates, or more complex objects. The syntax is always the same: name on the left, equals sign, value on the right.
account_balance = 1500000
account_name = "Treasury Account"
transaction_dates = ["2024-01-15", "2024-01-20", "2024-01-25"]
In finance work, you will read values from files and databases into variables constantly. Everything that follows is just using those variables to do something useful.
Rule 2: Indentation means scope
In Python, indentation tells you structure. The white space at the start of a line defines scope: which lines are inside a block and which are not.
Look at this:
if balance > threshold:
print("Balance is high")
email_manager()
print("Scan complete")
The first two lines after if are indented. That tells Python they only run if the condition is true. The last line is not indented, so it always runs.
Indentation replaces the brackets and braces that other languages use. It forces you to write code that looks like its structure. You cannot hide a line of code by sneaking it into a block; the indentation makes it obvious.
This matters when you are reading code. If a line is indented, it is conditional or inside a loop. If it is not, it runs in the main flow. You can trace the logic just by looking at the white space.
Indentation is not optional. If you get it wrong, the code will not run. Check it carefully; wrong indentation breaks the code.
Rule 3: Loops and conditions follow a pattern
Almost every useful script uses loops (repeat an action many times) and conditions (do this only if that is true). Both follow simple patterns in Python.
A weekly note on treasury, liquidity and practical Python. No spam, unsubscribe any time.
A condition:
if transaction_amount > 100000:
notify_risk_team()
elif transaction_amount > 50000:
log_to_watchlist()
else:
process_normally()
This reads almost like English: if the amount is more than 100000, notify the risk team. Else if it is more than 50000, log it. Otherwise, process it normally.
A loop:
for transaction in transaction_list:
if transaction["status"] == "pending":
settlement_date = calculate_settlement_date(transaction)
update_booking(transaction, settlement_date)
This also reads naturally: for each transaction in the list, if the status is pending, calculate the settlement date and update the booking.
These two patterns (if/elif/else and for loops) do the vast majority of work in finance scripts. You will use them constantly.
Reading code: trace before you write
Reading code is harder than writing it. When you write code, you know what you want to do. When you read code, you have to figure it out.
The best way to read code is to trace through it line by line, keeping track of what each variable holds at each step. This is the only reliable way to understand what code actually does, as opposed to what you think it should do.
Take this simple script:
accounts = ["AC001", "AC002", "AC003"]
high_balance_accounts = []
for account_id in accounts:
balance = get_balance(account_id)
if balance > 500000:
high_balance_accounts.append(account_id)
print(high_balance_accounts)
Trace through it:
- We create a list of three account IDs.
- We create an empty list to store results.
- We loop through each account ID.
- For each one, we fetch the balance.
- If the balance is more than 500000, we add the account ID to our results list.
- At the end, we print the results.
By the time you reach the print statement, you know what the script does: it finds all accounts with a balance above 500000 and prints their IDs. You traced through the logic.
When you read someone else's code, or your own three months later, do this for every script you read. It takes five minutes and saves you from misunderstanding the code.
Your first useful script
Working code teaches you more than theory.
Imagine you have a CSV file of transactions. You want to identify any where the amount is unusual (more than two standard deviations from the mean). Compliance teams do this weekly.
import csv
transactions = []
amounts = []
with open("transactions.csv") as file:
reader = csv.DictReader(file)
for row in reader:
amount = float(row["amount"])
amounts.append(amount)
transactions.append(row)
mean = sum(amounts) / len(amounts)
# This uses population standard deviation; use sample if n < 30
std_dev = (sum((x - mean) ** 2 for x in amounts) / len(amounts)) ** 0.5
threshold = mean + (2 * std_dev)
outliers = []
for transaction in transactions:
if float(transaction["amount"]) > threshold:
outliers.append(transaction)
for outlier in outliers:
print(f"{outlier['id']}: {outlier['amount']}")
This script does six things:
- Opens a CSV file and reads it.
- Extracts the amounts and stores them.
- Calculates the mean.
- Calculates the standard deviation.
- Identifies any transaction more than two standard deviations above the mean.
- Prints the results.
Every line follows the three rules above. It uses only variables, indentation, and loops.
Once you understand each line, you can modify this script for your own work. You can change the threshold, swap in different data, or alter what it prints. You control it.
The gap between "I know Python" and "I can use Python at work" is smaller than you think. A single script that solves one real problem beats a thousand tutorial examples. Write something that matters to your job, even if it is small.
What comes next
You now know the three rules that unlock Python syntax. You can read a script and trace through it. You can write simple loops and conditions.
Next, you will learn how to work with data. In finance, that means learning pandas (the library that lets you work with data tables like spreadsheets, but far more powerful) and how to read data from files and databases.
Start by reading through some actual scripts. Look at the banking and treasury workflows you do every week, and imagine how you would automate them. That is where Python becomes real.
For more depth on the basics, see our post on Python for finance: what it is, why it works, and how to start. Once you are ready to work with real data, reading a liquidity position with pandas walks through a practical task step by step.
The foundations are in place. The rest is building on them.
Get the next one in your inbox
A weekly note on treasury, liquidity and practical Python. No spam, unsubscribe any time.
Practitioner notes on treasury, liquidity, regulatory reporting and practical Python.
