How to Solve TypeError: ‘float’ object is not iterable
Floats and iterables are distinct objects In Python. A float is any decimal point number, and an iterable is an object capable of returning elements one at a time, for example, a list. A float is a single value and does not contain other values. If you try to iterate over a float, you will raise the error “TypeError: ‘float’ object is not iterable“.
To solve this error, ensure you use the range() method, for example,
for number in range(floating_point_number)
to iterate over a range of numbers up to the specified floating_point_number .
This tutorial will go through the error in detail. We will go through two example scenarios and learn how to solve them.
Table of contents
TypeError: ‘float’ object not iterable
What is a TypeError?
A TypeError occurs when we try to perform an operation on the wrong type of object. For example, if we try to calculate the square root of a list instead of an integer or a floating-point number, then a TypeError will be generated by the Python interpreter.
Difference Between a Float and an Iterable
Iterables are containers that can store multiple values and return them one by one. Iterables can store any number of values, and the values can either be the same type or different types. You can go to the next item in an iterable object using the next() method.
A floating-point number is any number with a decimal point. You can define a floating-point number in Python by defining a variable and assigning a decimal point number to it.
Floating-point numbers do not store multiple values like a list or a dictionary. If you try to iterate over a float , you will raise the error “TypeError: ‘float’ object is not iterable” because float does not support iteration.
You will get a similar error if you try to iterate over an integer or a NoneType object.
Example #1: Iterate Over a Floating Point Number
Let’s look at an example where we initialize a floating-point number and iterate over it.
Let’s see what happens when we run the code:
We raise the error because Python does not support iteration on a floating-point number.
Solution #1: Convert float to string Using the str() Method
The first solution involves converting the float_num object to a string using the str() method and iterating over every digit. We can do iteration over a string because the string type is iterable. However, the for loop will return each character of the float_num string.
Solution #2: Iterate Using the range() Method
To print the range of numbers, we can use the int() method to convert the number to an integer and then use the integer as input to the range() method. The range() method only accepts integer numbers, in which case we have to convert any floating-point number that we want to iterate over to an integer. The range() method returns an iterable object which we can iterate over using a for loop.
Let’s see what happens when we run the revised code:
Example #2: Determine if a Number is Prime
Let’s look at another example where we write a program to check if a number entered by the user is prime or not. Prime numbers are only divisible by themselves, and 1. To start, we will ask the user to insert the number to determine whether it is prime or not.
Then we define a function that determines whether the number is prime by using the modulo operator % . If the remained of x % y equals zero, then y is a factor of x. Prime numbers have two factors, one and itself.
Let’s run the code to see what happens:
The Python interpreter throws the TypeError because a floating-point number is not a suitable type to iterate over. A for loop requires an iterable object to loop over.
Solution
To solve this problem, we need to convert the input number to an integer using int() and pass the integer the range() method instead of the float. Let’s look at the revised code:
Let’s run the code to see what happens:
Our code successfully prints the result that 17.0 is a prime number.
Summary
Congratulations on reading to the end of this tutorial. The error “TypeError: ‘float’ object is not iterable” occurs when you try to iterate over a floating-point number as if it were an iterable object like a list.
You must use the range() method to iterate over a collection of numbers. However, you must convert the float to an integer beforehand passing it to the range() method. If you want to iterate over the digits of the float, you can convert the float to a string and use the range() function.
To learn more about Python for data science and machine learning, go to the online courses pages on Python for the best courses online!
TypeError: ‘float’ object is not iterable
In this article, we will learn about the error “TypeError: ‘float’ object is not iterable”. This error occurs when we try to iterate through a float value which we know is not iterable.
Let us understand it more with the help of an example.
Example 1:
Explanation:
In the above example, we are trying to iterate through a ‘for loop’ using a float value. But the float is not iterable. Float objects are not iterable because of the absence of the __iter__ method. Which we have discussed about in the below example 2.
Thus the error “TypeError: float object is not iterable” occurs.
Example 2:
Explanation:
In the above example, we are trying to print the list elements using the ‘for loop’. since the list is iterable, thus we can use the for loop for iteration.
To know whether an object is iterable or not we can use the dir() method to check for the magic method __iter__. If this magic method is present in the properties of specified objects then that item is said to be iterable
To check, do: dir(list) or dir(3.4)
__iter__magicmethod is present.
__iter__ magic method is absent.
Conclusion
The presence of the magic method __iter__ is what makes an object iterable. From the above article, we can conclude that the __iter__ method is absent in the float object. Whereas it is present in the list object. Thus float is not an iterable object and a list is an iterable object.
Python TypeError: ‘float’ object not iterable Solution
![]()
Python can only iterate over an iterable object. This is a type of object that can return its members one at a time. If you try to iterate over a non-iterable object, like a floating-point number, you see an error that says “TypeError: ‘float’ object not iterable”.
In this guide, we talk about what this error means and why you may encounter it. We walk through an example to help you understand how to resolve this error in your code.

