While Loops: Lists Exercise

Hey there everyone,

I am new to programming, and have been enjoying my time learning Python 3 these days! During one of the exercises about while loops, titled “While Loops: Lists” I came across a concept that I do not understand, and was wondering if someone can clarify this for me :slight_smile:

python_topics = ["variables", "control flow", "loops", "modules", "classes"]

length = len(python_topics)

index = 0

while index < length:

  print("I am learning about " + python_topics[index])

  index += 1

Can someone explain what “index” is doing in this code and explain how in the print portion the “python_topics” and “index” are interacting?

This would be very helpeful!

Thank you all in advance

What happens when you change this part: index += 1 ? What if you changed it to index += 2?

index refers to the particular item in the list. So, what is index = 0 referring to?
Remember, items are indexed in Python starting at 0. Also, what does len() do?
https://docs.python.org/3/library/functions.html#len

2 Likes

Hey there! thank you for replying so quickly, I really appreciate it.

Can I ask another question? On the topic of index, how does the index know which list it is referring to? lets assume our code had were two lists “python_topics” and “python_built_in_functions”, how would the index know which list it had to consider? Im not sure if this is a silly question, but if it makes sense, I would apreciate your thoughts on this!

Thanks in advance

wow…it just clicked…that was a very very very silly question…hahaha I will keep it up as a reminder…Thanks for helping out anyways…!!! apologies…

1 Like

Hey terawhiz50181,

The variable index is set to 0. It represents the indices of the python_topics list: [“variables”, “control flow”, “loops”, “modules”, “classes”]`
So, during the while loop, the print statement:

print("I am learning about " + python_topics[index]

will be evaluated as:

print("I am learning about " + python_topics[0])

The zeroth index is being evaluated from the python_topics list (i.e. “variables”), printing: "I am learning about variables."
Once that print statement has been evaluated, the variable index is incremented by 1:

index += 1 

The while loop continues because the condition hasn’t been met to end it (i.e. while index < length).
So now the print statement gets evaluated again, but this time the variable index is equal to 1 during this iteration. So the expression will be evaluated as:

print("I am learning about " + python_topics[1]) 

Now index 1 of python_topics list is being evaluated (i.e. “control flow”), printing: "I am learning about control flow."

Once that print statement has been evaluated, the variable index is incremented by 1 again:

index += 1 

Now the variable index is equal to 2, and the while loop will continue like this until the condition index < length: is met. In other words, when the variable index is no longer less than 5 the loop will stop.

I hope ths helps!

1 Like

No worries. You’re welcome. Sometimes it takes a few times for new information to click.

Happy coding!

1 Like

Hi terawhiz50181,

To answer your second question. The variable index is a storage location (identified by a memory address). It doesn’t “know” which list to refer to unless you specify it.
For example this code works:

python_topics = ["variables", "control flow", "loops", "modules", "classes"]
second_list = ["eggs", "ham", "spam", "cereal", "toast"]

length = len(python_topics)

index = 0

while index < length:

  print("I am learning about " + python_topics[index])
  print("I am eating " + second_list[index])

  index += 1

Output:

I am learning about variables
I am eating eggs
I am learning about control flow
I am eating ham
I am learning about loops
I am eating spam
I am learning about modules
I am eating cereal
I am learning about classes
I am eating toast

However, this wouldn’t be best coding practice because the variable length is set to the length of the python_topics list (i.e. len(python_topics)). The problem being is if the second_list had a different length! If the second_list was shorter than the primary list, then you will get an IndexError. If it was longer than the primary list then all the element in the second_list wouldn’t get printed. The best option would be to use 2 different index variables (e.g. idx1, idx2), to keep track of its own list.

I hope this helps!

1 Like

@seraph776 Thanks a lot! I really appreciate you taking the time to explain both questions thoroughly, it means a lot. I’m really glad to see that the community is so encouraging and motivating. I can imagine these are dumb questions for you guys who know this stuff but taking the time to explain it like this helps more than you know. Thanks :slight_smile:

This goes out to @lisalisaj too!

Thanks people!

1 Like

There are no dumb questions.
For me, answering questions helps solidify coding concepts. Now, armed w/your knowledge you can help out someone else! :slight_smile:

1 Like

Hey terawhiz50181,

I am glad I could be of assistance! As isalisaj mentioned, there are no dumb questions (except for the one not asked)! Stay motivated and code everyday! :slight_smile:

1 Like