FAQ: Learn Python: Loops - While Loops

This community-built FAQ covers the “While Loops” exercise from the lesson “Learn Python: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise While Loops

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

9 posts were split to a new topic: How does .pop() work?

A post was split to a new topic: Any other way to solve the same problem

3 posts were merged into an existing topic: How does .pop() work?

A post was split to a new topic: Why does this work the way it does?

2 posts were split to a new topic: Where is the typo in my code?

2 posts were split to a new topic: Should we print inside of the loop?

2 posts were split to a new topic: What is the purpose of index +=1?

2 posts were merged into an existing topic: How does .pop() work?

2 posts were split to a new topic: Why do I get an index error?

7 posts were merged into an existing topic: How does .pop() work?

3 posts were split to a new topic: Why does the code use less than 6 if it needs to stop at 6?

2 posts were split to a new topic: Why does my list stop at 5?

2 posts were split to a new topic: How can I use random to generate names?

Hi there,
I used list += item to add student in the poetry class, but they broke down as characters rather than original elements of a name. But when I used .append() it worked again. What’s the reason? Thanks a lot!

There is a really good stackoverflow question about it:

https://stackoverflow.com/questions/2347265/why-does-behave-unexpectedly-on-lists

Oh great! Thank you and I’d spend some time to digest that~

yes, will take some time to digest. In any case, better use .append(), given its a list specific method which won’t give this trouble.

1 Like

Aside

A list can be concatenated with another list. It cannot, however, be concatenated with a single value that is not constructed as a list object.

>>> a = [2, 5, 9, 12]
>>> a += 18
Traceback (most recent call last):
  File "<pyshell#93>", line 1, in <module>
    a += 18
TypeError: 'int' object is not iterable
>>> 

But, a sequence is iterable, so,

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

If we study the code in the screen-grab, we see a list being concatenated with a string (the popped object). The primitive cannot be concatenated; however, Python defines a string as iterable, and therefore assigns it as a sequence, hence they are inserted as single character string objects.


Edited: 2020/01/30

We can concatenate a single character string value; it’s a sequence with length 1.

3 Likes

I got it, and thanks again!