Find Your Bootcamp Match
- Career Karma matches you with top tech bootcamps
- Access exclusive scholarships and prep courses
Select your interest
First name
Last name
Phone number
By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.
TypeError: ‘float’ object not iterable
Iterable objects include list, strings, tuples, and dictionaries. When you run a for loop on these data types, each value in the object is returned one by one.
Numbers, such as integers and floating points, are not iterable. There are no members in an integer or a floating-point that can be returned in a loop.
The result of the “TypeError: ‘float’ object not iterable” error is usually trying to use a number to tell a for loop how many times to execute instead of using a range() statement.
An Example Scenario
Here, we write a program that calculates the factors of a number. To start, we ask the user to insert a number whose factors they want to calculate:
The input() method returns a string. We cannot perform mathematical operations on a string. We’ve used the float() method to convert the value returned by input() to a floating-point number so that we can use it in a math operation.
Next, we write a for loop that calculates the factors of our number:
This loop should go through every number until it reaches “factor”.
This loop uses the modulo operator to check whether there is a remainder left after dividing the factor by “n”, which is the number in an iteration of our loop. If there is a remainder, “n” is not a factor. If there is a remainder, our “if” statement executes and prints a message to the console.
Run our code and see what happens:
Our code returns a TypeError.
The Solution
In our example above, we’ve tried to iterate over “factor”, which is a float. We cannot iterate over a floating-point number in a for loop. Instead, we should iterate over a range of numbers between 1 and the number whose factor we want to calculate.
To fix our code, we need to use a range() statement. The range() statement generates a list of numbers in a specified range upon which a for loop can iterate. Let’s revise our for loop to use a range() statement:
Our range() statement generates a list of numbers between one and the value of “factor” plus one.
We have added 1 to the upper end of our range so that our “factor” number is considered a factor. Our for loop can then iterate over this list. We’ve converted “factor” to an integer in our code because the range() statement does not accept floating point numbers.
Run our code again and see what happens:
Our code successfully returns a list of all the factors of the number we inserted into our program.
Conclusion
The Python “TypeError: ‘float’ object not iterable” error is caused when you try to iterate over a floating point number as if it were an iterable object, like a list or a dictionary.
To solve this error, use a range() statement if you want to iterate over a number. A range statement generates a list of numbers in a given range upon which a for loop can iterate.
Now you’re ready to fix this common Python error in your code!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.
Python TypeError: 'float' object is not iterable Solution

In Python, we have some iterable objects such as a string, list, tuple, dictionary, and set. The one property of these iterable objects is we can perform for loop over them and access their individual elements one by one. We can use the for loop and iterate over these iterable objects. There are many other functions, such as map, range, and filter, which also return iterable objects. But if we try to perform an iteration on a floating-point number, we will receive the error TypeError: ‘float’ object is not iterable .
This Python tutorial will discuss this error in detail and see why it occurs and how to debug it. We will also walk through a common example of when many python developers encounter this error. So let’s get started with the Error Statement.
Python Error: TypeError: ‘float’ object is not iterable
The Error statement TypeError: ‘float’ object is not iterable has two parts Exception Type and Error message.
- TypeError (Exception Type)
- ‘float’ object is not iterable (Error Message)
1. TypeError
TypeError is a standard Python error. It is raised when we try to perform an invalid or unsupported operation on a Python object.
2. ‘float’ object is not iterable
This is the error message telling us that we are performing an iteration operation on a floating-point object, and Python does not support iteration on a floating-point number.
Example
Output
Break the code
In this example, we are getting this error because we are trying to loop over the float_num variable, which is a floating-point number. And Python for loop can not iterate over a float object, so the interpreter threw the error.
Solution
There can be two case scenarios with the above example, we can either want to iterate over the digits of the float number, or we want to create a range of numbers from 0 up to the float number float_num . If we want to iterate over every digit of the float_num we can convert the object into a string using the str function and iterate over every digit.
Solution 1 (Iterate over every digit)
Output
If we want to print the range of numbers from o to float_num we first need to convert the float num object to an integer using int() function then use that int number into the range() function so the for loop can iterate over the iterable object returned by the range() function.
Solution 2 (Iterate over the range from 0 to float_num )
Output
Common Example Scenario
Many Python learners encounter this error when they directly use the float number with the for loop and forget to use the range() function. However, the range() the function also does not work with floating-point numbers because it only accepts integer numbers as argument values. Still, many new python learners commit the mistake of using the floating-point number with for loop and encounter the TypeError: ‘float’ object is not iterable Error.
Example
Let’s create a Python program that tells if the entered number is a Prime or not.
Output
Break the code
In the above example, we are getting this error because in line 4, we are performing the for loop on the number object, which is the number entered by the user and converted into float using the float() function.
Solution
To solve the above problem, we need to take care of three things.
- First, convert the user input into int before using it in the for a loop.
- Second, use the range function instead of the integer or float when dealing with for loop.
- Third, we need to start the range function from 2 to user entered number . Because inside the for loop, we are performing modulo operation, and behind the scene, modulo operator use division operation, and when it tries do divide any number with 0, it returns the ZeroDivision Error.
Solution
Output
Wrapping Up!
Now let’s wrap up our article “TypeError: ‘float’ object not iterable” Solution. In this article, we discussed what this error is in Python and how to solve it. This error occurs when we perform the iteration operation or for loop on a Python floating-point number. This is a TypeError exception because for loop operation is not supported by floating-point numbers. Mostly this error is raised when the user forgets to put the range function and directly apply the for loop on a floating number.
If you are still getting this error in your program, you can share your code in the comment section. We will try to help you in debugging.