In this post, we are going to write a simple Python script to find the largest and the smallest number in a list.
It is really simple to find the largest and smallest number in a list using Python function max() and min() respectively, let’s write the program so you can understand it easily.
1 2 3 4 5 6 7 8 |
# Find the smallest number in a list mylist = [20,65,100,5,30,6] print("Smallest number:") print min(mylist) # Find the largest number in a list print ("Largest number:") print max(mylist) |
As you can see the above python script, we have created a list called “mylist” with 6 numbers [20,65,100,5,30,6]. The smallest number in our list is 5 and the largest number is 100. So to find the smallest number we have used the Python function min(mylist) and to find the largest number we have used max(mylist).
The output of the above Python script will be:
1 2 3 4 |
Smallest number: 5 Largest number: 100 |
Thank you for your time to read this post, I hope this would have helped you.