73.7k views
3 votes
Write a Python function fun3(e) to update a list of numbers e such that the first and last elements have been exchanged. The function needs to also return multiplication of elements of e. You should assume that the list e has at least 2 elements.

Example:

>>>lst = [4, 1, 2, -1]

>>>fun3(list)

-8

1 Answer

3 votes

Answer:

Here is code in Python:

#function to swap first and last element of the list

#and calculate product of all elements of list

def fun3(e):

product = 1

temp = e[0]

e[0] = e[-1]

e[-1] = temp

for a in e:

product *= a

return product

#create a list

inp_lst = [4, 1, 2, -1]

#call the fun3() with parameter inp_lst

print("product of all elements of list is: ",fun3(inp_lst))

#print the list after swapping

print("list after swapping first and last element: ",inp_lst)

Step-by-step explanation:

Create a list "inp_lst" and initialize it with [4,1,2,-1]. In the function fun3() pass a list parameter "e". create a variable "product" to calculate the product of all elements.Also create a temporary variable to swap the first and last element of the list.

Output:

product of all elements of list is: -8

list after swaping first and last element: [-1, 1, 2, 4]

User Benck
by
5.7k points