How do I find the range,average and top three values from this list without a TypeError?

python range to list
python range float
list mean python
how to find the average of 3 numbers in python
if in range python
python statistics standard deviation
standard deviation of list python
range python 3

I m new to python and working on a small project:

90, 75, 65, 50, 40 are the grades below

My code:

grade1 = int(input("Enter grade 1:"))
grade2 = int(input("Enter grade 2:"))
grade3 = int(input("Enter grade 3:"))
grade4 = int(input("Enter grade 4:"))
grade5 = int(input("Enter grade 5:"))

numbers = [grade1,grade2,grade3,grade4,grade5]
sorted_grades = sorted(numbers)
topthree = sorted_grades[-1,-2,-3]

However, on running topthree I receive an error:

TypeError: list indices must be integers or slices, not tuple

How do I avoid this?

You need to use list slicing like so:

topthree = sorted_grades[:-4:-1]

I know it says -4, but it takes the top three.

If you want to use a list, it takes a bit more effort:

indices = [-1, -2, -3]
topthree = [sorted_grades[i] for i in indices]

You can also sort in reverse:

sorted_grades = sorted(numbers, reverse=True)
topthree = sorted_grades[:3]

9. Lists — How to Think Like a Computer Scientist: Learning with , Like numeric 0 values and the empty string, the empty list is false in a boolean ( most recent call last): File "<stdin>", line 1, in <module> TypeError: list indices must be integers Now we are free to make changes to b without worrying about a: numbers = [1, 2, 3, 4, 5] for index in range(len(numbers)): numbers[index]� To average the last 3 numeric values in a range, you can use an array formula based on a combination of functions to feed the last n numeric values into the AVERAGE function. In the example shown, the formula in D6 is: { = AVERAGE ( LOOKUP ( LARGE

Python use : symbol for list slicing. So instead of use topthree = sorted_grades[-1,-2,-3], use topthree = sorted_grades[-1:-4:-1]. Format for list slicing is [start:stop:step].

11. Lists — How to Think Like a Computer Scientist: Learning with , We will use the term element or item to mean the same thing. Finally, a list with no elements is called an empty list, and is denoted []. We have already seen that we can assign list values to variables or pass lists as parameters to functions: for number in range(20): if number % 3 == 0: print(number) for fruit in ["banana",� AVERAGE Function. Easy peasy lemon squeezy (as my 5 year old would say). But if you don’t know AVERAGE it’s simply =AVERAGE(then enter your data range, or the numbers you want to average separated by commas) =AVERAGE(A1:A50) or =AVERAGE(50,65,25,30,81) LARGE Function. Also easy peasy lemon squeezy. Click here for more on the LARGE function.

Assuming that you have already collected the grades into a list called grades:

# New list of sorted entries
sorted_grades = sorted(grades)

# Sum of all list entries divided by the length
average = sum(grades)/len(grades)
# Last entry minus the first entry
range = grades[-1] - grades[0]
# Slice from the third-to-last entry to the end of the list
top_three = grades[-3:]

Syntax such as negative indexing and slicing is discussed further in the tutorial provided in the CPython documentation:

Like strings (and all other built-in sequence type), lists can be indexed and sliced:

>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
>>> squares[0]  # indexing returns the item
1
>>> squares[-1]
25
>>> squares[-3:]  # slicing returns a new list
[9, 16, 25]

All slice operations return a new list containing the requested elements. This means that the following slice returns a new (shallow) copy of the list:

>>> squares[:]
[1, 4, 9, 16, 25]

The general format for list indexing/slicing is some_list[start:stop:step]:

>>> numbers = [1,3,5,7,9]
>>> numbers[0:3]  # slice from the first element to the third
[1, 3, 5]
>>> numbers[:3]  # 0's can be omitted
[1, 3, 5]
>>> numbers[1:3]  # slice from the second element to the third
[3, 5]
>>> numbers[3:]  # slice from the third element to the end
[7, 9]
>>> numbers[-3:]  # slice from the third-to-last to the end
[5, 7, 9]
>>> numbers[::-1]  # slice of the whole list, stepping backward by 1 for each entry
[9, 7, 5, 3, 1]
>>> numbers[1::2]  # slice of every other entry, starting with the second
[3, 7]

Note the list slices are end-exclusive, so numbers[1:2] only returns the second entry: [3].

Python range() function, The range() function is used to generate a sequence of numbers. Most common use of range() function in Python is to iterate sequence type (List, string etc. well as how big the difference will be between one number and the next. range() The range of integers end at stop – 1. step: integer value which� To average the top 5 scores from the list, you can use a formula based on the combination of LARGE and AVERAGE. To understand this formula, you need..

Python statistics, mean() function can be used to calculate mean/average of a given of the central location of data in a set of values which vary in range. [data-set] : List or tuple of a set of numbers. TypeError when anything other than numeric values are passed as data4 = (fr( 1 , 2 ), fr( 44 , 12 ), fr( 10 , 3 ), fr( 2 , 3 )). Use the AVERAGE and the LARGE function in Excel to calculate the average of the top 3 numbers in a data set. 1. First, the AVERAGE function below calculates the average of the numbers in cells A1 through A6. 2. For example, to find the third largest number, use the following LARGE function. 3. The formula below calculates the average of the top 3 numbers.

Mean, Median, Mode, and Range, There are many "averages" in statistics, but these are, I think, the three most common, Find the mean, median, mode, and range for the following list of values:. When you want to total the amount of sales from your top 10 performers, you usually would have to sort the data first, then use the SUM function to add up the first 10 values in the sorted range.

5. Data Structures — Python 3.3.7 documentation, Remove the first item from the list whose value is x. To retrieve an item from the top of the stack, use pop() without an explicit index. [[row[i] for row in matrix] for i in range(4)] [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]] (most recent call last): File "< stdin>", line 1, in <module> TypeError: 'tuple' object does not� To average the top 3 scores in a data set, you can use a formula based on the LARGE and AVERAGE functions. In the example shown, the formula in G6 is: = AVERAGE ( LARGE ( B6:F6 , { 1 , 2 , 3 } )) How this formula works The LARGE function can

Comments
  • What you expecting from sorted_grades[-1,-2,-3]?
  • I am trying to get the top most grades. so in this example, I want 90,75,60 and it should drop the lowest two @Carcigenicate
  • It seems that you need to use negative indexing, e.g. sorted_grades[-3: ] or sorted_grades[-3: ][::-1]
  • i used topthree = sorted_grades[:-4:-1] but how would I get the range and the average? range being largest number minus smallest in topthree
  • @user9991165 Please avoid asking multiple distinct questions in the same question. Ask another one if needed; each question is for one specific problem. For both of your other issues, there are plenty of answers on SO.