9. More with from


#1



https://www.codecademy.com/courses/python-beginner-en-pwmb1/1/6?curriculum_id=4f89dab3d788890003000096#


My code is correct I just don't understand one part of it.


I don;t understand why we write square_list = [] on line 2. It's defining square_list as what? A set of numbers? Is that what you have to do before you calculate what square list is? What if it were a string? Would you write square_list = [""]?


start_list = [5, 3, 1, 2, 4]
square_list = []

for number in start_list:
    square_list.append(number ** 2)
    square_list.sort()
print start_list
print square_list


#2

square_list is going to be the new list of squared numbers we will be appending our squared numbers (square_list.append(number ** 2)) to. When we append, we are adding our numbers to the list.

Doing this:

... just means there's an empty string in our list, and it doesn't turn our list to a string... It's still a list nonetheless. Our output is now:

=> [1, 4, 9, 16, 25, '']  #see the quotes at the end of our list?... 
                               #...that's from square_list = [""]

If we wanted our string to be a list, we could use the str() method, but it depends on where you put it. Anyways... this isn't necessary in our lesson, so we won't be needing it.

Also, check out what @appylpye said :slight_smile:.


#3

Hi @hunt9108,

To make your code more efficient, remove the indentation from this statement ...

    square_list.sort()

With its being indented, that statement is part of the for loop, and therefore executes during every iteration of the loop. You only need to sort square_list once, after the loop has completed all its iterations, therefore that line of code should not be part of the loop.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.