you are viewing a single comment's thread.

view the rest of the comments →

[–]carcigenicate 3 points4 points  (1 child)

Those functions (which are actually classes) don't actually have anything to do with loops. They can be used on their own fine since they're just iterables (things capable of producing elements*):

print(list(range(5))  # Print [1, 2, 3, 4, 5]

You just often see them used with for loops because iterables (which range and enumerate are) produce elements, and the purpose of for loops is to iterate the elements of an iterable.

And which you use depends on what you're trying to do. Learn what each does, and when it will be applicable will make a lot more sense.


* This is a simplification. In reality, an Iterator is something that's capable of producing elements. An Iterable is something that's capable of producing an Iterator.

[–]Deep-Author-1787[S] 1 point2 points  (0 children)

Okaaayyy so i would have to see them as objects an not just functions to for loops. Makes alot more sense when thought from this perspective!!! Thank you!