Python is a highly readable, less syntactic, interactive, object-oriented, interpreted, and high-level programming language. Python uses English terms instead of punctuation.
Some of the features of Python are mentioned below -
- It can be used for production-ready applications or rapid prototyping as well.
- It can handle complex computations and big data.
- It uses simpler syntax and can be used to write complex codes in a shorter and simpler way.
- Designed for readability and provides scope. It mainly relies on whitespace, indentation, etc.
In this article, we will discuss how to find the average of numbers present in the list in Python using the Average function in Python.
Python Average
To find the average of given numbers in a list, we use the Python Average function. Average in Python is usually calculated by adding all the numbers in a list and then dividing it by the number of elements in this list.
There are multiple ways to find the average of a List in Python, and two of the main ways are mentioned below:
1) Python Average: Len() and Sum()
2) Python Average: statistics.mean()
1) Python Average: Len() and Sum()
The Sum() and Len() functions are built-in in Python and are used to find the averages.Â
This method of finding the average helps avoid looping through the list and hence is time and effort-saving. This also reduces redundancy and helps maintain DRY code since it helps in finding the average of a list using a single line.
Example 1
numbers = [30, 55, 3, 10, 2]
average = sum(numbers)/len(numbers)
print("Average of list: ", round(average,3))
Output
Average of the list:Â 20.0
Example 2
num_list = [1, 999, 2, 1023, 223, 876, 32]
average = sum(num_list)/len(num_list)
print("Average of list: ", round(average,2))
Output
Average of the list:Â 450.86
Example 3Â
numbers = [3098, 5565, 323, 1120, 2342, 75664]
average = sum(numbers)/len(numbers)
print("Average of list: ", round(average,1))
Output
Average of the list:Â 14685.3
Example 4Â
numbers = [3, 55986365, 564323, 1314320, 72325342, 7534265664]
average = sum(numbers)/len(numbers)
print("Average of list: ", round(average,4))
Output
Average of the list:Â 1277409336.1667
2) Python Average: statistics.mean()
To easily calculate the python average of a list, we can use the statistics module’s mean() function. The following examples demonstrate the same.
Example 1Â
from statistics import mean
num_list = [30, 55, 3, 10, 2]
average = mean(num_list)
print("Average: ", round(average,3))
Output
Average:Â 20
Example 2Â
from statistics import mean
numbers = [1, 999, 2, 1023, 223, 876, 32]
average = mean(numbers)
print("Average: ", round(average,3))
Output
Average:Â 450.857
Example 3Â
from statistics import mean
num_list = [3098, 5565, 323, 1120, 2342, 75664]
average = mean(num_list)
print("Average of list: ", round(average,1))
Output
Average of the list:Â 14685.3
Example 4Â
from statistics import mean
num_list = [3, 55986365, 564323, 1314320, 72325342, 7534265664]
average = mean(num_list)
print("Average of list: ", round(average,4))
Output
Average of the list: 1277409336.1667
Choose The Right Software Development Program
This table compares various courses offered by Simplilearn, based on several key features and details. The table provides an overview of the courses' duration, skills you will learn, additional benefits, among other important factors, to help learners make an informed decision about which course best suits their needs.
Program Name Automation Testing Masters Program Full Stack Developer - MEAN Stack Caltech Coding Bootcamp Geo All All US University Simplilearn Simplilearn Caltech Course Duration 11 Months 11 Months 6 Months Coding Experience Required Basic Knowledge Basic Knowledge Basic Knowledge Skills You Will Learn Java, AWS, API Testing, TDD, etc. HTML, CSS, Express.js, API Testing, etc. Java, JavaScript, Angular, MongoDB, etc. Additional Benefits Structured Guidance
Learn From Experts
Hands-on TrainingBlended Learning Program
Learn 20+ Tools and Skills
Industry Aligned ProjectsCaltech Campus Connect
Career Services
17 CEU CreditsCost $$ $$ $$$$ Explore Program Explore Program Explore Program
Master Python With Simplilearn!
To find the average of the numbers in a list in Python, we have multiple ways. The two main ways are using the Len() and Sum() in-built function and using the mean() function from the statistics module. In order to grasp Python concepts and dive deep into python development, we must consider resources that give us a complete insight into the topics with examples, activities, and exercises.Â
Simplilearn provides such a comprehensive course through its Post Graduate Program in Full Stack Web Development. This course can help you hone the right skills and become job-ready in no time.Â
If you have any questions, feel free to post them in the comments section below. Our team will get back to you at the earliest.