How to Use Float in Python (With Sample Code!)

In Python, floats are a common data type that lets users work with decimal numbers, covering a wider range of values than integers. Check out this quick tutorial on how to make and use floats in Python.

Written by Peter Grant
how-to-use-make-float-in-python
Image: Shutterstock / Built In
Brand Studio Logo
UPDATED BY
Matthew Urwin | Mar 19, 2025

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 cover a wider range of values than integers, although they may lead to errors in precision. 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, resulting in a less accurate estimate of the circle’s area.

Fortunately, Python has several ways to create floats. 

How Do You Make and Use Floats in Python?

  1. Directly assign a float to a variable.
  2. Calculate a float through other variables.
  3. Convert an integer to a float with float().
  4. Convert a string to a float with float().

More Tutorials on Built InHow to Find Outliers (With Examples)

 

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.

More From Peter Grant on Built In6 Important Things to Know About Python Functions

 

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.

More Expert Advice on Built In4 Python Tools to Simplify Your Life

 

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 = “18this_is_a_float = float(this_is_a_string)

this_is_a_float now returns —you guessed it — 18.0, the float representation of 18.

Float in Python: Python 3 float() Built-In Function Tutorial

 

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 and behaves unlike typical numerical values, 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!  

Frequently Asked Questions

In Python, a float is a data type that enables users to use decimal numbers, although it can still lead to errors in precision. Floats cover a much broader range of values than just using integers.

Yes, you can convert integers into floats in Python using the float() function. For example, float (5) becomes 5.0.

Short for “not a number,” NaN is a specific type of float that represents missing or undefined values in a data set. Users can produce a NaN in Python by using the function float (“Nan”).

Explore Job Matches.