Monday, June 21, 2021
Latest:
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 ?

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 am legend. My solution was similar to yours but I placed both if statements in a single for loop. Many thanks Mr. McWhorter

4. 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.

5. 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 😋 🥃

6. well the easy way to arrange the list is to just type grades.sort() and the hard way is to do it with for loops and If.

7. 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.

8. Love to see you teaching us C someday. Shout out for the Best teacher on youtube.

9. Paul have you successfully reached Africa. If yes then when are live streams starting

10. 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:

orgRank=0

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

orgRank=0

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

orgRank=orgRank+1

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

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?

11. Hey, Paul!
The way I did the homework was –

if i > max:

max=i

if i < min:

min=i

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!

12. Finished the homework, made this old man's brain work overtime, but she'll sort with best of them. This just gets better and better. See you next week.

13. 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?

14. I am legend. I did that a bit differently by not adding 2 more for loops instead I added the 2 if statements in the first for loop

15. 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.

16. Can we also use the min() and max() function instead of this for loop?

17. I AM LEGEND and proud to be learning Python

18. 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

19. 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?

20. 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

21. 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.

22. 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.

23. I'm a legend, but used a different approach, "grades.sort()"

24. Done by consolidating functions into two "for" loops, hence I AM LEGEND (all caps).

25. 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!

26. I am legend! And I didn't even have to google this time.

27. 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?