In this python function program, we will find the sum of 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 total
- 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 0.
- Use a for loop to iterate over the values in the list.
- After each iteration add the values to the variable.
- Print the output.
- Create a list and pass that list to the function while calling the function.
def total(list1):
t = 0
for val in list1:
t += val
print("Sum of given list: ",t)
l = [6,9,4,5,3]
total(l)
Output :
Sum of given list: 27