In this article you will learn a python program to add all the items present in the list. Iterate all the items present in the list and keep adding the items to a temporary variable
The summation should be done using the python function, the function should return a temporary variable as the final sum.
Write A Python Function To Sum All The Numbers In A List
This program is divided into three sections
- First define a function for the getting the summation of police staff all the integers
- Second is used to enter list it to the program
- Third is calling the function to find the sum of a list of integers and printing it
Define a function to standard for some to to add all the items present in the list. Iterate all the numbers present in a list and add it to a variable total and using the return statement return the total variable.
def list_sum(items):
total = 0
for item in items:
total += item
return total
In the main program take a variable length and ask the user to enter the length and what it into an integer and then using the for loop and the input function ask user to enter list of numbers
length = int(input("Enter List Length : "))
numbers = []
for _ in range(length):
numbers.append(int(input("Enter List Item (Integer) : ")))
Now take a variable as result and call the function, start the sum of all the list of numbers into a result variable and using the print function call print the sum of all the integers.
result = list_sum(numbers)
print("Sum of all the integers is :", result)
Python Working Code for Integer Numbers
=====================================
def list_sum(items):
total = 0
for item in items:
total += item
return total
length = int(input("Enter List Length : "))
numbers = []
for _ in range(length):
numbers.append(int(input("Enter List Item (Integer) : ")))
result = list_sum(numbers)
print("Sum of all the integers is :", result)
OUTPUT
===============
Enter List Length : 7
Enter List Item (Integer) : 1
Enter List Item (Integer) : 2
Enter List Item (Integer) : 3
Enter List Item (Integer) : 4
Enter List Item (Integer) : 5
Enter List Item (Integer) : 5
Enter List Item (Integer) : 6
Sum of all the floating point numbers is : 26
Python Code for Floating Point Numbers
===============================
def list_sum(items):
total = 0
for item in items:
total += item
return total
length = int(input("Enter List Length : "))
numbers = []
for _ in range(length):
numbers.append(float(input("Enter List Item (Floating Point Number) : ")))
result = list_sum(numbers)
print("Sum of all the floating point numbers is :", result)
OUTPUT
===============
Enter List Length : 5
Enter List Item (Floating Point Number) : 12.3
Enter List Item (Floating Point Number) : 34.5
Enter List Item (Floating Point Number) : 23.09
Enter List Item (Floating Point Number) : 31.99
Enter List Item (Floating Point Number) : 8.064
Sum of all the floating point numbers is : 109.94
Conclusion
=================
Execute the program by providing the random input integers as input to the Python program and note down result.
Comment down below if you have any suggestion on above program
0 Comments