Student Handbook

Student Handbook

Open in ChatGPT
Ask ChatGPT about this page
Open in Claude
Ask Claude about this page

float

Converts a value to a number with a decimal point.

Synopsis

float(value)

Description

Use float when the user may type a decimal, such as a price or a height.

Floats are stored in a fixed number of bits, so most decimals are only close, not exact. That is why 0.1 + 0.2 is not exactly 0.3.

Parameters

Name Meaning
value A string such as "3.5", or a whole number.

Return value

A float.

Example

price = float(input("Price: "))
print(price * 2)
print(float(7))
print(0.1 + 0.2)

It prints:

Price: 3.5
7.0
7.0
0.30000000000000004

Common mistake

n = float(input("Number: "))

Python stops with:

Number: 3,5
ValueError: could not convert string to float: '3,5'

Python uses a dot for the decimal point, never a comma.

See also

int, round, f-strings

float in the Python documentation

Last updated 2 weeks ago