fuckingpython.lol

Learn Python › Lesson 4

Python numbers and math: a very fast calculator with trust issues

Updated · every example run on Python 3.9 to 3.15

Python is a calculator that never gets tired. It adds, multiplies, divides three different ways, handles numbers with more digits than your bank will ever see, and does it all in a blink. It also tells you, with a perfectly straight face, that 0.1 plus 0.2 is not 0.3.

This lesson covers both halves: the math that works the way you expect, and the handful of places where it doesn't, so you meet them here instead of in a bill that is off by a cent.

int and float

Lesson 2 introduced the two number types. An int is a whole number, a float has a decimal point. Do math with them and Python picks the type of the answer for you:

main.py
cats = 3
cat_weight = 4.5
print(cats + 2)
print(cats * cat_weight)
print(10 - 2.0)
print(type(cats + 2))
print(type(10 - 2.0))
print(9_999_999_999 * 9_999_999_999)
Python 3.9 to 3.15 all print
5
13.5
8.0
<class 'int'>
<class 'float'>
99999999980000000001

Add, subtract or multiply two ints and you get an int (division plays by its own rules, next section). Put a float anywhere in the mix and the answer is a float, even when it comes out whole, which is why 10 - 2.0 prints 8.0 and not 8.

The underscores in the last line are just for your eyes: Python ignores them, so 9_999_999_999 is the same number as 9999999999. And look at the answer. Python ints have no fixed size limit, so that product is exact down to the last digit, where a pocket calculator would have shrugged, shown you something like 9.999999998E19, and quietly dropped the 1 at the end. Floats are not like that: they keep only about 15 to 17 significant digits, and that is where the shit later in this lesson comes from.

Division, remainders and powers

Seventeen slices of pizza, five hungry people. Python has three ways to divide them, plus ** for powers:

