Section 4.12 π©βπ» Keeping Track of Your Iterator Variable and Your Iterable
When students first begin using for loops, they sometimes have difficulty understanding the difference between the iterator variable (the loop variable) and the iterable.
The iterable is the object that you will be parsing through in a for loop, typically a String, list, or other type of sequence. Generally, this object does not change while the for loop is being executed.
The iterator (loop) variable is the variable which stores an element of the iterable (such as an item in the list or a single character from a String) when the for loop is being executed. Each time the loop iterates, the value of the iterator variable will change to a different element of the iterable.
Checkpoint 4.12.1.
What is the type of your iterable?
n = ["word", "phrase", 8, "beam"]
for item in n:
print(item)
string
Incorrect, that is not the type of the iterable.
list
Yes, the iterable is n, and it is a list.
iterable
Incorrect, that is not the type of the iterable.
error, unable to iterate over the object.
Incorrect, Python can iterate over this type.
Checkpoint 4.12.2.
What is the type of your iterable?
t = "couch"
for z in t:
print(z)
string
Yes, the iterable in this example is a string
list
Incorrect, that is not the type of the iterable.
iterable
Incorrect, that is not the type of the iterable.
error, unable to iterate over the object.
Incorrect, Python can iterate over this type.
Checkpoint 4.12.3.
What is the type of your iterable?
y = 18
for z in y:
print(z)
string
Incorrect, there are no strings present in the code.
list
Incorrect, there are no lists present in the code.
iterable
Incorrect, there are no iterable objects in the code.
error, unable to iterate over the object.
Yes, Python is unable to iterate over integers and floats.
Checkpoint 4.12.4.
What is the type of your iterable?
t = ["couch", "chair", "washer", "dryer", "table"]
for z in t:
print(z)
string
Incorrect, the iterable is not a string.
list
Yes, the iterable is t, and it is a list.
iterable
Incorrect, that is not the best answer for this problem.
error, unable to iterate over the object.
Incorrect, Python can iterate over this type.
Checkpoint 4.12.5.
What is the type of your iterable?
t = "couch"
for z in t:
print(z)
string
Correct! The iterable is a string.
list
Incorrect, there is no list in the code.
iterable
Incorrect, that is not the best answer for this problem.
error, unable to iterate over the object.
Incorrect, Python can iterate over this type.
Checkpoint 4.12.6.
Whatβs the type of your iterator variable?
t = ["couch", "chair", "washer", "dryer", "table"]
for z in t:
print(z)
string
Correct! Every item in the iterator variable will be a string.
list
Incorrect, that is not the type of the iterator variable.
integer
Incorrect, that is not the type of the iterator variable.
error, unable to iterate and initialize the iterator variable
Incorrect, the for loop is iterating over an iterable object.
Checkpoint 4.12.7.
Whatβs the type of your iterator variable in the first iteration?
t = [9, "setter", 3, "wing spiker", 10, "middle blocker"]
for z in t:
print(z)
string
Incorrect, think about what the for loop will look at first.
list
Incorrect, that is the type of the iterable, not the iterator variable.
integer
Yes, the first item in t is an integer.
error, unable to iterate and initialize the iterator variable
Incorrect, the for loop is iterating over an iterable object.
Checkpoint 4.12.8.
Whatβs the type of your iterator variable in the second iteration?
t = [9, "setter", 3, "wing spiker", 10, "middle blocker"]
for z in t:
print(z)
string
Yes, the second item in t is a string.
list
Incorrect, that is the type of the iterable, not the iterator variable.
integer
Incorrect, think about what the for loop will look at during the second iteration.
error, unable to iterate and initialize the iterator variable
Incorrect, the for loop is iterating over an iterable object.
Checkpoint 4.12.9.
Whatβs the type of your iterator variable in the final iteration?
red = "colors"
for blue in red:
print(blue)
string
Yes, the last value stored in the iterator variable is a string.
list
Incorrect, there is no list in the code.
integer
Incorrect, there is no integer in the code.
error, unable to iterate and initialize the iterator variable
Incorrect, the for loop is iterating over an iterable object.
As you go through the codelens window, you will be asked a set of questions.
You have attempted
of
activities on this page.