FAQ: Learn Python: Loops - While Loops

Great explanation!
Thanks a lot!

1 Like

what if i wanted to .pop() randomly…?

Python’s `.pop()` method supports indexing. When no index is given, the last element is popped, but when an index is given, it is that index to be popped.

With knowledge of the length of a list we can generate a random number (an index) that is within range.

``````from random import randrange
``````

We import `randrange` since it generates integers that will match (fall within) the range of our list.

``````names = ['Sasha', 'Eric', 'Maria', 'Jason', 'Alice',
'Mathew', 'Emma', 'Logan', 'Naomi', 'Parker']
n = len(names)
x = randrange(n)
print (names[x])``````

thanks a lot

1 Like

Fine

fine

fine

Sorry, but this doesn’t make sense.

They discussing
`a += b` will call `__iadd__` and mutate `a` ,
whereas `a = a + b` will create a new object and assign it to `a`

This is not explaining why only 1st character assigned to students_in_poetry list.

That isn’t happening in that screenshot.
What are the first letters? What is printed? Doesn’t match.

Consider,

``````>>> a += 18, 25
>>> a
[2, 5, 9, 18, 25]
>>>
``````

The augmented value is a sequence. Likewise below,

``````>>> a = [2, 5, 9, 18, 25]
>>> a += 'Python'
>>> a
[2, 5, 9, 18, 25, 'P', 'y', 't', 'h', 'o', 'n']
>>> a += ['Python']
>>> a
[2, 5, 9, 18, 25, 'P', 'y', 't', 'h', 'o', 'n', 'Python']
>>>
``````
2 Likes

this is great example!
But I’m still struggle to understand.

So for

1. string - it iterates
2. list - treated as atomic entity

I can memorise like this, unless there is a better explanation.

1 Like

list’s `+=` iterates regardless. It doesn’t care in the slightest what the input is.

1 Like

Make sense!!!

``````
>>> a = [2, 5, 9, 18, 25]
>>> a += 'Python'
>>> a
[2, 5, 9, 18, 25, 'P', 'y', 't', 'h', 'o', 'n']
>>> a += ['Python','Java']
>>> a
[2, 5, 9, 18, 25, 'P', 'y', 't', 'h', 'o', 'n', 'Python', 'Java']
>>>
``````

Thanks a lot for your help and support!

Why does this code:

i = 0
while i < 6:
students_in_poetry += all_students.pop()
i += 1

Yield this:
Value for `students_in_poetry` did not match `['Loki', 'Arius', 'Obie', 'Alexa', 'Minerva', 'Dora']` , (was `['L', 'o', 'k', 'i', 'A', 'r', 'i', 'u', 's', 'O', 'b', 'i', 'e', 'A', 'l', 'e', 'x', 'a', 'M', 'i', 'n', 'e', 'r', 'v', 'a', 'D', 'o', 'r', 'a']` ).

It’s the same names that we’re supposed to call for this lesson but each letter is its own string.

`.pop()` grabs an element, in this case a string.

The augmentation assignment operator treats a string object as a sequence if it is concatenating to a list, hence the letters.

You have two options, `.append()` or `[all_students.pop()]`.

Hi,
i’m just take my spare time to learn python and doing the courses codeacademy is offering!
In Chapter 7 - While Loops the excercise reads:

You are adding students to a Poetry class, the size of which is capped at 6. While the length of the `students_in_poetry` list is less than `6` , use `.pop()` to take a student off the `all_students` list and add it to the `students_in_poetry` list.

Lower is my way of solution but it wasn’t working at all because it throws me an IndexError back, saying

Traceback (most recent call last):
File “script.py”, line 6, in
cut_off = all_students.pop()
IndexError: pop from empty list

I can’t figure out why - well, yeah - in comparison with the solution down - i can.
But WHY does it give me an error? isn’t my approach “almost” the same like the solution?

``````all_students = ["Alex", "Briana", "Cheri", "Daniele", "Dora", "Minerva", "Alexa", "Obie", "Arius", "Loki"]
students_in_poetry = []

len_poetry = len(students_in_poetry) #why isn't this working???
while len_poetry < 6:
cut_off = all_students.pop()
students_in_poetry.append(cut_off)

print(students_in_poetry)

"""this is the solution given by codeacademy
while len(students_in_poetry) < 6:
student = all_students.pop()
students_in_poetry.append(student)

print(students_in_poetry)"""
``````

`len_poetry` never changes. The variable contains a value, that value never changes.

you set length once, that is it. You never update length after popping items

1 Like

How come while-loops need += 1 in the loop but for-loops do not?

``````dog_breeds = ['bulldog', 'dalmation', 'shihtzu', 'poodle', 'collie']

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

It seems that in while-loops we have to hardcode the iteration into the loops for it, where with for-loops we do not have to tell the index to increase by one.

python is taking care of this for us. In this case, the for loop would be a better fit.

the while loop would be a much better fit for example when you have to keep prompting the user until you get valid input, given this takes an undetermined amount of iterations.

1 Like

Ok Thanks! In this case we have a list of known length. I think I better understand the generality of while loops now. While loops do no seem like they need a list of objects to operate, where so far as I have seen, for loops do.

``````all_students = ["Alex", "Briana", "Cheri", "Daniele", "Dora", "Minerva", "Alexa", "Obie", "Arius", "Loki"]
students_in_poetry = []

for students in all_students:
while students_in_poetry < 6:
all_students.pop(students_in_poetry)
``````

Traceback (most recent call last):
File “script.py”, line 5, in
while students_in_poetry < 6:
TypeError: ‘<’ not supported between instances of ‘list’ and ‘int’

Did you mean to test the length of the list?