Advertisement
🌱 Beginner

Python Numbers – int, float, complex and Math Operations

Numbers are at the core of almost every program. Python has three built-in number types: int (whole numbers), float (decimal numbers), and complex (numbers with imaginary parts). This lesson covers all three, along with arithmetic operators, the math module, rounding, and common number operations.

⏱️ 18 min read 🎯 Beginner 📅 Updated 2026

Integers (int)

Integers are whole numbers — positive, negative, or zero. Python integers have unlimited precision: they can be arbitrarily large with no overflow.

Python
x = 10
y = -5
z = 0
big = 10 ** 100  # Python handles huge numbers!

print(type(x))   # <class 'int'>
print(big)       # 1 followed by 100 zeros
▶ Output
<class 'int'> 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Floats (float)

Floats are decimal numbers. They use 64-bit IEEE 754 double-precision, which means they have ~15-17 significant digits but can have precision issues.

Python
pi = 3.14159
temp = -23.5
scientific = 1.5e-3   # 0.0015

print(type(pi))   # <class 'float'>
print(0.1 + 0.2)  # Floating point gotcha!
print(round(0.1 + 0.2, 2))  # Fix: use round()
▶ Output
<class 'float'> 0.30000000000000004 0.3
💡
Tip

Never use floats for financial calculations. Use the decimal module instead: from decimal import Decimal

Advertisement

Arithmetic Operations

Python supports all standard arithmetic operations. Note: ** is power, // is floor division, % is modulo (remainder).

Python
a, b = 17, 5

print(a + b)   # 22  - addition
print(a - b)   # 12  - subtraction
print(a * b)   # 85  - multiplication
print(a / b)   # 3.4 - true division (always float)
print(a // b)  # 3   - floor division (rounds down)
print(a % b)   # 2   - modulo (remainder)
print(a ** b)  # 1419857 - power (17^5)
▶ Output
22 12 85 3.4 3 2 1419857

The math Module

Python's built-in math module provides advanced mathematical functions.

Python
import math

print(math.sqrt(16))    # 4.0 - square root
print(math.pi)          # 3.141592653589793
print(math.ceil(4.2))   # 5 - round up
print(math.floor(4.8))  # 4 - round down
print(math.log(100, 10))# 2.0 - log base 10
print(math.factorial(5))# 120
print(math.gcd(48, 18)) # 6 - greatest common divisor
▶ Output
4.0 3.141592653589793 5 4 2.0 120 6

Number Type Conversion

Convert between number types with int(), float(), and complex(). Note that int() truncates (does not round) floats.

Python
print(int(3.9))     # 3 - truncates, does NOT round
print(float(5))     # 5.0
print(int("42"))    # 42 - from string
print(float("3.14"))# 3.14

# Check if string is numeric before converting
value = "123"
if value.isdigit():
    print(int(value) * 2)  # 246
▶ Output
3 5.0 42 3.14 246

Numbers: int Is Unbounded, float Is Not Exact

Python has two everyday number types with very different behavior. int has arbitrary precision — it never overflows. float is IEEE-754 double precision — fast, but it can't represent many decimals exactly.

2 ** 1000            # a 300+ digit int — no overflow, ever
0.1 + 0.2            # 0.30000000000000004  ← float rounding!
0.1 + 0.2 == 0.3     # False (surprises everyone)

7 // 2               # 3   floor division
7 % 2                # 1   remainder
7 / 2                # 3.5 true division always gives float
NeedUse
counting, ids, big integersint
measurements, sciencefloat
money, exact decimalsdecimal.Decimal

The money rule: never use float for currency — those tiny rounding errors compound. Use Decimal("0.1") for exact decimal math. And to compare floats, don't use ==; use math.isclose(a, b) to allow for the tiny representation error. Division with / always returns a float even for whole results (4 / 2 == 2.0); use // when you want an integer.

🏋️ Practical Exercise

Work with both numeric types:

  1. Create an integer and a float, then print the type() of each.
  2. Compute integer division, float division, modulo, and exponentiation on two numbers.
  3. Use the math module to find a square root and round a value up with math.ceil().
  4. Convert a float to an int and observe how the decimal part is truncated.

🔥 Challenge Exercise

Write a program that takes the radius of a circle and reports its area and circumference using math.pi, rounded to two decimal places. Then demonstrate the classic floating-point surprise by printing 0.1 + 0.2 and explain in a comment why it is not exactly 0.3. Bonus: fix the comparison using math.isclose().

📋 Summary

  • Python has three numeric types: int (unlimited precision), float, and complex.
  • / always returns a float; // performs floor (integer) division.
  • % gives the remainder and ** raises to a power.
  • Floats follow IEEE 754, so some decimals (like 0.1) cannot be represented exactly.
  • The math module provides sqrt, ceil, floor, pi, and more.
  • Use int() and float() to convert; int() truncates toward zero.

Interview Questions on Numbers

  • What numeric types does Python provide?
  • What is the difference between / and // in Python?
  • Why is 0.1 + 0.2 not exactly 0.3?
  • How do you round a number up or down in Python?
  • What does the % operator do with numbers?
  • How do you convert between int and float?
  • What is the difference between round() and math.floor()?

FAQ

Why does 0.1 + 0.2 give 0.30000000000000004? +

Floats are stored in binary (IEEE 754), and 0.1 and 0.2 have no exact binary representation, so a tiny rounding error remains. Use round() for display or math.isclose() for comparisons, and the decimal module when exactness matters (e.g. money).

Is there a limit to how big an integer can be in Python? +

No. Python integers have arbitrary precision and grow to fit available memory, so you can compute huge factorials without overflow — unlike fixed-width integers in C or Java.

What is the difference between // and /? +

/ is true division and always returns a float (7 / 2 == 3.5). // is floor division and returns the largest whole number not greater than the result (7 // 2 == 3).

When should I use the decimal or fraction modules? +

Use decimal for exact base-10 arithmetic like currency, and fractions for exact rational numbers. Both avoid the binary rounding issues of float at the cost of speed.