Skip to main content
Editor

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

Beginner20 min6 exercises110 XP

Leer en españolЧитать на русскомLer em portuguêsอ่านภาษาไทย

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
# Integers - whole numbers
students = 30
lives = 3
year = 2025
negative = -15
# Floats - decimal numbers
price = 9.99
temperature = 98.6
pi = 3.14159
tiny = 0.001
print(type(students)) # <class 'int'>
print(type(price)) # <class 'float'>
print(type(negative)) # <class 'int'>
print(type(temperature)) # <class 'float'>
Output
<class 'int'>
<class 'float'>
<class 'int'>
<class 'float'>

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
# Addition
print(10 + 3) # 13
# Subtraction
print(10 - 3) # 7
# Multiplication
print(10 * 3) # 30
# Division (always returns a float!)
print(10 / 3) # 3.3333333333333335
Output
13
7
30
3.3333333333333335

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
# Division ALWAYS gives a float
print(10 / 2) # 5.0 - not 5!
print(type(10 / 2)) # <class 'float'>
# Even when both numbers are integers
print(100 / 10) # 10.0
print(8 / 4) # 2.0
Output
5.0
<class 'float'>
10.0
2.0

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

Math with variables
# Real-world example: calculating a total
price = 12.50
quantity = 4
tax_rate = 0.08
subtotal = price * quantity
tax = subtotal * tax_rate
total = subtotal + tax
print(f"Subtotal: ${subtotal}")
print(f"Tax: ${tax}")
print(f"Total: ${total}")
Output
Subtotal: $50.0
Tax: $4.0
Total: $54.0

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. For positive numbers, that means it throws away the decimal part.

