What is a variable?
A variable is a named container that holds a value in memory. When you assign a value to a variable, Python remembers that name and retrieves the value whenever you reference it later. Think of it like a named position in a trading ledger: once you write "GBP_Cash" at the top of a column, you can look up that column by name whenever you need the current cash position.
In Python, creating a variable is simple:
balance = 50000
You have just created a variable named balance and assigned it the value 50000. Now, whenever you write balance in your code, Python retrieves that value.
Why variables matter in finance work
In treasury and finance, you work with numbers all day: cash positions, interest rates, settlement dates, counterparty limits, liquidity buffers. Without variables, your Python code would be a disconnected mess of literal numbers scattered everywhere. With variables, your code becomes a living model that holds and updates real positions.
Variables let your code do what treasury systems do: store a position, perform an action on it, update it as new information arrives, and retrieve it again. They are how you move from writing individual calculations to writing reusable, maintainable finance code.
More fundamentally, variables are the foundation for everything you will do next. Functions need variables as arguments. Loops iterate over variables. Data structures (lists, dictionaries, DataFrames) organize many variables together. Without mastering variables first, every subsequent concept becomes harder to grasp.
Creating and assigning variables in Python
Assignment is the core operation. You write the variable name, then an equals sign, then the value:
rate = 0.045
settlement_date = "2024-01-15"
notional = 1000000
is_active = True
Python infers the data type from what you give it. 0.045 is a float. "2024-01-15" is a string. 1000000 is an integer. True is a boolean. You do not need to declare the type upfront. Python figures it out.
This flexibility is one reason Python is so practical for finance work. You can move fast. But it also demands discipline: you must stay aware of what type you are actually holding. If you think settlement_date is a date object but it is really a string, your code will fail when you try to add days to it.
You can also assign multiple variables in one line:
bid, offer, mid = 1.5420, 1.5425, 1.54225
And you can reuse the name of an existing variable to create a new one:
rate = 0.045
rate_in_basis_points = rate * 10000
Here, rate_in_basis_points does not change rate. It holds a new value based on the old one. The original variable is untouched.
Working with data types
Python recognises several core data types. Understanding them now will save you hours of debugging later.
Integers are whole numbers. Use them for counts, notional amounts, or any value with no decimal point.
num_trades = 42
notional_amount = 5000000
Floats are decimal numbers. Use them for rates, yields, prices, or anything that requires precision.
spot_rate = 1.2450
discount_rate = 0.03875
pv = 99.512
Strings are text. Use them for dates (if you have not yet converted them to date objects), counterparty names, currency codes, or any text data.
currency = "GBP"
counterparty = "Bank of London plc"
settlement_date = "2024-01-15"
Booleans are True or False. Use them for flags: is this trade active? Is the collateral eligible? Has the position been settled?
is_active = True
is_eligible = False
requires_hedge = True
A weekly note on treasury, liquidity and practical Python. No spam, unsubscribe any time.
You can check the type of a variable with the type() function:
rate = 0.045
print(type(rate)) # <class 'float'>
trades = 15
print(type(trades)) # <class 'int'>
currency = "EUR"
print(type(currency)) # <class 'str'>
This is invaluable when you are unsure what a variable actually holds.
Naming variables so others (and you) understand them
In real finance code, never write x = 50000. Someone reading your code (or you, six months later) will have no idea what x represents. Is it a notional? A balance? A limit?
Use names that are transparent:
cash_position_gbp = 50000
usd_loan_rate = 0.045
settlement_date = "2024-01-15"
is_derivative_eligible = True
Python convention is to use lowercase with underscores, often called snake case. Names should be nouns or noun phrases that tell you what the variable holds. If you find yourself adding a comment to explain what a variable is, the name is not clear enough.
In a real finance codebase, clear naming saves your team time. It signals intent. It reduces the cognitive load for anyone reading the code. It also makes it easier to spot errors: cash_position_gbp tells you immediately that this is a GBP amount, so if you are adding a USD figure to it without converting, you will notice the problem sooner.
When you work in finance, clarity is not optional. It is a professional responsibility.
Reassigning variables as your program runs
A core feature of variables is that you can change their value. This is reassignment.
cash_balance = 1000000
print(cash_balance) # Output: 1000000
cash_balance = 950000
print(cash_balance) # Output: 950000
You have reassigned cash_balance to a new value. The old value is discarded. This mirrors what happens in a real treasury system: a position is updated throughout the day as trades settle and new cash flows arrive.
You can also update a variable based on its current value:
cash_balance = 1000000
# Add a cash inflow and store the result back in the same variable
cash_balance = cash_balance + 250000
print(cash_balance) # Output: 1250000
Or more concisely:
cash_balance += 250000
This syntax (+=, -=, *=, /=) is shorthand for "take the current value, do the operation, and store the result back in the same variable". It is so common in finance code that it is worth memorising.
notional = 5000000
notional -= 500000 # A trade settles, reduces notional
print(notional) # Output: 4500000
Variables as the foundation for everything else
Once you understand variables, every subsequent Python concept becomes tractable.
Functions take variables as arguments and return new variables:
def calculate_accrued_interest(principal, annual_rate, days):
daily_rate = annual_rate / 365
accrued = principal * daily_rate * days
return accrued
notional = 1000000
rate = 0.045
accrued_interest = calculate_accrued_interest(notional, rate, 30)
print(accrued_interest)
Here, the variables notional, rate, and the result accrued_interest are all working together through the function.
Loops iterate over variables and update them:
balances = [100000, 150000, 200000]
total = 0
for balance in balances:
total += balance
print(total) # Output: 450000
Data structures like lists and dictionaries store many variables together:
portfolio = {
"GBPUSD": 5000000,
"EURUSD": 3000000,
"USDJPY": 2000000
}
print(portfolio["GBPUSD"]) # Output: 5000000
Each of these concepts builds on variables. Master variables now, and you will find that functions, loops, and data structures are far less mysterious.
Practical takeaway
Variables are your mechanism for holding real data. Assign them with intention, name them clearly so others understand what they contain, and update them as your program runs. Every piece of finance code you write will rely on this skill. Start now, and you will find that the next concepts (functions, loops, data structures) click into place almost naturally.
Get the next one in your inbox
A weekly note across Finance & Treasury, Innovation & Automation and Career Development. No spam, unsubscribe any time.
Notes across finance and treasury, innovation and automation, and career development, written by practitioners who do the work.
