Revisiting Turtle!
Do you remember the program that drew a square in
[
Lesson 6]?
Instead of writing the sequence
4 times, we can use the
for statement.
Analyze the example below:
HOW DO WE READ THIS?
For each element in the list, we will draw a side with the color specified by that element!
Python, essentially, does this: the list has
4 elements, so for each one, it executes the set of
subordinate instructions! At each step, the variable
color holds the current element,
meaning the drawing color that we set as an argument for the
color() method.
Python is super cool!
It gives us the ability to
iterate (
that's the name of the operation) within a data collection -
in our case,
a list that simply holds four colors.
So, let's read it again:
for each element of the data collection, perform the following...
Unlike other programming languages, Python allows for a
for loop that is natural
and close to
everyday speech.
Pay attention to indentation, okay?
The set of commands is subordinate to the
for statement, so each one must be
indented by exactly
4 characters because they all form
3 lines of a
compound statement,
in this case, a sequential (linear) one!
Proceed to the next page.