72.1k views
0 votes
Q2 - Square Everything (0.25 points) Write a function called square_all that takes an input called collection that you assume to be a list or tuple of numbers. This function should return a new list, which contains the square of each value in collection. To do so, inside the function, you will define a new list, use a loop to loop through the input list, use an operator to square the current value, and use the append method to add this to your output list. After the loop, return the output list.g

User Ben Stott
by
6.4k points

1 Answer

0 votes

Answer:

See explaination

Step-by-step explanation:

#Define the function square_all() having a list of

#integers. This function will return a list of square

#of all integer included in the given list.

def square_all(num_list):

#Create an empty list to store resultant list.

sq_list = []

#Start a for loop till the length of the given list.

for index in range(0, len(num_list)):

#Multiply the current list value with its value

#and append the resultant value in the list

#sq_list in each iteration.

sq_list.append(num_list[index] * num_list[index])

#Return the final list of squares of all integrs in

#the list num_list.

return sq_list

#Declare and initialize a list of integers.

intList = [2, 4]

#Call the function square_all() and pass the above list

#as argument of the function. Display the returned list.

print(square_all(intList))

User Mariya
by
6.3k points