Numeric Types: int and float
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Python has two primary numeric types for everyday programming: int for whole numbers and float for decimal values.
Arbitrary-precision integers
Integer (int) is the type for whole numbers such as 7, 0 or -250. A Python int has no fixed size, so it never overflows.
In Python 3, integers have arbitrary precision. Unlike Java's 32-bit int or 64-bit long, Python integers never overflow.
big_number = 10 ** 30print(big_number)Example 1 · An integer that never overflows
print(2 ** 100)1267650600228229401496703205376
What this code does
2 ** 100means 2 raised to the power 100.- The result is far larger than 64 bits, and Python prints it exactly.
Python automatically allocates as many bytes of memory as needed to store the exact integer value.
Readability with underscores
When writing large numbers, place underscores between digits to make the value easy to read. Python ignores the underscores.
annual_revenue = 1_500_000print(annual_revenue)The console prints 1500000. Use underscores for currency, serial codes, or financial figures.
Floating-point numbers and precision
Float is the type for numbers with a decimal point, such as 68.5. Floats are stored in binary, so some decimals are only close approximations.
A float represents a real number with a decimal point. Python floats follow the IEEE 754 double-precision standard.
weight = 68.5ratio = 1e-3print(weight)print(ratio)Example 2 · A float is an approximation
total = 0.1 + 0.2print(total)print(round(total, 2))0.30000000000000004 0.3
What this code does
0.1and0.2cannot be stored exactly in binary.- Their sum prints with a tiny error at the end.
round(total, 2)gives the clean value 0.3.
1e-3 represents scientific notation for 1 * 10⁻³, which evaluates to 0.001.
Rounding with round()
round() is a built-in function that returns a number rounded to a given count of decimal places. Without the count it returns a whole number.
Use the built-in round(number, digits) function to round a floating-point number to a specific number of decimal places.
price = 19.8567print(round(price, 2))Example 3 · Interest on a savings balance
account_balance = 2_500_000interest_rate = 0.0725interest_earned = account_balance * interest_rate
print(account_balance)print(interest_earned)print(round(interest_earned, 2))2500000 181250.0 181250.0
What this code does
account_balanceuses underscores for readability.interest_earnedcalculates 2500000 * 0.0725 = 181250.0.round()formats the float to 2 decimal places.
The console displays 19.86. When digits is omitted, round() returns the nearest integer.
Next step
Check your understanding
9 quick questions