Python Numbers: Integers, Floats, and Math You'll Actually Use
You're splitting a restaurant bill with three friends. The total is $85. How much does each person owe? You grab your phone calculator, but a Python programmer just types 85 / 4 and gets the answer instantly.
Math is the backbone of programming. Games use it for physics. Apps use it to calculate prices. Data science runs on it. The good news? Python makes math feel like a supercharged calculator.
In this tutorial, you'll learn how Python handles numbers, what operators are available, and how to avoid the mistakes that trip up beginners. By the end, you'll be doing real-world calculations with confidence.
What Are Integers and Floats?
Python has two main types of numbers. Integers (int) are whole numbers with no decimal point. Floats (float) are numbers with a decimal point.
Think of it like money. "5 dollars" is an integer. "5.99 dollars" is a float. The decimal point is the difference.
When should you use each? Use integers for things you count: people, items, scores, lives. Use floats for things you measure: weight, temperature, percentages, money.
How to Do Math in Python
Python supports all the basic math operations you already know. The symbols look almost identical to what you'd write on paper.
Addition, subtraction, and multiplication work exactly as you'd expect. Division has one surprise: it always returns a float, even when the result is a whole number.
You can use math with variables just like with plain numbers. This is where things get useful.
What Are Integer Division (//) and Modulo (%)?
Python has two special operators that go together like peanut butter and jelly: integer division (//) and modulo (%). They answer two different questions about the same division problem.
Imagine you have 17 cookies and 5 friends. 17 // 5 tells you each friend gets 3 cookies. 17 % 5 tells you there are 2 cookies left over.
How Does Integer Division (//) Work?
Integer division (//) divides and rounds down to the nearest whole number. It throws away the decimal part entirely.
How Does the Modulo Operator (%) Work?
The modulo operator (%) gives you the remainder after division. It's incredibly useful and shows up more often than you'd expect.
The most common use of modulo is checking if a number divides evenly. If x % y == 0, then x is perfectly divisible by y. This is how you check for even/odd, leap years, and more.
How to Use Exponents in Python
Python uses ** for exponentiation (raising a number to a power). Most languages use a function for this, but Python makes it an operator. Simple and clean.
Here's a neat trick: raising a number to the power of 0.5 gives you its square root. And ** (1/3) gives the cube root. You don't need a special function for basic roots.
What Is the Order of Operations in Python?
Python follows the same order of operations you learned in math class. You might know it as PEMDAS: Parentheses, Exponents, Multiplication/Division, Addition/Subtraction.
Here's the full priority list, from highest to lowest:
() — Parentheses (always first)** — Exponentiation*, /, //, % — Multiplication, division, floor division, modulo+, - — Addition and subtractionresult = base + rate * hours - discount / 2
print(result)result = base + (rate * hours) - (discount / 2)
print(result)What Built-in Math Functions Does Python Have?
Python comes with several built-in functions for common math tasks. No imports needed — these are available everywhere.
How to Use abs(), round(), min(), and max()
How to Convert Between Integers and Floats
Sometimes you need to switch between number types. Use int() to convert a float to an integer, and float() to convert an integer to a float.
Python also handles automatic conversion in some situations. When you mix an integer and a float in a math operation, Python automatically converts the integer to a float.
What Are Common Number Mistakes in Python?
Let's look at the mistakes that trip up Python beginners. Knowing these in advance will save you hours of debugging.
Mistake 1: Expecting / to Return an Integer
total_items = 10
people = 2
each = total_items / people
print(f"Each gets {each} items") # "Each gets 5.0 items"total_items = 10
people = 2
each = total_items // people
print(f"Each gets {each} items") # "Each gets 5 items"Mistake 2: Confusing / and //
Mistake 3: Forgetting Parentheses in Formulas
# Converting Fahrenheit to Celsius
fahrenheit = 212
celsius = fahrenheit - 32 * 5 / 9
print(celsius) # 194.22... WRONG!# Converting Fahrenheit to Celsius
fahrenheit = 212
celsius = (fahrenheit - 32) * 5 / 9
print(celsius) # 100.0 CORRECT!Practice Exercises
Time to put your number skills to work! These exercises use real-world scenarios so you can see how Python math applies to everyday problems.
Try each one on your own before checking hints. Making mistakes is part of learning.
You're buying items at a store. Calculate the total cost and print it.
Print the total in this exact format:
Total: $16.0Use the modulo operator (%) to check if a number is even or odd.
A variable number is given with the value 37. Print the result in this exact format:
37 is oddIf the number were even (like 42), the output would be:
42 is evenThis code is supposed to split 10 pizzas equally among 3 groups and report how many are left over. But it's using the wrong division operators!
Fix the code so the output is:
Each group gets 3 pizzas
Leftover pizzas: 1Convert a temperature from Fahrenheit to Celsius using this formula:
**Celsius = (Fahrenheit - 32) * 5 / 9**
A variable fahrenheit is given with the value 72. Calculate the Celsius value, round it to 1 decimal place, and print:
72F = 22.2CWithout running the code, predict what each line will print. Write your predictions as comments, then run the code to check.
Calculate compound interest using this formula:
**A = P * (1 + r) t
Where:
Calculate the final amount and the interest earned, then print:
Final amount: $1628.89
Interest earned: $628.89Round both values to 2 decimal places.
Summary: Python Numbers and Math
| Here's a quick reference of every operator and function from this tutorial: | |||
|---|---|---|---|
| --- | --- | --- | --- |
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 5 / 3 | 1.666... |
// | Integer division | 5 // 3 | 1 |
% | Modulo (remainder) | 5 % 3 | 2 |
** | Exponentiation | 5 ** 3 | 125 |
| Built-in functions: | ||
|---|---|---|
| --- | --- | --- |
abs(x) | Absolute value | abs(-5) → 5 |
round(x, n) | Round to n decimals | round(3.14, 1) → 3.1 |
min(a, b, ...) | Smallest value | min(3, 1, 4) → 1 |
max(a, b, ...) | Largest value | max(3, 1, 4) → 4 |
int(x) | Convert to integer | int(3.9) → 3 |
float(x) | Convert to float | float(5) → 5.0 |
What's Next?
In the next tutorial — [Python Strings](/python/python-strings) — you'll learn how to work with text in Python. You'll discover slicing, string methods, and how to combine strings and numbers into readable output.