Get maximum and minimum number from a list in Python
Today in this Python related post, I am going to show you how we can get the maximum and minimum number from a list.
For example, suppose, we have a list in Python that is given below:
number_list = [12, 15, 26, 13, 11]
From the above list, we have to find the maximum and minimum number. This task is going to be easy. Continue reading.
Python already has two functions. These are max() and min() functions. Using these functions, we can easily get maximum and minimum number from a list in Python.
To get the minimum number from the list, we just have to pass the list as a parameter to the Python min() function just like you can see below:
minimum_number = min(number_list)
And to get the maximum number from the list we have to pass the list as a parameter to max() function of Python which you can see below:
maximum_number = max(number_list)
- How to delete a file in Python with examples
- How to read CSV file in Python using Pandas Library?
- Duplicate Elements Removal of an Array or List in Python
If you want to run and print the maximum and minimum number of the list, then below is the complete code that contains everything:
number_list = [12, 15, 26, 13, 11] minimum_number = min(number_list) maximum_number = max(number_list) print("Minimum number is "+ str(minimum_number)) print("Maximum number is "+ str(maximum_number))
In the above code, you can see that we have used the str() method while printing because the variables contain an integer. So to print it on the screen we have converted them with the str() method of Python.
If you run the code in your system, you will able to see the maximum and minimum number printed on your screen.
Below is the output that you will see after you run the above code:
Minimum number is 11 Maximum number is 26
You can change the number and add any number as you like in the list. It will always get the maximum and minimum number from the list perfectly and show it to you.