Before you write a function, you need to know what kinds of values Python works with. Every value has a type. The type decides how it behaves — most importantly, what happens when you do math with it. Scroll down: first meet the four core types, then see why division gives those long decimals, then type your own expressions and watch Python tell you the value and its type.
Python has many types, but four cover almost everything you do in accounting work. You can
always ask Python what type a value is with type(x).
type(5) # <class 'int'> a whole number type(5.0) # <class 'float'> a number with a decimal point type("x") # <class 'str'> text, in quotes type(True) # <class 'bool'> a yes/no value: True or False
Whole numbers, no decimal point. Counts: number of invoices, a quantity.
Numbers with a decimal point. Money, rates, anything that divides.
Text. Always in quotes. Names, account codes, descriptions.
A yes/no answer. The result of a comparison like amount > 0.
5 and 5.0 look like the same number,
but Python stores them differently — one is an int, the other a float. The type
you have changes what math does, which is exactly the surprise in the next step.This is the part that trips up every accountant the first time. Divide $100 three ways and Python hands you a number that will not stop.
float numbers in binary — base 2.
Lots of ordinary base-10 decimals, like 0.1, have no exact binary representation, the
same way 1/3 has no exact decimal (it is 0.3333... forever). So the computer keeps the closest
value it can, and a tiny rounding error shows up when you add or divide. This happens in Excel and every
programming language — Excel just hides it by rounding what it shows you on screen. Python shows you the
truth.
| Expression | Result | What it does |
|---|---|---|
7 / 2 | 3.5 | true division, always a float |
7 // 2 | 3 | floor division — divide and drop the remainder |
7 % 2 | 1 | modulo — just the remainder |
round(0.1 + 0.2, 2) | 0.3 | round to 2 decimal places for display |
round(x, 2) when you show it to a person.0.1 + 0.2 == 0.3 is False because
of that tiny tail. Compare rounded values, or check that the difference is tiny, instead.Type a single Python expression, predict what comes back, then Run it. You will see both the value and its type. This is real Python, running in your browser.
int, float, str, or
bool? Then Run and see if you were right. The surprises are the point.
Load an example, or type your own:
Things worth trying: type(5) versus type(5.0),
then 5 == 5.0 (are they equal?), then 0.1 + 0.2 == 0.3 (is it really 0.3?).
You now know the four types and why float math gets long. Next you will write your own
functions — and the very first thing a function does is take values of some type and hand back a
value of some type. When a function returns a weird long decimal, you will know it is just a float
being honest, and you will reach for round(x, 2).
int,
float, str, bool. type(x) tells you the type of any
value. The slash / always makes a float; // is floor division and
% is the remainder. round(x, 2) controls how a number is displayed. And the two
money rules: round for display, and never test floats for exact equality.