More with "for"


#1



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


my code works, but I do not understand the sort function.


Why do you not put anything in the parentheses of sort? I thought it should be" sort(square_list) similar how you would use the len function... I do not understand the concept of when there is open parentheses or when you put something inside the parentheses etc.


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


#2

if the function has a parameter, you need to supply an argument. sort() doesn't have a parameter, so need for an argument between parentheses.

don't get confused with sorted(square_list), the sorted() function does need an argument.

If a function requires an argument depends how the built in function are coded/designed


#3

Easiest way to understand it for me was to realise that certain functions use the parameter as the list i.e str(variable_name) and len(list_name) but there are functions which have a different format such as variable_name.lower() or variable_name.upper() and the same goes for list_name.append(), list_name.remove() or list_name.sort() in your case. I hope this answers your question.


#4

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