What is the purpose of `index +=1`?

In the example given for While Loops—

dog_breeds = [‘bulldog’, ‘dalmation’, ‘shihtzu’, ‘poodle’, ‘collie’]

index = 0
while index < len(dog_breeds):
print(dog_breeds[index])
index += 1

What is the purpose of—

index += 1

We need to be sure that i is constantly changing (increasing) so that the test condition eventually fails, otherwise the loop will run indefinitely (infinite loop). We wouldn’t want that.

9 Likes

dog_breeds = [‘bulldog’, ‘dalmation’, ‘shihtzu’, ‘poodle’, ‘collie’]
for dog_breeds
i got an error is If i used a single ’ quote.

it work with " double quote.

Sorry I still dont get this can you elaborate a bit further please.

Every loop needs to stop at some point, for this example it is going to happen when index exceeds.

index =+ 1 means, index = index + 1.

If we want to reach that point we need to bring the ‘index’ value to that level by adding 1 in every iteration by index =+ 1.

2 Likes

so we can write it both ways? i+=1 and i=+1

The one on the left, only. The assignment operator (=) must be last, always.

3 Likes

Even when this is available on other programming languages like C, C++. Python only allows the i += 1

1 Like

You can also use the syntax i=i+1

This code seems to work fine:

while len(students_in_poetry) < 6:
name = all_students.pop()
students_in_poetry.append(name)

Was I supposed to count index in this exercise?
ps. first time posting - couldn’t figure out how to make indented text in the post… sorry…

No, since we are tracking the length of the students_in_poetry list.

I guess i++ should also work.

Not in Python. We don’t have those unary operators at our disposal.

1 Like

In OP’s code the index makes sense. We’re increasing the index so that the loops stops once the index is the length of the dog_breeds list.

What tripped me up was that this index incrementing technique is used in the example, but we don’t necessarily need it in the exercise. In the exercise all we need is:

while len(students_in_poetry) < 6:

This would have been a lot easier to digest as 2 separate exercises. One using the length of the target list, and the other by incrementing a new index.

Learn to leave lessons behind. Once we learn that 6 plus 7 is 13, we know that 6 plus 8 is not. It’s up to us to reason that 8 plus 5 will be, though is it is reasoning, not dumb luck that will find this conclusion.

If a loop is designed to iterate an operation across a list, index by index starting from 0, why do we need to include notation that tells it to increase the index? I guess I figured the (index += 1) was implied by the loop itself. When would I need to vs. not need to specify this condition in my code?

Python iterable objects have a method for iterating them. A range is an iterable object. The method is a property of that object and works in the background. We don’t need to increment the index the way we do in JavaScript, etc.

>>> a = range(10)
>>> dir(a)
['__bool__', '__class__', '__contains__', '__delattr__',
 '__dir__', '__doc__', '__eq__', '__format__', '__ge__',
 '__getattribute__', '__getitem__', '__gt__', '__hash__',
 '__init__', '__init_subclass__', '__iter__', '__le__', 
'__len__', '__lt__', '__ne__', '__new__', '__reduce__',
 '__reduce_ex__', '__repr__', '__reversed__', '__setattr__',
 '__sizeof__', '__str__', '__subclasshook__', 'count',
 'index', 'start', 'step', 'stop']
>>> 

Note the dunder method, above… __iter__. That’s the method that is running in the background. for calls that method on the range.

So everytime it loops index is increased by 1?

Correct.

The code inside a loop will run each time the loop iterates, unless you have a break, continue, or return statement; conditional statements; etc.

Thank you, my brain tells me to do this: 1 + index = index… but that doesn’t seem right either haha