7.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 and the iterable.

The iterable is the object that you will parsing through in a for loop. Generally, this object does not change while the for loop is being executed.

The iterator variable is the variable which stores a portion of the iterable when the for loop is being executed. Each time the loop iterates, the value of the iterator variable will change to a different portion of the iterable.

    iter-7-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.
  • tuple
  • 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.

    iter-7-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.
  • tuple
  • 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.

    iter-7-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.
  • tuple
  • Incorrect, there are no tuples 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.

    iter-7-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
  • Incorrect, there is no list in the code.
  • tuple
  • Yes, the iterable in this situation is a tuple.
  • 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.

    iter-7-5: What is the type of your iterable?

    t = "couch"
    for z in t:
        print(z)
    
  • string
  • list
  • tuple
  • iterable
  • error, unable to iterate over the object.

    iter-7-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.
  • tuple
  • 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.

    iter-7-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.
  • tuple
  • Incorrect, there is no tuple in the code.
  • 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.

    iter-7-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.
  • tuple
  • Incorrect, there is no tuple in the code.
  • 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.

    iter-7-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.
  • tuple
  • Incorrect, there is no tuple 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.

(clensQuestion6_100_10)

(clensQuestion6_100_11)

Next Section - 7.13. Glossary