Floats are one of the most common data formats in Python. The name “float” is short for “floating point number” and we use this data format to represent real numbers with both an integer and fractional component (typically by using a decimal).
To make that definition less formal (and nerdy), a float is a data format that represents a number involving a decimal point. 7.26
is a float. 8
is not.
Floats are critical in most mathematical programs as they allow calculations to be far more precise than when we use integers. For instance, imagine calculating the area of a circle using only integers. Your high school geometry teacher would probably groan seeing you reduce pi to 3
, and your estimate of the circle’s area would be…very wrong.
Fortunately, Python has several ways to create floats.
How Do You Make and Use Floats in Python?
- Directly assign a float to a variable.
- Calculate a float through other variables.
- Convert an integer to a float with float().
- Convert a string to a float with float().
1. Directly Assign a Float to a Variable
You can create a new variable as a float any time you please, all you have to do is type it into your program. For instance, the following example creates a float:
this_is_a_float = 17.8954
2. Calculate a Float Through Other Variables
Similarly, you can assign a float value to a variable via calculation from other variables. For example, you can add two values together. Any time the result of the calculation is a float, Python will automatically define your new variable as a float. Take the following code:
this_is_not_a_float = 2
this_is_a_float = 17.8954
this_is_also_a_float = this_is_not_a_float + this_is_a_float
this_is_also_a_float
now returns 19.8954
, which is a float.
Convert an Integer to a Float
Python also has a built in float()
function that you can use to create floats. One common use of this function is when you’re trying to convert integers to floats. For instance:
this_is_a_float = float(3)
Now the variable this_is_a_float
returns 3.0
, which is the float representation of the integer 3
.
Convert a String to a Float
The same function also works on strings. This commonly occurs when you’re reading values from certain types of data storage or out of filenames (something I do frequently when performing automated data analysis).
For example, consider the following code:
this_is_a_string = “18”
this_is_a_float = float(this_is_a_string)
this_is_a_float
now returns —you guessed it — 18.0
, the float representation of 18
.
A Special Note on NaNs
Sometimes things simply don’t go right when working with data. For instance, it’s quite common that a large data set is missing some samples. In these cases, Python will represent the missing values as nan
. The name is an acronym for “not a number.” In Python, nan
is a specific kind of float value. It cannot be converted to a different value or used in calculations, but it’s worth being aware that it’s technically a float.
Correspondingly, you can actually make a nan
with the float()
function. You do it by passing a string as we did above. For example, the following code:
this_is_a_float = float(“NaN”)
And there you have it! Now you know what floats are, why you would use them and how to create them. Just be careful to avoid accidentally creating NaNs!