Python: warming up...

Python Types Trainer

Built-in AI tutor. Confused by a result? Ask the helper on this page. It coaches, it does not just hand you the answer.

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.

Step 1

The four types you use every day

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

int

5, 0, -12, 1000

Whole numbers, no decimal point. Counts: number of invoices, a quantity.

float

5.0, 3.14, 87.5

Numbers with a decimal point. Money, rates, anything that divides.

str

"INV-001", "Acme"

Text. Always in quotes. Names, account codes, descriptions.

bool

True, False

A yes/no answer. The result of a comparison like amount > 0.

Why the type matters. 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.
Step 2

Why division gives long decimals

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.

10 / 3 -> 3.3333333333333335 # the slash / ALWAYS makes a float, even 10 / 5 gives 2.0, not 2
0.1 + 0.2 -> 0.30000000000000004 # the classic. Not 0.3. There is a tiny tail of error.
This is not a Python bug. Computers store 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.

Two more division tools, and how to clean it up

ExpressionResultWhat it does
7 / 23.5true division, always a float
7 // 23floor division — divide and drop the remainder
7 % 21modulo — just the remainder
round(0.1 + 0.2, 2)0.3round to 2 decimal places for display
The accounting takeaway. Dividing $100 three ways really does give 33.333... — the math is correct, it is the presentation that needs work. Interest, allocations, and any rate math will produce long decimals. So two rules you will use all term:
  • Round money for display with round(x, 2) when you show it to a person.
  • Never test floats for exact equality. 0.1 + 0.2 == 0.3 is False because of that tiny tail. Compare rounded values, or check that the difference is tiny, instead.
Step 3

Try it yourself — the value and its type

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.

Predict first. Before you click Run, say out loud (or to yourself) two things: what value do you expect, and what type will it be — 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:

Result
Type an expression and click Run.

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?).

What to carry into the function trainer

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).

The vocabulary you are building. A type is the kind of a value: 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.