![]() |
VOOZH | about |
Our goal is to find sum and average of List in Python. The simplest way to do is by using a built-in method sum() and len(). For example, list of numbers is, [10, 20, 30, 40, 50] the sum is the total of all these numbers and the average is the sum divided by the number of elements in the list.
Python provides convenient built-in functions like sum() and len() to quickly calculate the sum and length of list respectively.
Sum of the list: 150 Average of the list: 30.0
Explanation:
If we'd like to manually calculate the sum and average without using built-in functions then we can find this by looping through the list.
Sum of the list: 150 Average of the list: 30.0
Explanation: We iterate over the list 'a' and calculate their sum and length. And to find the average divide total calculated sum by length of list.
Related Articles: