Python

Python Tutorial 11: Finding Maximum and Minimum Numbers in a List in Python



You guys can help me out over at Patreon, and that will help me keep my gear updated, and help me keep this quality content coming:

In this video we show step-by-step instructions on how to write a python program that will find high and low numbers in a list. I do not assume you are an expert, so these lessons are designed for complete beginners.
#Python
#Lessons
#Programming

Similar Posts

48 thoughts on “Python Tutorial 11: Finding Maximum and Minimum Numbers in a List in Python
  1. Personally I used the function max and min. Easier and more simple, is it still ok ?

    print('Your highest grade was', max(grades))

    print( 'But your lowest grade was', min(grades) )

  2. I stopped in the middle to figure this one out. Seems one issue. We cannot assume that the highest grade would be 100. What if were based on 200? We can't up and rewrite the code every time the test has different scoring method. So we need to sort it out with the known variables. My method was to assume "lowScore=grades[0]" as a starting point. I placed that under the line of grades.append(grade). Then code "if (grades[i]<lowScore): lowScore=grades[i]". That swaps out to the lower value. Of course the best way would be to prompt the user of what the maximum score is and based it on that. I just found assigning a static value as the maximum grade to be too limited.

  3. I have manually done the highest/lowest and sorted the list. I don’t see the sense in doing like this though. There is a Max, Min and Sort function built into lists and arrays. These are probably more efficient and less prone to errors.

  4. I spent nearly 3 hours trying to do this homework and folding up like a cheap garden deckchair 😔
    Then, after a few days, I decided to try again 🤨
    And, well, I'm not going to give the game array 🤭 I mean away!!! 🤦🤦🤦🤫
    But I'm now sitting back in my deckchair like a legend 😁 drinking a nice glass of ice tea 😋 🥃

  5. Hello Paul;
    Today I finished the homework of this lesson, and I did not use the sort function. It took me many hours. I am 78 years old, and your homework keep my brain from rusting.

  6. Just writing to see if any else solved this week's problem the same way I did (spoilers for the answer btw):

    This is all the new stuff I added to my existing code:

    orgGrades=[]

    orgRank=0

    for q in range(0,x,1):

    orgRank=0

    for m in range(0,x,1):

    if(Grades[q]<Grades[m]):

    orgRank=orgRank+1

    orgGrades[orgRank]=Grades[q]

    print("Your Grades from highest to lowest is: ")

    for j in range(0,x,1):

    print(" ",orgGrades[j])

    I had to create two new variables; one being a new array called orgGrades (standing for organized grades) and an int called orgRank (organized rank). Essentially once we are done adding to the Grades & the new orgGrades array (they are both the same at this point) and have calculated the avg., max & min grades; this new FOR loop checks one grade at a time and compares its value to the rest of the grades in the Grades array and assigns the grade a rank to properly organize the orgGrades array from highest to lowest. For example, lets say our Grades array was [92,100,59,87], our orgGrades would be exactly the same as this. In this new piece of code, first we start at the first for loop which sets i =0 and then the next for loop goes to m=0. We compare 92 to 92 and check if it's lower than itself (obiously it isn't) and if it isn't it doesn't change the orgRank. After that the for loop cycles through the entire array adding 1 to orgRank whenever 92 is lower than a value. After that's done, the orgRank is used to place 92 into orgGrades at its proper order (in this case 92 is has a orgRank of 1 because it's only lower than 1 value in the entire Grades array, this 1 determines its position in the new orgGrades array). Then it moves to 100, does that again and so on and so on. I'm pretty sure there's a better way of doing this, one that doesn't require making a new array but this was the quickest solution I found and it works 😀 . Anyone who has also solved this, how did you do it?

  7. Hey, Paul!
    The way I did the homework was –
    # max grade

    max=grades[0]

    for i in grades:

    if i > max:

    max=i

    print('Max grade is: ', max)

    #min grade

    min=grades[0]

    for i in grades:

    if i < min:

    min=i

    print('Min grade is: ', min)

    This way we are not stuck to 0-100 system, we might as well give negative grades. Is there a school somewhere in the world where teachers give negative grades, though? 🙂

    I love your tutorials and it's a pity, these tutorials are not used in high school programs all around the world.. Especially in this C19 era..
    I can't wait to see the next ones.
    Wish you all the best!

  8. Hi Paul, I wanted to ask something.

    You said that we have to use a for loop in order to sort the numbers in the list. I did it by using the 'sorted()' function command. Is that right or it would be better to find it using a for loop?

  9. Have you looked at using formatted string literals at all, they were introduced in Python 3.6 and make your code look so much nicer.
    From my homework: print(f'Your average grade is {average}, your lowest grade is {low_grade} and your highest grade is {high_grade}')

  10. I found low and high grade through this form. Is there any differences of note in the two methods? for num1 in grades:
        if (maxvalue == 0 or num1 > maxvalue):
            maxvalue = num1

  11. I finished the homework for next week, sorting the grades from high to low, but I couldn't do it without a while loop. do you think this is cheating?

  12. To the best online teacher, thank you for the content. You make us see the fun in doing engineering. It isn't always about the hard stuff, we can also do a lot of cool stuff

  13. I am legend. Got this via two methods. A for loop that steps thru and compares each grade, and also used a Python list methods .min and .max. This revealed that there are many many many list methods in Python.

  14. My solution was to first start with biggest grade and then, when searching for smallest, make the initial variable same as the biggest so i know it cannot get lower than low.

  15. Nailed it, be it by using max and min functions instead of another for loop…. Things are starting to make sense and I discover there are multiple ways leading to Rome…. Thanks Paul!

  16. Your teaching style makes it almost seem as though you are here, in my room, in person. Makes it easy to learn for folk of any age.(spoken by a 76 year old). Can I do a one off donation?

Leave a Reply

Your email address will not be published. Required fields are marked *