Python is a high-level, object-oriented programming language that comes with a rich set of built-in functions that make programming easier and more efficient. One of these built-in functions is "min()". The "min()" function is used to return the smallest item in an iterable or the smallest of two or more arguments.
The syntax for the "min()" function is as follows:
|
min(iterable, *[, key, default])
|
Here, "iterable" is the sequence or collection of elements that you want to find the minimum of, and "key" is a function that is used to determine the order of the elements in the iterable. "default" is an optional argument that is used to specify a default value if the iterable is empty.
Let's take a look at some examples to better understand the "min()" function.
Example 1: Finding the minimum element in a list
|
numbers = [4, 2, 7, 1, 9, 3]
min_num = min(numbers)
print(min_num)
|
1
In this example, we have a list of numbers, and we use the "min()" function to find the smallest number in the list. The smallest number in the list is 1, which is returned by the "min()" function.
Example 2: Finding the minimum value in a dictionary
Output:
0.25
In this example, we have a dictionary that contains the prices of various fruits. We use the "min()" function to find the lowest price among the values in the dictionary. The lowest price is 0.25, which is returned by the "min()" function.
Example 3: Finding the minimum of two or more arguments
|
num1 = 10
num2 = 5
num3 = 15
min_num = min(num1, num2, num3)
print(min_num)
|
Output:
5
In this example, we have three numbers, and we use the "min()" function to find the smallest number among them. The smallest number is 5, which is returned by the "min()" function.
In conclusion, the "min()" function is a very useful built-in function in Python that helps you find the smallest element in a sequence or collection of elements. It is very easy to use and can be applied to a wide range of scenarios.