Python for Humans · Draft v0.1 · Nov 16, 2026 · Draft position: chapter 2 · First draft of the numbers chapter, from integers and floats to a pizza party planner.
Numbers
Integers, Floats & Arithmetic
- You should already know
- Variables, input() and f-strings (chapter 1).
- You'll build
- A pizza party planner that works out how many pizzas to order, how many slices are left over, and what everyone owes.
- You'll be able to
- Tell integers from floats, use + - * / // % and **, convert text with int() and float(), round results, and format money in an f-string.
Strings are only half the story. In the introduction, you used Python to add, subtract, multiply, and divide, and your Time Machine used int() to turn an answer into a number. In this chapter, numbers get the same attention we just gave strings.
By the end, you'll build a program that plans a pizza party down to the last slice.
Two Kinds of Numbers
Run this code and look closely at the answers:
print(7 + 3) print(10 / 2) print(3 * 1.5)
7 + 3 gives 10, as you'd expect. But 10 / 2 gives 5.0, not 5. Where did that .0 come from?
Python has two kinds of numbers. Whole numbers like 10, -3, and 2026 are called integers, or int for short. Numbers with a decimal point, like 5.0, 1.5, and 3.14, are called floats, short for floating-point numbers.
To Python, 5 and 5.0 are equal, but they are different types. You can ask Python for the type of anything with the built-in type() function:
print(type(5)) print(type(5.0)) print(type("5"))
Python says int, float, and str, which is short for string. Three values that all look like five, and three different types!
Two rules explain the 5.0 you saw earlier:
- Division with
/always gives a float, even when the answer is a whole number. - If you mix an integer and a float, like
3 * 1.5, the answer is a float.
Splitting the Pizza
Here's a real-world problem. A pizza has 12 slices and there are 5 friends. How many slices does each person get?
slices = 12 friends = 5 print(slices / friends) print(slices // friends) print(slices % friends)
The first answer is 2.4. That's mathematically correct, but nobody wants four tenths of a slice of pizza.
The second line uses //, called floor division. It divides and then throws away anything after the decimal point, so it always gives a whole number: 2 slices each.
The third line uses %, called modulo. It doesn't divide at all. It gives you the remainder, what's left over after dividing: 2 slices.
So 12 slices for 5 friends means 2 slices each, with 2 left over. In programming, // and % are often used together, just like this.
Now change friends to 4, then 6, then 13. Predict each answer before you click Run. What does a remainder of 0 tell you?
Powers and Order
Python has one more math operator: **, which means "to the power of".
print(2 ** 10) print(2 ** 100)
2 ** 10 is 2 multiplied by itself 10 times: 1024. And 2 ** 100 is a 31-digit number! Many programming languages struggle with numbers that big, but Python integers can grow as large as you need.
When you combine operators, Python follows the same order of operations you may remember from math class: powers first, then multiplication and division, then addition and subtraction. Use parentheses to change the order.
print(2 + 3 * 4) print((2 + 3) * 4)
The first line gives 14 because Python multiplies before it adds. The parentheses in the second line tell Python to add first, so it gives 20. When in doubt, add parentheses. They make your intentions clear to Python and to the humans reading your code.
Turning Text into Numbers
You already know this one from the Time Machine: input() always gives you a string, even when someone types digits. To do math with the answer, you need to convert it.
int() converts text to an integer. Its partner, float(), converts text to a float, which is what you need for prices, measurements, or anything else with a decimal point.
pizzas = int(input("How many pizzas? ")) price = float(input("Price of one pizza? ")) print(pizzas * price)
Try 3 pizzas at 12.50 each. Then run it again and type 12.50 for the number of pizzas. Python shows a ValueError, because int() can't turn "12.50" into a whole number. Just like with strings, Python is very literal about numbers.
int() can also convert a float, but watch out: it doesn't round. It chops off everything after the decimal point, so int(7.9) gives 7.
A Strange Answer
Here's a puzzle. What do you think 0.1 + 0.2 equals? Run it and see:
print(0.1 + 0.2) print(round(0.1 + 0.2, 2))
Python says 0.30000000000000004! Computers store floats in binary, using ones and zeros, and most decimal numbers can't be stored exactly in binary. So floats are sometimes off by a tiny amount. This isn't a bug in Python. Almost every programming language does the same thing.
For everyday programs, that tiny difference doesn't matter. When you want a tidier answer, the built-in round() function rounds a number to as many decimal places as you ask for. round(0.1 + 0.2, 2) gives 0.3.
Formatting Money
Rounding helps, but prices still look odd:
total = 23.5 print(f"Total: ${total}") print(f"Total: ${total:.2f}") print(f"Big number: {1000000:,}")
$23.5 looks wrong for money. In an f-string, you can add a colon and a format after a variable's name. :.2f means "show this as a float with 2 decimal places", so you get $23.50.
The last line shows another handy format: :, adds commas to big numbers, so 1000000 becomes 1,000,000.
Build a Pizza Party Planner
Now let's put everything together. This program asks how many people are coming to your party and plans the pizza order.
print("🍕" * 12) print(" PIZZA PARTY PLANNER") print("🍕" * 12) people = int(input("How many people are coming? ")) slices_each = int(input("How many slices will each person eat? ")) price = float(input("How much does one pizza cost? ")) slices_per_pizza = 8 slices_needed = people * slices_each # Enough whole pizzas, plus one more for the extra slices. pizzas = slices_needed // slices_per_pizza + 1 leftover = pizzas * slices_per_pizza - slices_needed total = pizzas * price per_person = total / people print(f"{people} people × {slices_each} slices = {slices_needed} slices") print(f"Order {pizzas} pizzas.") print(f"There will be {leftover} slices left over.") print(f"Total cost: ${total:.2f}") print(f"Each person owes ${per_person:.2f}")
Try 5 people eating 3 slices each, at 12.99 a pizza. The party needs 15 slices. One pizza has only 8, so 15 // 8 gives 1 whole pizza, and we add one more for the remaining slices. That's 2 pizzas, 1 slice left over, $25.98 in total, and $5.20 each.
Look at everything this program uses: string repetition for the banner, int() and float() to convert input, // to count whole pizzas, a float for the price, division for each person's share, and f-strings with :.2f to format money.
Your turn: Plan your own party
- Change
slices_per_pizzato12for extra-large pizzas. Does your order change? - Add sales tax. If tax is 8%, try
total = pizzas * price * 1.08. - Add a tip, and print how much of the total it is.
- Change the banner to your own design.
Bonus: Try 4 people eating 2 slices each. They need 8 slices, exactly one pizza. What does the planner tell you to order? Is it right?
What's Next?
You now know Python's two main kinds of numbers, integers and floats, and how to do math with them. You can split things evenly with // and %, convert input with int() and float(), round numbers, and format money with f-strings.
But if you tried the bonus, you found a bug. When the slices divide evenly, our planner still adds an extra pizza, and 8 slices go to waste.
The fix needs something our programs can't do yet. If there's a remainder, order one more pizza. Otherwise, don't. Remember what a remainder of 0 tells you?
Our programs need to make decisions. That's what we'll explore next.