HOWTO · Python
Float vs Int in Python
Learn when Python uses int or float, how to convert input before arithmetic, and how to choose a numeric type for counts, measurements, and calculations.
On this page
Python’s int type represents whole numbers with arbitrary precision, while float represents numbers using binary floating-point and can approximate decimal values. Use int for counts and whole-number quantities, and use float when a value needs a fractional part. Because input() returns text, convert user input before doing numeric arithmetic.
What Is the Difference Between int and float in Python?
An int can represent positive or negative whole numbers, including zero, without a fractional part. Python integers can grow as needed, subject to available memory. A float can represent fractional values such as 2.5 and values written in scientific notation. Integers preserve whole-number values exactly; many decimal fractions cannot be represented exactly in binary floating-point, so a calculation may produce a nearby value.
Both types support arithmetic, but the type affects the result. Adding two integers produces an integer. An operation that combines an int with a float commonly produces a float, so a whole-number input can become a floating-point result when used in a calculation with a fractional value. The built-in type() function can help inspect a value while learning or troubleshooting.
How Python Handles Numeric Input
The built-in input() function reads a line and returns it as a str, even when the characters look like a number. Consequently, adding two unconverted inputs joins their text: the strings "2" and "3" combine as "23". Convert each value with int() or float() before arithmetic, choosing the conversion that matches the value you expect.
Run this example with the two text values shown:
first_text = "2"
second_text = "3"
print(first_text + second_text)
first = int(first_text)
second = int(second_text)
print(first + second)
print(type(first).__name__, type(float("2.5")).__name__)
23
5
int float
For an interactive program, the same conversions can wrap calls to input(), such as int(input("Count: ")) or float(input("Measurement: ")). The entered text must be a valid representation for the chosen conversion; otherwise Python cannot convert it. Keep the raw string if you also need to preserve exactly what the user typed.
Working with int Values
Use int for counts, indexes, and quantities that are defined in whole units. Integer literals can also use binary, octal, and hexadecimal notation: write prefixes such as 0b, 0o, and 0x when those representations make a value easier to read. These prefixes describe the same integer value in different bases; they do not create different numeric types.
int() converts a suitable value to an integer. When converting a float, it discards the fractional part by truncating toward zero, so it is not a rounding operation. This distinction matters when a measured or calculated value must be rounded according to a rule: choose an explicit rounding approach instead of assuming int() rounds. Use type(value) or type(value).__name__ to inspect a value’s type when debugging conversion or arithmetic.
Working with float Values
Use float() to convert suitable numeric text or another number to a floating-point value. A decimal point is not required: float("4") represents the value as a float, while float("2.4") represents a fractional value. Floating-point arithmetic is useful for measurements and ratios, but its binary representation can make some decimal results appear with a small precision difference. Avoid comparing computed floats as if every decimal fraction were stored exactly; for application-specific comparisons, use an appropriate tolerance or a decimal arithmetic approach.
Scientific notation uses e or E to express a power of ten, as in 1.2e3 for 1,200. Read more about scientific notation. A float can be combined with an integer in arithmetic, and the resulting value is commonly a float. This is convenient, but the possible approximation still applies.
Run this example:
amount = float("2.5")
float_total = amount + 1.25
mixed_total = 2 + amount
print(float_total)
print(mixed_total)
3.75
4.5
float("2.5") converts numeric text, amount + 1.25 performs float arithmetic, and 2 + amount combines an int with a float. Both calculations produce float results.
When to Use int vs. float
Choose int for counts such as the number of pages, items, or attempts. Choose float for fractional measurements, rates, and ratios when ordinary floating-point precision is adequate. Consider the precision requirements before using float for scientific or financial calculations; floating-point is not a general promise of exact decimal arithmetic.
A simple-interest calculation can combine a principal amount, a percentage rate, and a time period. If the rate is represented as a float, the result is commonly a float as well. This illustrates mixed arithmetic; financial software may need decimal arithmetic and explicit rules for rounding and currency.
The practical rule is to choose the type that matches the meaning of the value, then check whether the calculation’s precision requirements fit that type. Convert input at the point where the program begins treating text as a number, and inspect types when a result behaves differently than expected.