How to Write a Decimal Number in Python with float
A price of 19.99 needs a decimal point; a count of 3 doesn't. Python keeps the two kinds apart: whole numbers are int, numbers with a decimal point are float. This guide writes both in Python 3.15.2, checks their types, and shows the one surprise floats hold.
Syntax
<name> = <digits>.<digits>
| Part | What it means |
|---|---|
<digits>.<digits> | A number with a decimal point, such as 19.99 or 2.0 |
| The decimal point | Makes it a float, even when nothing follows it but a zero |
The Python tutorial says: "The integer numbers (e.g. 2, 4, 20) have type int, the ones with a fractional part (e.g. 5.0, 1.6) have type float."
The Example
A shop line has a price of 19.99, a quantity of 3 and a weight of 2.0 kg. The script prints the type of each and the line's total.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
price = 19.99
quantity = 3
weight = 2.0
print(type(price))
print(type(quantity))
print(type(weight))
print(price * quantity)

Step 2: Run It
uv run python floats.py
<class 'float'>
<class 'int'>
<class 'float'>
59.97

19.99 is a float and 3 is an int. 2.0 is a float too: the decimal point decides, not whether there's anything after it. Multiplying a float by an int gave a float, 59.97. The command works unchanged in PowerShell.
Step 3: See That a Float Is Close, Not Exact
print(0.1 + 0.2)
print(7 / 2)
print(round(0.1 + 0.2, 2))
uv run python float_sum.py
0.30000000000000004
3.5
0.3

0.1 + 0.2printed0.30000000000000004. The floating-point chapter of the tutorial explains why: "the decimal floating-point numbers you enter are only approximated by the binary floating-point numbers actually stored in the machine."7 / 2gave the float3.5:/always gives a float, even for two ints.round(..., 2)rounded the sum to two decimal places for display:0.3.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Comparing floats with ==, as in 0.1 + 0.2 == 0.3 | False, because of the tiny difference Step 3 shows | Compare with a tolerance: math.isclose(0.1 + 0.2, 0.3) gave True |
Writing 2 when you need a float | It's an int; some libraries choose a different type from it | Write 2.0 |
| Storing money as floats in accounting code | Rounding differences add up | Use decimal.Decimal or whole cents |
FAQ
How do I turn text into a float?
float("19.99") gives 19.99.
What does 1e3 mean?
It's 1000.0, a float written in scientific notation: e3 means "times ten to the power 3".
Conclusion
19.99 and 2.0 are float and 3 is int. Floats are close approximations: 0.1 + 0.2 printed 0.30000000000000004, and round() gave 0.3 for display.
Additional Resources
- Numbers in the Python tutorial —
int,floatand the arithmetic operators. - Floating-point arithmetic: issues and limitations — why
0.1 + 0.2isn't exactly0.3. - math.isclose() — comparing floats with a tolerance.