main.py
slices = 17
people = 5
print(slices / people)
print(slices // people)
print(slices % people)
print(10 / 2)
print(2 ** 10)
print(-17 // 5)
print(2 + 3 * 4, (2 + 3) * 4)

people = 0
print(slices // people)
Python 3.15 prints
3.4
3
2
5.0
1024
-4
14 20
Traceback (most recent call last):
  File "/home/me/project/main.py", line 12, in <module>
    print(slices // people)
          ~~~~~~~^^~~~~~~~
ZeroDivisionError: division by zero
PythonPrints
3.9–3.13ZeroDivisionError: integer division or modulo by zero
3.14–3.15ZeroDivisionError: division by zero

Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on November 11, 2026.

  • / is true division and always gives a float, even when it divides evenly: 10 / 2 is 5.0.
  • // is floor division: how many whole slices each person gets.
  • % is the remainder: how many slices are left in the box for whoever gets there first.
  • ** is a power: 2 ** 10 is ten 2s multiplied together.

Careful with // and negative numbers. It rounds down, toward minus infinity, not toward zero, so -17 // 5 is -4, not -3. And Python does the powers first, then *, /, // and %, then + and -, the order from math class. Parentheses beat all of it, which is why the two answers on that line differ.

The last line divides by zero people, and Python refuses with a ZeroDivisionError. The wording changed in Python 3.14. Up to 3.13 it depended on the operator and the type, so // on ints said "integer division or modulo by zero" and / on a float said "float division by zero". From 3.14 on, every one of them just says "division by zero".

round() and its weird ties

round() rounds a number to the nearest whole number, or to as many decimal places as you ask for:

main.py
price = 19.987
print(round(price, 2))
print(round(price))
print(type(round(price)))
print(round(2.5))
print(round(3.5))
print(round(0.5))
Python 3.9 to 3.15 all print
19.99
20
<class 'int'>
2
4
0

For a float like price, no second argument gets you an int back, and a second argument gets you a float. Now the weird part: round(2.5) is 2, not 3. When a number sits exactly halfway, Python rounds to the nearest even number, so 2.5 goes down to 2, 3.5 goes up to 4, and 0.5 goes all the way down to 0. That rule is called round half to even, or banker's rounding, and it exists because always rounding halves up makes a long column of rounded numbers drift upward. It is not what you learned at school, and it is not a bug.

If you only want to show a number rounded, use the f-string format from lesson 3, f"{price:.2f}". That changes what gets printed and leaves the number itself alone.

0.1 + 0.2 is not 0.3

Here it is, the most famous "bug" in programming. == asks whether two values are equal and answers True or False; lesson 5 does comparisons properly.

main.py
total = 0.1 + 0.2
print(total)
print(total == 0.3)
print(round(total, 2) == 0.3)
print(round(2.675, 2))
Python 3.9 to 3.15 all print
0.30000000000000004
False
True
2.67

Floats are stored in binary, and in binary 0.1 has no exact form, the same way 1/3 has no exact form in decimal: 0.3333 and so on forever, cut off somewhere. So Python stores the closest binary number it can, and 0.1 plus 0.2 lands on a value a hair away from the stored 0.3. This is not Python being dumb. JavaScript, Java, C and nearly every other language store floats the same way and get the same not-quite-0.3 answer.

The last line is the same problem in disguise. 2.675 can't be stored exactly either: the stored value is a tiny bit below it, so it rounds down to 2.67.

What to do about it:

  • Don't compare floats with == as they are. Round before you compare, as the third print does.
  • For money, count cents in ints. $19.99 is 1999 cents, ints are exact, and // and % split it back into dollars and cents.
  • Most of the time, do nothing. The error shows up around the 17th digit, and for a cat's weight nobody gives a damn.

Converting: int(), float() and str()

Text that looks like a number is still text. "30" + 1 is the TypeError from lesson 3, and you will be handed number-shaped text all the time: from files, from the command line, and from input(), the function that asks the user to type something and always gives back a str:

age = int(input("How old are you? "))

int(), float() and str() convert between the types:

main.py
age_text = "30"
price_text = "4.75"
age = int(age_text)
price = float(price_text)
print(age + 1)
print(price * 2)
print(str(age) + " years")
print(int(4.99))
print(int(-4.99))
print(int("  42  "))
Python 3.9 to 3.15 all print
31
9.5
30 years
4
-4
42

int() on a float doesn't round: it chops off everything after the point, toward zero, so 4.99 becomes 4 and -4.99 becomes -4. Spaces around the text are fine; int() and float() ignore them. str() goes the other way and turns a number into text, so + can glue it to other text.

Break it: int() of something that isn't a whole number

int() of a string wants text that spells a whole number, like "42" or "-7". Hand it anything else and you get a ValueError. The one that catches everybody:

main.py
weight = "4.5"
print(float(weight))
print(int(weight))
Python 3.15 prints
4.5
Traceback (most recent call last):
  File "/home/me/project/main.py", line 3, in <module>
    print(int(weight))
          ~~~^^^^^^^^
ValueError: invalid literal for int() with base 10: '4.5'
PythonPrints
3.9–3.15ValueError: invalid literal for int() with base 10: '4.5'

Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on November 11, 2026.

float() reads "4.5" happily; int() refuses, because "4.5" is not how you write a whole number. The message is the same on every version from 3.9 to 3.15: "invalid literal for int() with base 10", followed by the text it choked on. You get the same thing from int("seven"), and from int("") when somebody just pressed Enter.

If the text might have a decimal point, go through float first: int(float(weight)) gives 4. The rest of the fixes, and where the bad text usually comes from, are on ValueError: invalid literal for int() with base 10.

Try it yourself

A dinner bill comes to 9475 cents, split between 4 friends. Using // and %, print how many whole cents each one pays, the same share in dollars with two decimals, and how many cents are left over for whoever loses at rock-paper-scissors. The output should look like Each pays 2368 cents ($23.68), 3 cents left over.

Show the answer
bill = 9475
friends = 4
share = bill // friends
left_over = bill % friends
print(f"Each pays {share} cents (${share / 100:.2f}), {left_over} cents left over")

The $ before the braces is just a character in the text. Only what is inside {} gets run.

Questions

How do I divide in Python without getting a decimal?

Use floor division, //: 17 // 5 is 3. It rounds down, so with negative numbers -17 // 5 is -4. The remainder is 17 % 5.

Why does 0.1 + 0.2 not equal 0.3 in Python?

Floats are stored in binary, where 0.1 and 0.2 can only be approximated, and their sum comes out as 0.30000000000000004. Every language that uses standard floating-point numbers does the same, so compare rounded values or count money in whole cents.

How do I round to 2 decimal places in Python?

round(x, 2) gives you the rounded number, and f"{x:.2f}" gives you text with exactly two decimals for printing. Both round exact halves to the even neighbor, and both work on the stored binary value, which is why round(2.675, 2) is 2.67.

How do I convert a string to a number in Python?

int("42") for whole numbers and float("4.75") for anything with a decimal point. int() raises a ValueError for text like "4.5" or "seven", so use float() when the text may have a decimal point.

Sources

Read on November 11, 2026.