{"id":8616,"date":"2021-09-30T11:00:56","date_gmt":"2021-09-30T05:30:56","guid":{"rendered":"https:\/\/python-programs.com\/?p=8616"},"modified":"2021-11-22T18:35:30","modified_gmt":"2021-11-22T13:05:30","slug":"python-program-to-calculate-the-average-of-numbers-in-a-given-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-calculate-the-average-of-numbers-in-a-given-list\/","title":{"rendered":"Python Program to Calculate the Average of Numbers in a Given List"},"content":{"rendered":"
Don’t miss the chance of Java programs examples with output pdf free download<\/a> as it is very essential for all beginners to experienced programmers for cracking the interviews.<\/p>\n List in Python :<\/strong><\/p>\n The list data type is one of the most often used data types in Python. The square brackets [ ] easily identify a Python List. Lists are used to store data items, with each item separated by a comma (,). A Python List can include data elements of any data type, including integers and Booleans.<\/p>\n One of the primary reasons that lists are so popular is that they are mutable. Any data item in a List can be replaced by any other data item if it is mutable. This distinguishes Lists from Tuples, which are likewise used to store data elements but are immutable.<\/p>\n Given a list, the task is to Calculate the average of all the numbers in the given list in python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to calculate the average of all numbers in the given list in Python some of them are:<\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation :<\/strong><\/p>\n We used the split function to split the input string separated by spaces, then used the map function to convert all of the strings to floats and placed all of the items in a list.<\/p>\n Related Articles:<\/strong><\/p>\n Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews. List in Python : The list data type is one of the most often used data types in Python. The square brackets [ ] easily identify a …<\/p>\ngiven list = [8, 9, 1, 23, 15, 20, 19, 13, 8, 7, 5, 2, 7, 10, 14, 16]<\/pre>\n
The average value of the given list [8, 9, 1, 23, 15, 20, 19, 13, 8, 7, 5, 2, 7, 10, 14, 16] = 11.0625<\/pre>\n
given list = [ 47 24.5 27 28 11 23 34.8 33 31 29 45 37 39 ]<\/pre>\n
The average value of the given list [47.0, 24.5, 27.0, 28.0, 11.0, 23.0, 34.8, 933.0, 31.0, 29.0, 45.0, 37.0, 39.0] = 100.71538461538461<\/pre>\n
Program to Calculate the Average of Numbers in a Given List in Python<\/h2>\n
\n
Method #1: Counting sum manually ( Static Input separated by spaces)<\/h3>\n
\n
# given list\r\ngiven_list = [8, 9, 1, 23, 15, 20, 19, 13, 8, 7, 5, 2, 7, 10, 14, 16]\r\n# Take a variable say sumOfList which stores the\r\n# sum of all list elements and initialize it to 0.\r\nsumOfList = 0\r\n# Traverse the given list using for loop\r\nfor eleme in given_list:\r\n # Add the iterator value to the sumOfList after each iteration.\r\n sumOfList = sumOfList+eleme\r\n# calculating the length of given list using len() function\r\nlength = len(given_list)\r\n# Calculate the average value of the given list by Dividing sumOfList with length\r\nlistAvg = sumOfList\/length\r\n# printng the the average value of the given list\r\nprint(\"The average value of the given list\", given_list, \"=\", listAvg)\r\n<\/pre>\n
The average value of the given list [8, 9, 1, 23, 15, 20, 19, 13, 8, 7, 5, 2, 7, 10, 14, 16] = 11.0625<\/pre>\n
Method #2:Using sum() function ( Static Input separated by spaces)<\/h3>\n
\n
# given list\r\ngiven_list = [8, 9, 1, 23, 15, 20, 19, 13, 8, 7, 5, 2, 7, 10, 14, 16]\r\n# Calculating the sum of list using sum() function and store it in variable sumOfList.\r\nsumOfList = sum(given_list)\r\n\r\n# calculating the length of given list using len() function\r\nlength = len(given_list)\r\n# Calculate the average value of the given list by Dividing sumOfList with length\r\nlistAvg = sumOfList\/length\r\n# printng the the average value of the given list\r\nprint(\"The average value of the given list\", given_list, \"=\", listAvg)\r\n<\/pre>\n
The average value of the given list [8, 9, 1, 23, 15, 20, 19, 13, 8, 7, 5, 2, 7, 10, 14, 16] = 11.0625<\/pre>\n
Method #3:Using sum() function ( User Input separated by spaces)<\/h3>\n
\n
# Scan the list separated by spaces using map and float functions (Because list values can be decimal also)\r\ngiven_list =list(map(float,input(\"Enter some random elements of the list : \").split()))\r\n# Calculating the sum of list using sum() function and store it in variable sumOfList.\r\nsumOfList = sum(given_list)\r\n\r\n# calculating the length of given list using len() function\r\nlength = len(given_list)\r\n# Calculate the average value of the given list by Dividing sumOfList with length\r\nlistAvg = sumOfList\/length\r\n# printng the the average value of the given list\r\nprint(\"The average value of the given list\", given_list, \"=\", listAvg)\r\n<\/pre>\n
Enter some random elements of the list : 47 24.5 27 28 11 23 34.8 33 31 29 45 37 39\r\nThe average value of the given list [47.0, 24.5, 27.0, 28.0, 11.0, 23.0, 34.8, 933.0, 31.0, 29.0, 45.0, 37.0, 39.0] = 100.71538461538461<\/pre>\n
\n