Welcome to our tutorial on finding the minimum and maximum of a list of numbers in Python! In this tutorial, we will be using a list of numbers as examples to demonstrate how to find the minimum and maximum of a list of numbers in Python.
Python provides a very interesting built-in functions “min()” and “max()” which can be used to find the minimum and maximum of a list of numbers respectively. These functions take in a list of numbers as an argument and return the minimum and maximum numbers of a given list.
- min() – This function is used to compute the minimum of the values passed in its argument and the lexicographically smallest value if strings are passed as arguments.
- max() – This function is used to compute the maximum of the values passed in its argument and the lexicographically largest value if strings are passed as arguments.
Python Program to Print Minimum and Maximum of a List of Numbers
# Minimum And Maximum Of A List Of Numbers In Python m = [4,12,43.5,19,2] # max used to compute the maximum of the values passed in its argument print("Maximum number is : ",max(m)) # function is used to compute the minimum of the values passed in its argument print("Minimum number is : ",min(m))
See the magic of predefined functions. If you compare our previous function with this function, you will get to know that we have reduced lines of code. Here we have defined a list with the name “m” and assigned value to the list. In the next line, we have a print statement with a message, and here, we have used the max function and passed the variable m, which returns the max number from the list.
Then in the next line, another print statement with the min function will return the minimum number from the list. For instance, we have a list of numbers [4, 12, 43.5, 19, 2]. To find the minimum number in the list, we can use the min() function and similarly we can use the max() function, to find the maximum number in the list.
Output:
This will output 2, which is the minimum number in the list. This will output 43.5, which is the maximum number in the list.
Maximum number is : 43.5
Minimum number is : 2
I hope now you know how to get the minimum and maximum of a list of numbers with the help of user-defined functions as well as predefined functions. It’s worth noting that, these functions work for any type of numbers, including integers, floating-point numbers, and even negative numbers.
In conclusion, finding the minimum and maximum of a list of numbers in Python is a simple and straightforward task that can be achieved using the built-in min() and max() functions. With the example of [4, 12, 43.5, 19, 2], we saw how easy it is to find the min and max of a list of numbers in Python. By understanding this concept, you’ll be well on your way to using Python for more advanced data manipulation and analysis tasks.
For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions