There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
#Your code below:
length = len(python_topics)
index = 0
while index < length:
print("I am learning about " + python_topics[index])
index += 1
Why does the loop output the full list if the length is 4, and once the index reaches 4, then it’s not less than 4, it’s equal to 4? I tried using index <= length, but I got an error that it was out of the index range. Can someone help me understand this better?
In Python, as in many other languages, list indices begin at zero, not 1. When index reaches 5 (the length of the list) it points to an element that does not exist.
my question would revolve around the fact that in this exercise, we created a variable assigned to the value len(list) as a termination condition for the for-loop. I wondered why we had to create this variable as opposed to directly employing len(list), therefore “sparing” the declaration of the length variable itself?
Written in code, why is the following notation preferred:
length=len(list)
for i in length:
<code>
as opposed to
for i in len(list):
<code>
?
I was thinking that it would be computationally more inefficient to compute the length of a list upon every iteration, but wondered if this was true or if there was “something else to it”.
I think it’s more in showing that can more than we should. Logic dictates, but sometimes tossing in a gratuitous variable makes it easier for the reader and we can use less documentation. Let the code speak for itself, as it were.
Could someone help me to understand some things with this exercise please?
Does anyone know why this code isn’t accepted?
length = len(python_topics)
index = 0
while length > index:
for element in python_topics:
print('I am learning about '+element)
index += 1
On the given solution - the list element is printed each iteration using the variable ‘index’ in the square brackets in the loop. I thought the variable named index was declared to serve as the value that increments each loop iteration. How is the index variable connected to the list elements?
Do you know re. the 2nd question - I don’t understand how the term ‘index’ is used in the loop to refer to the index of each element of the list as it iterates through the loop. I thought ‘index’ was a variable declared to be checked in the condition of the loop (against the length of the list) and incremented each iteration. How does the code know when we use our variable ‘index’ that it should also refer to the actual index of the list?
In order to reproduce with while what the for loop is doing, we need to consider the difference between element (a value IN the list) and index, one of list length (by count) indices.
# ---iterable---
for element in python_topics:
print ('I am learning about ' + element)
# -value-
The while loop is not iterating the iterable and its values. We must set up the scenario by which it will output the same lines as the for loop.
index = 0
length = len(python_topics)
We’ll use those variables and subscript of the iterable to access the values at each index.
while index < length:
print ('I am learning about ' + python_topics[index])
index += 1
See how that works?
Bottom line, one form or the the other, but not both at the same time.
Oops. Accidentally hit enter. Just doing a little experimenting with while loops. My question is, when I change the bottom index to 2, (index += 2), why does it only take away “I am learning about modules.” statement as opposed to showing all of them when index += 1?
I wasn’t sure where to ask this but you seem to know what you’re talking about. How does the system know that index (in lists/while loops) is corresponding to the index of a list? Is index a function or a variable? I don’t understand lol, thank you sir!
index in this instance is a variable. What we call it is inconsequential, but makes sense to name it for what it represents. The system doesn’t ‘know’ anything that we don’t tell it, explicitly. It will know an object is a list by examining the data structure, which will have type 'list`. Lists are subscriptable which is denoted by the square brackets.