0
What is different between for and while
2 Answers
+ 3
We use the for loop to iterate through the elements of a "collection".
This is still quite vague, a collection can be a range of numbers, a data structure that contains values (for example list, set, tuple, dict), any custom object which implements the iteration behaviour through magic methods.
It can also be a generator which may even produce infinite values.
In case of the while loop, you need to determine the exit condition manually inside the loop. You don't need an iterable object (collection) to use it, you can just change a variable and check it's value if it meets a condition to continue or not.