collection data types we split every line on whitespaceand for each item we attempt to convert it to float if conversion succeeds--as it will for integers and for floating-point numbers in both decimal and exponential notations--we add the number to the numbers list and update the frequencies default dictionary (if we had used plain dictthe update code would have been frequencies[numberfrequencies get(number using str format(with mapping unpacking if conversion failswe output the line number (starting from line as is traditional for text files)the text we attempted to convertand the valueerror exception' error text rather than using positional arguments ( format(filenamelinoetc or explicitly named argumentsformat(filename=filenamelino=linoetc )we have retrieved the names and values of the local variables by calling locals(and used mapping unpacking to pass these as key-value named arguments to the str format(method def calculate_statistics(numbersfrequencies)mean sum(numberslen(numbersmode calculate_mode(frequencies median calculate_median(numbersstd_dev calculate_std_dev(numbersmeanreturn statistics(meanmodemedianstd_devthis function is used to gather all the statistics together because the mean ("average"is so easy to calculatewe do so directly here for the other statistics we call dedicated functionsand at the end we return statistics named tuple object that contains the four statistics we have calculated def calculate_mode(frequenciesmaximum_modes)highest_frequency max(frequencies values()mode [number for numberfrequency in frequencies items(if frequency =highest_frequencyif not ( <len(mode<maximum_modes)mode none elsemode sort(return mode there may be more than one most-frequently-occurring numberso in addition to the dictionary of frequenciesthis function also requires the caller to specify the maximum number of modes that are acceptable (the calculate_statistics(function is the callerand it specified maximum of three modes the max(function is used to find the highest value in the frequencies dictionary thenwe use list comprehension to create list of those modes whose frequency equals the highest value we can compare using operator =since all the frequencies are integers