Stuck on 9


#1

I dont know what to do.
Heres my code so far

start_list = [5, 3, 1, 2, 4]
square_list = []
for numbers in start_list:
    1thing = numbers ** 2
  2ndsquare = .append(1thing)
  3rdstage = square ** 2
  3rdstage.sort()

print square_list


#2

No idea what you are doing here except the fact you are trying to square a list of numbers.

Easy enough:

start_list = [5, 3, 1, 2, 4]
print(*(num**2 for num in start_list))

Also:

start_list = [5, 3, 1, 2, 4]
square_list = [num**2 for num in start_list]
print(square_list)

OR:

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

Any questions?


#3

ive tried

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

but i got an error saying i have to also .sort() the start_list so i did this
start_list = [5, 3, 1, 2, 4]
square_list = []

# Your code here!
for num in start_list:
    square_list.append(num**2)
    .sort(num)
print(square_list)

and i got an invaild syntax


#4

That should do it for you.


#5

i still get this


#6

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

Your code here!

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

print square_list

Thats what I used, and it seems to work. I'm sure you can combine the .append and the .sort into one line, but I kind of like keeping it spaced so its simplier to read.


#7

I think square_list.sort() isn't valid, maybe outdated. It is the right answer in this exercise but to sort this vector sorted(square_list) is correct to sort the vector - but it's not accepted by the lecture..


#8

sorted() and list.sort() do two different things. sorted() return a new list containing the elements of the old one in order. list.sort() modifies the list it's called on. I'd guess that the reason the sorted(square_list)-solution didn't work probably is because squared_list never was assigned the result. The assignment is specifically to put the result in squared_list.


#9

You will get an error. Try this one:
start_list = [5, 3, 1, 2, 4]
square_list = []
for num in start_list:
square_list.append(num**2)
square_list.sort()
print(square_list)


#10

CORRECT ANSWER

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

this what finally worked.
Try this


#12

how do I?
Add a key to inventory called 'pocket'
Set the value of 'pocket' to be a list consisting of the strings 'seashell', 'strange berry', and 'lint'


#13

nice code you have here...i just added at the end with print square_list.sort() and everything work like a charm..alhamdulillah..thanks again.


#14

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

# Your code here!
#for keyword (variable) in keyword and (data string)
for number in start_list:
#(data string) .append for pushing data into square list and (variable (num) ** 2)
square_list.append(number**2)
#and now using .sort() for sorting data in square_list
square_list.sort()
# print square_list here
print square_list


#15

This worked

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

Your code here!

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


#16

which is the "proper" way? i know they all works but which is the "right one"? ...and if possible, why?


#17

Although the general consensus of Pythonists is there should only be one obvious way to do somethings, the truth is what ever way works for you is the proper way.

Depending on your skill level and the size of application for your code each one shifts in value, if you have something one off then a lambda or the for loop example would be of perfect use.

If you had a huge data set to work on then the generator example would provide the best results because it does not consume memory as it runs and because it gets converted into byte-code when python compiles it making it super fast.

So realistically the problem dictates the solution, if you need a one-off what you are able to quickly write and get done is the best, if you have a huge data set to work with take the extra time to create a generator and a way to utilize it.


#18

...so it does affects performance, i thought that it's just matter of style, company policy or whatever.
Is there a way to measure my codes performance?


#19

Yes, tons of ways.

An easy to implement way is to simply use cProfile

EXAMPLE

import cProfile

def compute():
    return 2**1048

cProfile.run("compute")

This will output some fancy stuff for you telling you how long different things take.


#20

do you even code bro, work it out for yourself


#21

how is it easy enough for you, are a python coder from a long time or you just figured it out from the first time just like that because if i didn't get it the same way you said then i shall drop this language learning and find some thing else to do.