Skip to main content

Python Numbers: Integers, Floats, and Math You'll Actually Use

Beginner20 min6 exercises110 XP
0/6 exercises

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.

Integers vs floats
Loading editor...

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.

The four basic operators
Loading editor...

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.

Division always returns a float
Loading editor...

You can use math with variables just like with plain numbers. This is where things get useful.

Math with variables
Loading editor...

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.

Integer division with //
Loading editor...

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.

Modulo operator (%)
Loading editor...

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.

Exponents with **
Loading editor...

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.

Compound interest with exponents
Loading editor...

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.

Order of operations
Loading editor...

Here's the full priority list, from highest to lowest:

  • () — Parentheses (always first)
  • ** — Exponentiation
  • *, /, //, % — Multiplication, division, floor division, modulo
  • +, - — Addition and subtraction
  • Confusing: relies on operator precedence
    result = base + rate * hours - discount / 2
    print(result)
    Clear: parentheses show intent
    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()

    abs() and round()
    Loading editor...
    min() and max()
    Loading editor...

    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.

    Converting between number types
    Loading editor...

    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.

    Automatic type promotion
    Loading editor...

    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

    Bug: expecting integer from /
    total_items = 10
    people = 2
    each = total_items / people
    print(f"Each gets {each} items")  # "Each gets 5.0 items"
    Fix: use // for integer result
    total_items = 10
    people = 2
    each = total_items // people
    print(f"Each gets {each} items")  # "Each gets 5 items"

    Mistake 2: Confusing / and //

    The difference between / and //
    Loading editor...

    Mistake 3: Forgetting Parentheses in Formulas

    Bug: wrong result without parentheses
    # Converting Fahrenheit to Celsius
    fahrenheit = 212
    celsius = fahrenheit - 32 * 5 / 9
    print(celsius)  # 194.22... WRONG!
    Fix: parentheses enforce correct order
    # 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.

    Exercise 1: Calculate the Total Cost
    Write Code

    You're buying items at a store. Calculate the total cost and print it.

  • 3 notebooks at $4.50 each
  • 2 pens at $1.25 each
  • Print the total in this exact format:

    Total: $16.0
    Loading editor...
    Exercise 2: Even or Odd Checker
    Write Code

    Use 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 odd

    If the number were even (like 42), the output would be:

    42 is even
    Loading editor...
    Exercise 3: Fix the Bug — Wrong Division
    Fix the Bug

    This 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: 1
    Loading editor...
    Exercise 4: Fahrenheit to Celsius Converter
    Write Code

    Convert 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.2C
    Loading editor...
    Exercise 5: Predict the Output
    Predict Output

    Without running the code, predict what each line will print. Write your predictions as comments, then run the code to check.

    Loading editor...
    Exercise 6: Compound Interest Calculator
    Write Code

    Calculate compound interest using this formula:

    **A = P * (1 + r) t

    Where:

  • P = principal (starting amount) = $1000
  • r = annual interest rate = 0.05 (5%)
  • t = number of years = 10
  • A = final amount
  • Calculate the final amount and the interest earned, then print:

    Final amount: $1628.89
    Interest earned: $628.89

    Round both values to 2 decimal places.

    Loading editor...

    Summary: Python Numbers and Math

    Here's a quick reference of every operator and function from this tutorial:
    ------------
    +Addition5 + 38
    -Subtraction5 - 32
    *Multiplication5 * 315
    /Division5 / 31.666...
    //Integer division5 // 31
    %Modulo (remainder)5 % 32
    **Exponentiation5 ** 3125
    Built-in functions:
    ---------
    abs(x)Absolute valueabs(-5)5
    round(x, n)Round to n decimalsround(3.14, 1)3.1
    min(a, b, ...)Smallest valuemin(3, 1, 4)1
    max(a, b, ...)Largest valuemax(3, 1, 4)4
    int(x)Convert to integerint(3.9)3
    float(x)Convert to floatfloat(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.