How do I find the average of a list of numbers in Python?

Average means adding up all the items to the list and then dividing by the number of numbers in the list.

  1. def average(num): 
  2. sum = 0 
  3. for i in num: 
  4. sum = sum + i  
  5.  
  6. avg = sum / len(num) 
  7. return avg 
  8.  
  9. print("The average is", average([19,2,53,32,15])) 

The sum() and len() built-in functions are used to find intermediate levels in Python

  1. list = [1,2,3,4,5] 
  2. avg = sum(list)/len(list) 
  3. print("The average is ", round(avg,2)) 

Also, you can find out the average of a list using the mean method from the NumPy library.

  1. from numpy import mean 
  2. list = [5, 3, 4, 35, 12, 6, 42] 
  3. avg = mean(list) 
  4. print("The average is ", round(avg,2)) 

Comments

Popular posts from this blog

proassignment help backlink data index 41

programming shark backink data index 31

Programming shark backlink data index 67