In this python function program, we will multiply all the numbers in a list.
What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.
Steps to solve the program
- Create a function mul.
- Use the def keyword to define the function.
- Pass a parameter i.e. list to the function.
- Create a variable and assign its value equal to 1.
- Use a for loop to iterate over the values in the list.
- After each iteration multiply the variable by the value.
- Print the output.
- Create a list and pass that list to the function while calling the function.
def mul(list1):
t = 1
for val in list1:
t *= val
print("Product of elements in the given list: ",t)
l = [-8, 6, 1, 9, 2]
mul(l)
Output :
Product of elements in the given list: -864