top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

show some statistics

Writer's picture: Ahmed SheblAhmed Shebl

In this code, I didn't use built in function for statistics.


I made two function that takes a list argument:

1 - list_average(lst) --> take a list and return the average.


def list_average(lst):
    total = 0
    for item in lst:
        total += item
    
    count_items = len(lst)
    
    return total / count_items

2 - list_max_min(lst) --> take a list and return the max and min items.


def list_max_min(lst):
    max_item = lst[0]
    min_item = lst[0]
    for item in lst:
        if item > max_item:
            max_item = item
        elif item < min_item:
            min_item = item
     
    return max_item, min_item

Then I take the input from user as numbers separated by spaces, and converted it to list


print("Enter numbers separated by space between them: ")
lst = input().split(' ')

Then to be sure no error arise, in a for loop I created two lists:

one for numeric values, and the other for wrong values which is not numeric.


for item in lst:
    try:
        lst_numeric.append(float(item))
    except ValueError:
        lst_not_numeric.append(item)

Then I call the functions I created to calcualate statistics.


list_count = len(lst_numeric)
list_average = list_average(lst_numeric)
list_max, list_min = list_max_min(lst_numeric)

Finally, I print the numeric values and their statistics, and if there are wrong values entered by the user, I print them in separate lines.


print("the numbers: ")
print(lst_numeric)
print("has theses statistics:")
print("---------------------")
print("count:   " + str(list_count))
print("average: " + str(list_average))
print("max:     " + str(list_max))
print("min:     " + str(list_min))
if len(lst_not_numeric) > 0:
    print("Note that you entered these wrong type input:")
    print("---------------------------------------------")
    print(lst_not_numeric)

The complete code is here:


0 comments

Recent Posts

See All

Opmerkingen


COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page