This post is for treasury and reporting teams building Python skills from scratch. Read on for the detail on data types, worked examples from finance scenarios, and Python code you can run right now.
Why Data Types Matter in Finance
Data types are not abstract theory. They are the foundation of every piece of Python code you will write in treasury, reporting, or risk. A data type tells Python how to store a value, how to interpret it, and what operations you can perform on it. Get this wrong and your program will produce wrong results silently: a balance sheet that does not balance, a rate calculation that is off by a factor of 10, or a file that looks correct until someone tries to use it.
Read on for the four data types you use every single day in finance work, how to convert between them, how to spot when you have used the wrong type, and how to debug when something breaks.
The Four Core Data Types You Need
Python has many data types. For finance work, you need four.
Integers: Whole Numbers for Counts and Flags
An integer is a whole number. No decimal point. In treasury and reporting, you use integers for counts: the number of counterparties, the number of transactions in a batch, the number of days in a settlement cycle. You also use them for flags: a status code, a booking date offset, or a simple true or false represented as 1 or 0.
transaction_count = 1250
settlement_days = 2
batch_id = 20240115001
counterparties_flagged = 0
print(type(transaction_count))
Output:
<class 'int'>
Unlike some languages, Python integers do not overflow. You can store very large settlement amounts as integers (in cents, not pounds, to avoid decimals) without running into size limits.
One thing to watch: when you divide two integers, Python gives you a float back, not an integer. We will see why that matters in a moment.
Floats: Numbers with Decimals for Money and Rates
A float is a number with a decimal point. In finance, floats are everywhere: interest rates, FX forwards, bond yields, and money amounts. Any balance or P&L figure that might have pence in it should be a float.
interest_rate = 0.0475
usd_gbp_rate = 1.2734
balance_gbp = 1250000.50
daily_accrual = 542.33
print(type(interest_rate))
Output:
<class 'float'>
Floats are approximate. They are stored in binary, not decimal. This means that some decimal numbers (like 0.1) cannot be stored exactly. In practice, for balance sheet work, you will see tiny rounding errors accumulate over many calculations.
# This looks odd, but it is what binary storage does
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)
Output:
0.30000000000000004
False
In production systems, amounts are often stored as integers (in pence) and only converted to floats for display. For Treasury and reporting code, be aware of this. If you are reconciling balances to the penny, use the Decimal type (from the decimal module) instead of float. For analysis and preliminary numbers, float is fine.
In production finance code, store amounts as integers in pence, cents or basis points and convert to float only for display or analysis. It sidesteps rounding errors entirely.
Strings: Text and Everything You Read from a File
A string is text. It is anything in quotes: single, double, or triple. In finance work, strings are everywhere. They represent ISIN codes, counterparty names, file paths, column headers, and data you read from CSV files or databases.
isin = "GB0002374006"
counterparty = "Barclays plc"
report_date = "2024-01-15"
file_path = "/data/treasury_positions.csv"
print(type(isin))
print(type(counterparty))
Output:
<class 'str'>
<class 'str'>
Beginners often miss this: when you read data from a file or user input, Python treats it as a string by default. If you read "1500000" from a CSV file, Python sees it as the text "1500000", not the number 1500000. You cannot add, subtract, or multiply strings as if they were numbers.
# This raises a TypeError
balance_from_file = "1500000"
interest_accrued = 25000
result = balance_from_file + interest_accrued
You will get an error. But the point is: a string looks like a number, but it is not. You have to convert it first.
Booleans: True or False
A boolean is the simplest type: it is either True or False. That is all. In finance code, booleans flag conditions: is this counterparty approved for new business? Has this trade been settled? Is this rate within tolerance?
is_counterparty_approved = True
settlement_complete = False
is_hedged = True
print(type(is_counterparty_approved))
Output:
<class 'bool'>
Booleans come from comparison operations: when you ask "is A greater than B?" or "does this code match that code?" Python answers with True or False. You will use booleans constantly in filtering data, validating inputs, and controlling the flow of your code.
balance = 1500000
threshold = 1000000
exceeds_threshold = balance > threshold
print(exceeds_threshold)
print(type(exceeds_threshold))
Output:
True
<class 'bool'>
How Python Stores and Interprets Types
Python is dynamically typed. This means you do not declare a type when you create a variable. Python looks at the value you assign and figures out the type automatically.
A weekly note on treasury, liquidity and practical Python. No spam, unsubscribe any time.
x = 42 # Python sees a whole number, so x is an int
y = 42.0 # Python sees a decimal point, so y is a float
z = "42" # Python sees quotes, so z is a string
This is flexible and fast to write. The cost is that you, the programmer, have to keep track of types as you go. Python will not stop you from doing something nonsensical; it will just give you an error or wrong output.
Type Conversion: From Strings to Numbers and Back
Sooner or later, you will need to convert from one type to another. You convert types using int(), float(), or str() functions. This is a deliberate instruction to Python to interpret a value in a different way.
String to Number (Parsing User Input or File Data)
The most common conversion in finance code is parsing strings from files and user input. You need to turn a string into a number so you can calculate with it.
# Data read from a CSV file comes in as strings
balance_string = "2500000.50"
interest_rate_string = "0.0425"
transaction_count_string = "1250"
# Convert to numbers
balance = float(balance_string)
rate = float(interest_rate_string)
count = int(transaction_count_string)
print(balance + 500) # Now this works
print(type(balance))
Output:
2500500.5
<class 'float'>
Use int() for whole numbers and float() for decimals. If you try to convert a string that does not look like a number, Python will raise an error and stop.
# This will crash
bad_conversion = int("2500.50") # ValueError: invalid literal
Number to String (Formatting Output)
The reverse: when you need to write a number to a file, send it in an email, or print it in a report, convert it to a string.
balance = 2500000.50
rate = 0.0425
# Convert to string
balance_text = str(balance)
rate_text = str(rate)
print("Current balance: " + balance_text)
print("Interest rate: " + rate_text)
Output:
Current balance: 2500000.5
Interest rate: 0.0425
For finance reporting, you often want to control the format: number of decimal places, thousand separators, currency symbols. Python's f string (formatted string literal) does this cleanly.
balance = 2500000.50
rate = 0.0425
# Format with control
print(f"Balance: £{balance:,.2f}")
print(f"Rate: {rate:.3%}")
Output:
Balance: £2,500,000.50
Rate: 4.250%
Float to Integer (Rounding and Truncation)
To convert a decimal to a whole number, use int() or round(). The int() function truncates (cuts off the decimal). The round() function rounds to the nearest integer (or to a specified number of decimal places).
accrual_daily = 542.87
accrual_cumulative = 15679.456
# Truncate
truncated = int(accrual_daily)
print(truncated) # 542
# Round
rounded = round(accrual_cumulative, 2)
print(rounded) # 15679.46
Output:
542
15679.46
In finance, rounding matters; choose round to nearest, floor, or ceiling based on your requirement.
Common Type Mistakes in Finance Code
These happen when reading data from files or APIs.
Treating a balance as a string when it should be a number. You read "1500000.50" from a file, forget to convert it, and later try to add interest. Python crashes or gives you a concatenation instead of a sum.
Forgetting that division of two integers produces a float. You divide settlement days or transaction counts and get a float when you expected an integer.
days_total = 365
num_quarters = 4
days_per_quarter = days_total / num_quarters
print(days_per_quarter)
print(type(days_per_quarter))
Output:
91.25
<class 'float'>
Assuming a boolean is a number. In Python, True is 1 and False is 0, and you can add them to numbers. This works, but it is confusing and error prone.
settled = True
unsettled = False
print(settled + unsettled) # 1
Output:
1
Avoid this pattern. Convert booleans to numbers explicitly if needed; keep them as booleans otherwise.
Reading currency amounts with thousand separators or currency symbols. A CSV file might have "£2,500,000.50" or "2500000.50 GBP". Strings have to be cleaned before conversion.
# This will crash
bad = float("£2,500,000.50")
Remove symbols and separators before conversion.
raw = "£2,500,000.50"
cleaned = raw.replace("£", "").replace(",", "")
amount = float(cleaned)
print(amount)
Output:
2500000.5
How to Inspect and Debug Types
When code breaks, check whether types are what you expect. Use the type() function.
balance = 1500000
rate = 0.0425
isin = "GB0002374006"
is_approved = True
print(type(balance))
print(type(rate))
print(type(isin))
print(type(is_approved))
Output:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
When you read data from a file or an API, always check the type. You will be surprised how often something you expected to be a number is actually a string.
# Simulating data read from a file
data = ["1500000", "0.0425", "GBP"]
for item in data:
print(f"{item} is type {type(item)}")
Output:
1500000 is type <class 'str'>
0.0425 is type <class 'str'>
GBP is type <class 'str'>
All file input arrives as strings. You must convert before calculation.
Takeaway
Data types determine whether your code works. You have four core types to master: integers for counts and flags, floats for money and rates, strings for everything you read from a file, and booleans for conditions.
Python decides types automatically when you assign a value. That is flexible, but it means you have to think about types as you code. When you read data from a file or user input, it comes in as a string. You have to convert it to a number before you can calculate with it. Use int() and float() for conversion. Use the type() function to check what you actually have.
Build the habit: every time you read data, ask "what type is this really?" The five seconds you spend will save you hours of debugging a silent error later.
Next week: working with lists and dictionaries to handle multiple values.

The Complete Python Course
Welcome to the most practical and beginner friendly Python Bootcamp Course on YouTube.
Take the courseGet 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.
