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.

11 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.

1 Like

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.

3 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.

4 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.

2 Likes

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

You’re correct in that this is incorrect. When assigning variables values, the expression on the right side of the assignment operator (=) is evaluated, then assigned to the variable on the left. Here, you are trying to assign 1 + index the value of index. 1 + index is not a valid variable name and will throw an error. You’re nearly there though!

You see what they mean by how weird this appears on first glance?

a = a + 1

The reverse can never be true.

a + 1 = a

The complexity of the mind is something we must always be wary of. IT sees these anomalies long before we do.