Hello everyone, am back to discuss about a new python program. Here we learn how to sum all the elements in a list quite easily. We use a predefined function called sum() and apply it to the list, the functions returns the sum of all the elements in a list.
Sum of elements in a list ? programminginpython.com
Task :
To find the sum of all the elements in a list.
Approach :
- Read input number asking for length of the list using input() or raw_input().
- Initialise an empty list lst = .
- Read each number using a for loop.
- In the for loop append each number to the list.
- Now we use predefined function sum() to find the sum of all the elements in a list.
- Print the result.
Program :
lst = num = int(input(‘How many numbers: ‘))for n in range(num): numbers = int(input(‘Enter number ‘)) lst.append(numbers)print(“Sum of elements in given list is :”, sum(lst))
Output :
Sum of elements in a list ? programminginpython.com
Post: http://programminginpython.com/python-program-calculate-sum-elements-list/
Python Programming – Sum of all the elements in a list
Hello everyone, am back to discuss about a new python program. Here we learn how to sum all the elements in a list?
programminginpython.com
GitHub: https://github.com/avinashn/programminginpython.com/blob/master/sum_elements_list.py
avinashn/programminginpython.com
This repo consists code of all the programs discussed at programminginpython.com website
github.com
Video: https://www.youtube.com/watch?v=6PWAb9Ps6Oo