📢
Admissions Open for August 2026 Batch | Free Career Counselling | Limited Scholarships
Register Now →

Learning Guides

Numbers in Python: int, float and Number Operations

Quick answer: Learn how Python handles int, float and complex numbers, arithmetic operations, rounding, and common numeric interview questions.

Python's Numeric Types

age = 25            # int: whole numbers
price = 89000.50    # float: decimal numbers
complex_num = 3 + 4j  # complex: rarely used outside specialised maths

Arithmetic Operations

a, b = 10, 3

print(a / b)    # 3.333...  true division, always returns a float
print(a // b)   # 3         floor division, discards the remainder
print(a % b)    # 1         modulus, the remainder
print(a ** b)   # 1000      exponentiation

Rounding Numbers

print(round(3.456, 2))   # 3.46
print(round(3.456))      # 3
print(round(2.5))        # 2, not 3 -- Python rounds .5 to the nearest EVEN number

This "round half to even" behaviour, sometimes called banker's rounding, often surprises people expecting 2.5 to always round up to 3.

Converting Between Number Types

x = 10
y = float(x)   # 10.0

z = 3.99
w = int(z)     # 3, int() truncates rather than rounds

Useful Built-In Numeric Functions

print(abs(-15))        # 15
print(max(4, 9, 2))     # 9
print(min(4, 9, 2))     # 2
print(pow(2, 5))        # 32, same as 2 ** 5
print(sum([1, 2, 3, 4]))  # 10

A Common Floating Point Surprise

print(0.1 + 0.2)   # 0.30000000000000004, not exactly 0.3

This happens because most decimal fractions cannot be represented exactly in binary floating point, a limitation shared by nearly every programming language, not specific to Python. When comparing floats, check if they are close enough rather than exactly equal.

print(round(0.1 + 0.2, 2) == 0.3)   # True

The math Module

import math

print(math.sqrt(16))    # 4.0
print(math.ceil(4.2))   # 5
print(math.floor(4.8))  # 4
print(math.pi)          # 3.141592653589793

Common Interview Questions

Why does 0.1 + 0.2 not exactly equal 0.3 in Python?

Floating point numbers are stored in binary, and most decimal fractions cannot be represented exactly in binary, producing tiny rounding errors that are a property of floating point arithmetic in general, not a Python specific bug.

What is the difference between int() truncation and round()?

int() simply cuts off the decimal part regardless of its value. round() rounds to the nearest whole number, following the round-half-to-even rule for exact halves.

FAQ

Frequently Asked Questions

Why doesn't 0.1 + 0.2 exactly equal 0.3 in Python?

Floating point numbers are stored in binary, and most decimal fractions cannot be represented exactly in binary, producing tiny rounding errors. This affects nearly all programming languages, not just Python.

What is the difference between / and // for numbers in Python?

/ always performs true division and returns a float. // performs floor division and discards the remainder.

How does Python's round() function handle exact halves like 2.5?

It rounds to the nearest even number, so round(2.5) gives 2 and round(3.5) gives 4, a convention known as round-half-to-even or banker's rounding.

What is the difference between int() and round() when converting a float?

int() truncates by simply discarding the decimal part. round() rounds to the nearest whole number based on the actual decimal value.

Want This Mapped to Your Own Background?

A free counselling session will tell you which path fits, and will tell you honestly if none of ours does.

Book Free Career Counselling

Keep Reading

Related Articles