This example will tell you how to calculate a list of number’s average values in python.
1. Use Python To Calculate Average Number Of A Number List Example1.
# This function is used to calculate the average number of a list number. def calculate_average_number_in_list(list): # If the list object is not none. if list: # Used to save summarize value of the number list value. sum = 0.0 # Number type list element counter. number_type_element_count = 0 # Loop in the list object. for value in list: # If the list element is a number. if is_number(value): # Number type list element counter plus 1. number_type_element_count = number_type_element_count + 1 # Convert the value to a float number. float_number = float(value) # Add the number to the summarize value. sum = sum + float_number # Calculate the average number value of the number list. average_number = sum / number_type_element_count print('Average number of list : ') print(list) print('is') print(average_number) ''' check whether the value can be converted to a float number value. If the value is a string and contains no digit value, then it can not be converted to a float number.''' def is_number(value): ret = True try: float_value = float(value) print('\'' + value + '\' is a number.') except: print('\'' + value + '\' is not a number.') ret = False finally: return ret if __name__ == '__main__': # Create a list with number and none number string. list = [1,2,3,4,5,"6",7,8.1,'abc','10'] calculate_average_number_in_list(list)
Before is the above source code execution result.
'6' is a number. 'abc' is not a number. '10' is a number. Average number of list : [1, 2, 3, 4, 5, '6', 7, 8.1, 'abc', '10'] is 5.122222222222223
2. Calculate User Input Number List Average Value In Python.
from builtins import input ''' check whether the value can be converted to a float number value. If the value is a string and contains no digit value, then it can not be converted to a float number.''' def is_number(value): ret = True try: float_value = float(value) print('\'' + value + '\' is a number.') except: print('\'' + value + '\' is not a number.') ret = False finally: return ret def calculate_user_input_number_list(): number_count = eval(input("How many numbers in the list?")) sum = 0 for i in range(number_count): x = eval(input("Enter a number : ")) if is_number(x): sum = sum + float(x) else: print("Your input value\' " + x +"\' is not a number, program exit. ") return average_value = sum / number_count # str(average_value) will avoid the TypeError: can only concatenate str (not "float") to str. print("\nThe average valur is: " + str(average_value)) if __name__ == '__main__': calculate_user_input_number_list()
Below is the above source code execution result.
How many numbers in the list?3 Enter a number : 89 Enter a number : 63 Enter a number : 98 The average valur is: 83.33333333333333
If your input is not a number type, then it will show the below output in the console.
How many numbers in the list?3 Enter a number: 1 Enter a number: "6" '6' is a number. Enter a number: "abc" 'abc' is not a number. Your input value 'abc' is not a number, program exit.