What Makes Something Iterable

A structure is iterable if you can walk through it one item at a time, from beginning to end.

Lists and arrays are iterable. Strings are iterable, a string is a sequence of characters. Dictionaries and objects are iterable through their keys. Sets are iterable. A single number is not. There's nothing to walk through.

✦ Why this concept comes before loops

A loop doesn't work on 'anything.' It works on iterables. Once you know which structures are walkable and what you get on each step, every loop in the next two modules becomes obvious instead of magical.

EXAMPLE
loading editor…
YOUR TURN
Your turn → Make a string, a list and a dictionary of your own, and print the length of each.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: What You Get on Each Step →
← Back