To solve TypeRrror: ‘float’ object is not iterable exception in Python, convert the floating-point number into a string. To handle the TypeError, you can also use the try-except statement. The try-except statement built-in exception handler can handle exceptions whenever it is thrown.
TypeError: ‘float’ object is not iterable in Python
The TypeError: ‘float’ object is not iterable raises when we iterate over the floating object. We can iterate through strings, lists, tuples, and dictionaries. Other than these data types, we cannot iterate over them. If we iterate over the floating object, a TypeError exception is thrown.
Example
lst = [1.5, 2.5, 3.5, 4.5, 5.5]
for i in lst:
print(i, end=" ")
Output
1.5 2.5 3.5 4.5 5.5
In this example, we defined a list, and then it is iterated over all the elements using the for loop. So the output for this program will be 1.5, 2.5, 3.5, 4.5, and 5.5.
Moving over all the elements in a list is known as the list traversal or iteration. In this example, we have seen how to traverse the floating-point objects list. Now let us see how we can traverse over a string.
str = "Hello"
for i in str:
print(i + "")
Output
H
e
l
l
o
You can see from the output that we can traverse over strings. In string traversal in every iteration, each letter is printed.
Now let’s slightly modify the program and assign a floating-point number to the variable in the same program.
fp = 2.1
for i in fp:
print(i + "")
Output
TypeError: 'float' object is not iterable
We expect the program to output like 1 . 7 5, but this won’t happen; instead, it throws an error known as the “float” object, which is not iterable.
Floating numbers can not be traversed using for loop in Python. However, we can traverse the numbers to obtain the desired output.
fp = 2.1
try:
for i in fp:
print(i, end=" ")
except TypeError:
print("TypeError has occurred")
Output
TypeError has occurred
Hence to solve this TypeError, we can convert the float number to a string, and then we can iterate over it.
fp = 2.10
temp = str(fp)
for i in temp:
print(i, end=" ")
Output
2 . 1 0
The output is like 2 . 1 0. Hence we can convert the floating object into a string and iterate over the string. However, we cannot use a floating-point number in the range.
for i in range(2.1):
print(i, end=" ")
Output
TypeError: 'float' object cannot be interpreted as an integer
This code throughs a TypeError: ‘float’ object is not iterable. To solve this error, use the int() method.
for i in range(int(5.0)):
print(i, end=" ")
Output
0 1 2 3 4
This code will get executed, and the output will be displayed as 0 1 2 3 4. Hence, we can convert a float number to an integer to use this number in the range() method.
That’s it for this tutorial.
See also
TypeError: list indices must be integers or slices, not str
ZeroDivisionError: division by zero

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has experience with various programming languages and technologies, including PHP, Python, and JavaScript. He is comfortable working in front-end and back-end development.