Reading history

Python Numbers: int, float, Rounding and the math Module

3 min read 1 views

Learn Python numbers: int and float, the / // % ** operators, how round() really works, the math module, and Decimal for exact money values.

Python has three kinds of number: int for whole numbers, float for decimals and complex for engineering maths. Most programs only need the first two. This lesson shows how they behave in arithmetic, how rounding really works, and when to reach for the math and decimal modules.

In this lesson you will learn

  • The difference between int and float
  • All of Python’s arithmetic operators, including //, % and **
  • How round() works and why it sometimes surprises people
  • Useful functions from the math module
  • How to handle money and compare decimals safely

int and float

A number without a decimal point is an int; one with a decimal point is a float. Python’s int has no upper limit, so huge results never overflow as they can in C.

whole = 42
price = 99.5
big = 7 ** 30
print(type(whole).__name__, type(price).__name__)
print(big)
print(10 / 4, 10 // 4, 10 % 4, 2 ** 10)
print(-7 // 2, -7 % 2)

Output

int float
22539340290692258087863249
2.5 2 2 1024
-4 1
OperatorMeaningExampleResult
/Division, always gives a float10 / 42.5
//Floor division: rounds down10 // 42
%Remainder10 % 42
**Power2 ** 101024

Notice -7 // 2 is -4, not -3. Python’s floor division always rounds towards minus infinity, and the remainder takes the sign of the divisor. This differs from C, where -7 / 2 is -3.

Rounding and Built-in Number Functions

import math
print(round(2.675, 2), round(3.14159, 3))
print(round(2.5), round(3.5))        # banker's rounding: to the nearest even
print(math.floor(4.7), math.ceil(4.2), math.sqrt(81))
print(abs(-15), max(3, 9, 4), min(3, 9, 4))
print(int("2026") + 1, float("2.5") * 2)
print(f"{1234567.891:,.2f}")

Output

2.67 3.142
2 4
4 5 9.0
15 9 3
2027 5.0
1,234,567.89
  • round(2.675, 2) gives 2.67, not 2.68, because 2.675 cannot be stored exactly in binary; the stored value is a hair below 2.675.
  • round(2.5) is 2 and round(3.5) is 4. Python rounds exact halves to the nearest even number (“banker’s rounding”), which avoids bias when you round many values.
  • math.floor() always goes down and math.ceil() always goes up.
  • The format code :,.2f adds thousands separators and two decimals, ideal for showing prices.

Comparing Decimals and Handling Money

from decimal import Decimal
import math, random
print(0.1 + 0.2 == 0.3, math.isclose(0.1 + 0.2, 0.3))
print(Decimal("0.10") + Decimal("0.20"))
random.seed(7)
print([random.randint(1, 6) for _ in range(5)])

Output

False True
0.30
[3, 2, 4, 6, 1]

Never compare floats with ==; use math.isclose(). For money, use Decimal built from strings, so every paisa is exact. The random module creates test data; random.seed(7) makes the “random” numbers repeat the same way every run, which helps when debugging.

Common Number Mistakes

  1. Expecting / to give a whole number. 6 / 3 is 2.0. Use // when you want an int.
  2. Using ^ for powers. 2 ^ 3 is bitwise XOR (1). Use 2 ** 3.
  3. Building Decimal from a float. Decimal(0.1) keeps the float’s tiny error. Write Decimal("0.1").
  4. Converting text with a decimal point using int(). int("2.5") raises ValueError; use float() first.

Practice Exercises

Exercise 1. A class has 47 students and each bench seats 3. How many full benches are there, and how many students are left over?

Show answer

47 // 3 is 15 full benches and 47 % 3 leaves 2 students.

Exercise 2. Print 1500000 as 1,500,000.00.

Show answer
print(f"{1500000:,.2f}")

Exercise 3. What do round(0.5) and round(1.5) return?

Show answer

0 and 2: exact halves go to the nearest even number.

Frequently Asked Questions

Is there a maximum int value in Python?

No. Python integers grow as large as memory allows. Only floats have a fixed range, up to about 1.8 × 10308.

How do I always round half up, like in school?

Use the decimal module: Decimal("2.5").quantize(Decimal("1"), rounding=ROUND_HALF_UP) gives 3.

What is the difference between // and int()?

// rounds down, so -7 // 2 is -4. int(-3.5) cuts towards zero, giving -3.

Summary

  • int holds whole numbers of any size; float holds decimals approximately.
  • / always returns a float; // rounds down; % gives the remainder; ** is power.
  • round() rounds halves to even; floats can make results look off by one in the last digit.
  • Use math.isclose() to compare floats and Decimal for money.
Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *