4Geeks chosen to deliver AI education in the Bahamas alongside Harvard, Oxford, and Columbia.See more

What is float in python?

A float type variable is used to store real numbers with fractions (or floating point numbers). The float() method returns a float number from any received integer, numeric string or even a boolean!
3 min readLast updated

Short answer: In Python, a float is a data type that stores real numbers with a fractional part, such as 1.5, as a 64-bit double-precision value with a maximum of about 1.8 × 10^308. The built-in float() function converts integers, numeric strings, booleans, 'inf' and 'nan' to floats; for example, float(10) returns 10.0.

python
x = 10
print(float(x))
 
#Output -> 10.0

What is a Float in Python?

Float is a type of variable present in all programming languages that it´s used to store real numbers with fractions (or floating point numbers). Based on this, we can differentiate in it's composition an Integer type number separated by a . (dot) from the fractional parts. It´s represented as a 64-bit double-precision value whose maximum is about 1.8 * 10308.

If a float goes beyond that limit, Python represents it as inf (infinity), while converting an integer that is too large raises an OverflowError (see the last example).

python
x = 1.5
# -> 1 integer
# -> .5 fraction
#-> 1.5 float number

Syntax

The float()syntax is quite simple and straight forward.

python
float()

Float() will accept any numeric value (inside it's limit), inf, infinity or NaN and return the floating point value if possible.

Using float() with different types of data

Passing a variable to float()

python
x= 5
print(float(x))
#Output -> 5.0

Passing a integer directly to float()

python
print(float(8))
#Output -> 8.0

Passing a integer with negative value directly to float()

python
print(float(-55))
#Output -> -55.0

Passing a string with a numeric values to float()

python
text = '35'
print(float(text))
#Output -> 35.0

Passing a string with white spaces to float()

python
text = '    35   '
print(float(text))
#Output -> 35.0

Passing a boolean type to float()

python
bol = True
print(float(bol))
#Output -> 1.0

Booleans can have 2 possible values, 1 for True and 0 for False, that´s why if received True float() returns 1.0

Passing 'inf' to float()

python
print(float('inf'))
#Output -> inf

Passing 'nan' to float()

python
print(float('nan'))
#Output -> nan

Passing a string with mixed numeric and non numeric values to float()

python
print(float('4 Geeks Academy'))
#Output -> Traceback (most recent call last):
# File "main.py", line 1, in <module>
# print(float('4 Geeks Academy'))
# ValueError: could not convert string to float: '4 Geeks Academy'

Passing a value outside the limit causing overflow error to float()

python
print(float(10**432))
#Output -> Traceback (most recent call last):
# File "main.py", line 1, in <module>
# print(float(10**432))
# OverflowError: int too large to convert to float

To learn to Code with Python take a look at our Courses in 4Geeks. Hope you enjoyed the reading and keep on the Geek side!

Understanding how float() converts values, and when it raises errors, is part of writing reliable Python code. To keep building these skills step by step, you can learn to code in a program with projects and mentors.