For loop append


#1

start_list = [5, 3, 1, 2, 4]
square_list = []
sqrnum=0
for number in start_list:
sqrnum = number ** 2
print sqrnum
square_list = square_list.append(sqrnum)# Your code here!

square_list.sort()

print square_list

How to append sqrnum to the square_list?


#2

Instead of

square_list = square_list.append(sqrnum)

it should be

square_list.append(sqrnum)

#3

start_list = [5, 3, 1, 2, 4]
square_list = []
sqrnum=0
for number in start_list:
sqrnum = number ** 2
print square_list.append(sqrnum)

square_list.sort()
print square_list


#4

You can also use things like,

start_list = [5, 3, 1, 2, 4]
print(sorted((num**2 for num in start_list)))
# OUTPUT: [1, 4, 9, 16, 25]

Using a generator we can do all types of fancy stuff.


#5

Guys
this is what worked for me

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

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

square_list.sort()

print square_list


#7

Hi Guys,
I worked this ,It will definitely work...........

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

Your code here!

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

square_list.sort()
print square_list


#8

Thank you! Helps a lot.


#11

Vmtech: You end up printing 4 extra lines of None by having an unnecessary print statement in the for loop. You should could write: for number in start_list: square_list.append(number**2) ...