Iterables are things you can loop over.
Iterators are the agent that does the looping over iterables.
Also, just to make things confusing (really it's to make our lives easier as Pytonistas), iterators are also iterables and all iterators are their own iterators.
Generators are just one flavor of iterator: they're the most common type of iterator you'll usually create yourself.
If you want to make a lazy iterable, one that computes values as you loop over it, generators are usually the tool you'll use.
forloops work by Trey Hunner
Hello friendly web visitor! 👋
This page is part of Python Morsels, an online Python skill-building service.
The best way to learn is by doing. In the case of Python that means writing Python code. If you'd like to improve your Python skills every week, try out Python Morsels by entering your email below to create an account.
Python Morsels topics pages are free and the first month's worth of exercises is free as well. You don't need to enter payment details to sign up.
You can find explanations of many other Python topics by signing up below.