In Snap!, we have two different for loops. One of them iterates over a range of numbers, and the other one (for each) iterates over a list.
|
|
In Python, there is only one for loop and it behaves more like for each in Snap!. Python's for loop is more powerful than the Snap! equivalent, because it can iterate over many different types of objects, including lists, strings, ranges and more.
We use the term iterable to refer to any object that we can iterate over using the for keyword.
For example, we can iterate over a range:
|
|
|
|
And we can iterate over a string:
>>> chant = "Go Bears!"
>>> for letter in chant:
... print(letter)
G
o
B
e
a
r
s
!