Integer division with //
# Regular division vs integer division
print(17 / 5) # 3.4 - regular division (float)
print(17 // 5) # 3 - integer division (rounds down)
print(10 / 3) # 3.3333333333333335
print(10 // 3) # 3
print(7 / 2) # 3.5
print(7 // 2) # 3
# Works with negative numbers too (rounds toward negative infinity)
print(-17 // 5) # -4 (not -3!)
Output
3.4
3
3.3333333333333335
3
3.5
3
-4

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 (%)
# Modulo gives the remainder
print(17 % 5) # 2 (17 = 5*3 + 2)
print(10 % 3) # 1 (10 = 3*3 + 1)
print(20 % 4) # 0 (20 = 4*5 + 0, divides evenly)
print(7 % 2) # 1 (7 = 2*3 + 1)
# The classic use: check if a number is even or odd
number = 42
if number % 2 == 0:
print(f"{number} is even")
else:
print(f"{number} is odd")
Output
2
1
0
1
42 is even

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). Languages like C and Java use a function for this, but Python makes it an operator. Simple and clean.

Exponents with **
# Exponentiation with **
print(2 ** 3) # 8 (2 cubed: 2 * 2 * 2)
print(5 ** 2) # 25 (5 squared: 5 * 5)
print(10 ** 6) # 1000000 (10 to the 6th power)
# Works with floats too
print(9 ** 0.5) # 3.0 (square root of 9!)
print(27 ** (1/3)) # 3.0 (cube root of 27)
Output
8
25
1000000
3.0
3.0

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
# Practical example: compound interest
# Formula: A = P * (1 + r) ** t
principal = 1000 # Starting amount
rate = 0.05 # 5% annual interest
years = 10 # Time period
amount = principal * (1 + rate) ** years
print(f"${principal} at {rate*100}% for {years} years = ${round(amount, 2)}")
Output
$1000 at 5.0% for 10 years = $1628.89

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
# Without parentheses - follows PEMDAS
print(2 + 3 * 4) # 14 (not 20!) - multiplication first
print(10 - 2 ** 3) # 2 - exponent first: 2**3=8, then 10-8=2
print(10 + 6 / 2) # 13.0 - division first: 6/2=3, then 10+3
# With parentheses - you control the order
print((2 + 3) * 4) # 20 - parentheses force addition first
print((10 - 2) ** 3) # 512 - parentheses first: 8**3=512
print((10 + 6) / 2) # 8.0 - parentheses first: 16/2=8
Output
14
2
13.0
20
512
8.0

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

  • () - Parentheses (always first)
  • ** - Exponentiation
  • -x - Negation, a minus sign in front of a value (so -2 ** 2 is -4)
  • *, /, //, % - 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()
    # abs() - absolute value (distance from zero)
    print(abs(-10)) # 10
    print(abs(10)) # 10
    print(abs(-3.7)) # 3.7
    # round() - round to nearest value
    print(round(3.7)) # 4
    print(round(3.2)) # 3
    print(round(3.14159, 2)) # 3.14 (round to 2 decimal places)
    print(round(1234, -2)) # 1200 (round to nearest hundred!)
    Output
    10
    10
    3.7
    4
    3
    3.14
    1200
    min() and max()
    # min() - smallest value
    print(min(5, 3, 8, 1)) # 1
    print(min(-10, -20, -5)) # -20
    # max() - largest value
    print(max(5, 3, 8, 1)) # 8
    print(max(-10, -20, -5)) # -5
    # Practical: clamp a value between bounds
    score = 150
    clamped = min(max(score, 0), 100) # Keep between 0 and 100
    print(f"Score {score} clamped to {clamped}") # Score 150 clamped to 100
    Output
    1
    -20
    8
    -5
    Score 150 clamped to 100

    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
    # float → int (truncates, does NOT round)
    print(int(3.9)) # 3 - chops off the decimal
    print(int(3.1)) # 3
    print(int(-2.7)) # -2 - truncates toward zero
    # int → float
    print(float(42)) # 42.0
    print(float(-7)) # -7.0
    # string → numbers
    print(int("25")) # 25
    print(float("3.14")) # 3.14
    Output
    3
    3
    -2
    42.0
    -7.0
    25
    3.14

    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
    # Mixing int and float - Python "promotes" to float
    result = 5 + 2.0
    print(result) # 7.0 (float, not int)
    print(type(result)) # <class 'float'>
    # This happens with all operators
    print(10 * 1.5) # 15.0
    print(7 - 0.5) # 6.5
    print(type(3 + 0.0)) # <class 'float'>
    Output
    7.0
    <class 'float'>
    15.0
    6.5
    <class '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

    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 //
    # / gives the full decimal answer
    print(7 / 2) # 3.5
    # // rounds DOWN to a whole number
    print(7 // 2) # 3
    # They give very different results!
    print(15 / 4) # 3.75
    print(15 // 4) # 3
    Output
    3.5
    3
    3.75
    3

    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
    # Item prices and quantities
    notebook_price = 4.50
    notebook_qty = 3
    pen_price = 1.25
    pen_qty = 2
    # Calculate the total cost
    # Print the result
    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
    number = 37
    # Use modulo to check if number is even or odd
    # Print: "{number} is even" or "{number} is odd"
    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
    total_pizzas = 10
    groups = 3
    # Bug: using the wrong division operators
    pizzas_each = total_pizzas / groups
    leftover = total_pizzas / groups
    print(f"Each group gets {pizzas_each} pizzas")
    print(f"Leftover pizzas: {leftover}")
    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
    fahrenheit = 72
    # Convert to Celsius using the formula
    # Round to 1 decimal place
    # Print the result
    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.

    print(10 // 3) # Your prediction:
    print(10 % 3) # Your prediction:
    print(2 ** 4) # Your prediction:
    print(15 // 4) # Your prediction:
    print(15 % 4) # Your prediction:
    print(type(10 / 2)) # Your prediction:
    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.

    principal = 1000
    rate = 0.05
    years = 10
    # Calculate the final amount using the formula
    # Calculate interest earned (final - principal)
    # Print the results

    Summary: Python Numbers and Math

    Here's a quick reference of every operator and function from this tutorial:

    OperatorNameExampleResult
    +Addition5 + 38
    -Subtraction5 - 32
    *Multiplication5 * 315
    /Division5 / 31.666...
    //Integer division5 // 31
    %Modulo (remainder)5 % 32
    **Exponentiation5 ** 3125

    Built-in functions:

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

    Related Tutorials

    Was this tutorial helpful?