Can you please tell me how to calculate the lowest and highest number in this list using python

StackOverflow https://stackoverflow.com/questions/19672374

  •  01-07-2022
  •  | 
  •  

Question

markList=[]
while True:
    mark=float(input("Enter your marks here(Click -1 to exit)"))
    if mark == -1:  break
    markList.append(mark)

markList.sort()
mid = len(markList)//2
if len(markList)%2==0:
    median=(markList[mid]+ markList[mid-1])/2
    print("Median:", median)

else:
    print("Median:" , markList[mid])     #please do not touch anything starting from this line and above, I have already found the median with this and all Im looking for it to 

find out the lowest and highest grades, this program is asking for the user to input their grades and it telss you the highest, lowest and grade average

min(mark)
print("The lowest mark is", min(mark))

max(mark)
print("The highest mark is", max(mark))
Was it helpful?

Solution

I'm not a Python expert by any stretch but I've learned some basics. You can refer to the documentation on the list object here:

http://docs.python.org/2/library/functions.html#max

list objects in python allow a min() and max() function. You can call these even before you sort the list, for example:

print min(marklist) or print max(marklist).

Since you've already sorted the list to do your median calculation, you could also just retrieve the first and last item in the list, as the lowest & highest marks, respectively:

print marklist[0] #prints the first/0-index position in the list

And likewise the maximum value, since python supports reverse-indexing of list objects:

print marklist[-1] #prints the last position in the list

I tested it with this simple code:

marklist=[]
marklist=[10,13,99,4,3,5,1,0,22,11,6,5,38]
print min(marklist),max(marklist)
marklist.sort()
print marklist[0],marklist[-1]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top