Python Lists of Floats

In this tutorial, I will explain an important topic known as lists of floats in Python. I will explain how to create and manipulate lists of floats in Python with examples.

To add elements to a list of floats in Python, you can use the append() method. This method adds a new float to the end of the list, ensuring that your data structure remains ordered and easily extendable. For example, if you have a list temperatures = [72.5, 68.4, 75.0] and you use temperatures.append(71.6), the list will become [72.5, 68.4, 75.0, 71.6].

What is a List of Floats in Python?

In Python, a list is a collection of items that are ordered and changeable. Lists can contain items of different data types, but when we specifically talk about a “list of floats,” we mean a list where each item is a floating-point number. Floats are numbers with a decimal point, such as 3.14 or 2.718.

Syntax

Here is the syntax to create a list of floats in Python:

float_list = [1.1, 2.2, 3.3, 4.4, 5.5]

In this example, float_list is a list containing five float numbers.

Check out How to Check if a Float Variable is Empty in Python?

Create and Manipulate Python Lists of Floats

Now, let me show you some examples to let you understand how to create and manipulate lists of floats in Python.

Example 1: Create a List of Floats

Here is how to create a list of floats in Python.

temperatures = [72.5, 68.4, 75.0, 70.2, 69.8]
print(temperatures)

In this example, temperatures is a list that holds float values representing temperature readings in Fahrenheit.

I executed the above code, and you can see the exact output in the screenshot below:

python list of floats

Example 2: Add Elements to a List

You can add elements to a list using the append() method. Here is how to add an element to a list of floats in Python.

temperatures = [72.5, 68.4, 75.0, 70.2, 69.8]
temperatures.append(71.6)
print(temperatures)

This will add 71.6 to the end of the temperatures list.

Here is the exact output in the screenshot below:

list to float python

Example 3: Insert Elements at a Specific Position

To insert a float at a specific position into the lists of floats, use the insert() method in Python.

temperatures = [72.5, 68.4, 75.0, 70.2, 69.8]
temperatures.insert(2, 73.3)
print(temperatures)

This will insert 73.3 at the third position (index 2) in the list.

Here is the exact output in the screenshot below:

list of floats python

Example 4: Remove Elements

You can remove elements using the remove() method or the pop() method from the Python lists of floats.

temperatures = [72.5, 68.4, 75.0, 70.2, 69.8]
temperatures.remove(68.4)
print(temperatures)

last_temp = temperatures.pop()
print(last_temp)
print(temperatures)

The remove() method deletes the first occurrence of the specified value, while the pop() method removes and returns the last item in the list.

Here is the exact output in the screenshot below:

python float list

Check out Concatenate String and Float in Python

Useful Methods for Python Lists of Floats

Python provides several built-in methods to work with lists of floats efficiently. Let me share a few useful methods to work with lists of floats in Python.

1. sum()

The sum() function calculates the total of all float numbers in the list.

temperatures = [72.5, 68.4, 75.0, 70.2, 69.8]
total_temperature = sum(temperatures)
print(total_temperature)

2. min() and max()

The min() and max() methods are very useful to work with Python lists of floats. These functions return the smallest and largest float values in the list, respectively.

Here is an example.

temperatures = [72.5, 68.4, 75.0, 70.2, 69.8]
lowest_temp = min(temperatures)
highest_temp = max(temperatures)
print(lowest_temp, highest_temp)

Here is the exact output in the screenshot below:

float list python

3. sorted()

The sorted() function returns a new list containing all float numbers from the original list in ascending order.

Here is an example of using the sorted() method for a Python lists of floats.

temperatures = [72.5, 68.4, 75.0, 70.2, 69.8]
sorted_temperatures = sorted(temperatures)
print(sorted_temperatures)

4. len()

The len() function returns the number of elements in the list. Here is how to get the length of a lists of floats in Python.

temperatures = [72.5, 68.4, 75.0, 70.2, 69.8]
number_of_readings = len(temperatures)
print(number_of_readings)

Conclusion

In this tutorial, I explained how to work with Python lists of floats with some examples. I also explained how to create lists of floats in Python, add elements to them, and remove them. Finally, we saw some useful methods for working with lists of floats in Python.

You may also like the following tutorials:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.