The 6 AI Engineering Patterns, come build with Greg live: Starts Jan 6th, 2025
Leverage
Glossary

Float

Python float data type and conversion errors

Python float refers to a data type. A data type is a fancy word for data format, similar to having different formats in excel. Floats specifically refer to numbers with a decimal point.

x = 5.6 # Floats are numbers with decimal points
print (type(x))
>>> <class 'float'>

In this case, round numbers (Ex: 5.0), can float too, all that matters is they have a decimal point. As you’re working with data, differentiating floats vs int (no decimal points) isn’t a problem…until it is.

Usually, you won’t think about specifying floats or ints, but every now and then you’ll get an error that says “can not recognize float.” You’ll know that this is a data-type problem you’ll need to fix.

Python Float Error: “invalid literal for int() with base 10:”

This is a common error you’ll see when trying to convert a string that looks like a float (Ex: ‘9.34’) into an int directly. You’ll need to first convert this to a float (and not a string), then to an int. Like this: int(float(‘9.34’)

Let’s take a look at a python float code sample.

Link to code

On this page