Python Lists and Dictionaries


I have no idea how to write this function. It says that my syntax is invalid. I have yet to figure out how to fit the square_list into the function.

I expected it to show the correct square root of each of the numbers in order.

Replace this line with your code. 
start_list = [5, 3, 1, 2, 4]
square_list = []

# Your code here!
for number in start_list:
    start_list ** 2

print square_list


Look what instruction says.

Write a for-loop that iterates over start_list and .append()s each number squared (x ** 2) to square_list.

It means we have to append items of start_list after we square them individually.

what append means?
Appending means to add an item at the end of the list.
How to do it?
On list class we have a method for it append(item)
How it works?
suppose this..

x = [1,2,3,4,5]

# now we want to add an item says "a"
we do this..

x.append("a") # notice the dot notation

print x # it will print [1, 2, 3, 4, 5, "a"]

Point to note that here you have to square the items of given list then append it to square_list, to square either use exponentiation operator ( **)or can multiply the item with itself ( as item is a number)

I think you should revise for loop traversal over list too.
check the the 10.3 read


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