Python offers a wide range of built-in functions to perform various tasks such as pow(), abs(), input(), etc. All these functions serve different tasks, such as the pow() function calculates the power, the abs() function retrieves an absolute value, the input() function takes input from the user, and so on. Similarly, “min()” is an inbuilt function that finds the lowest element.
This write-up will present a comprehensive guide on the Python min() function with numerous examples. The content of this post is discussed as follows:
- What is Python min() Function?
- Example 1: Python min() to Find the Smallest Number and Alphabet
- Example 2: Python min() to Find the Smallest Items and String in a List
- Example 3: Python min() to Find Smallest Key and Key Value in Dictionary
So let’s get started!
What is Python min() Function?
Python’s “min()” function is used to find the minimum value of an iterable object such as a list, tuple, or dictionary. This function takes numeric and string values as an argument and returns the smallest number or string. If you specify the mixture of numbers and alphabets in the min() function, then it will not retrieve anything. The syntax of the Python “min()” is shown below:
min(x1, x2, x3, ...)
or
min(iterable)
The parameters x1 and x2 are the values to be compared. The parameter “iterable” can have one item or multiple items to compare.
Example 1: Python min() to Find the Smallest Number and Alphabet
In the example given below, the inbuilt “min()” function finds the smallest number and string from the given numbers.
Code:
Number = min(28, 32, 3, 441, 66.2)
Alphabet = min("Bike", "Car", "Ship")
print(Number)
print(Alphabet)
In the above code:
- The “min()” function is used to find the smallest numbers from the given integer and float values.
- The “min()” function is again utilized to retrieve the lowest alphabet value from the tuple.
Output:
The above output shows that the min() function finds the smallest number and lowest alphabet.
Example 2: Python min() to Find the Smallest Items and String From a List
Python’s “min()” function can also be used to find the minimum value from an iterable List. In the example given below, the “min()” function finds the minimum value of numeric and string items of the list.
Code:
Num_List = [43, 22, 38, 45, 50, 26]
Str_List = ["itslinuxfoss", "Python Guide", "Linux", "Ubuntu"]
print("Smallest number:", min(Num_List))
print("Smallest string:", min(Str_List))
In the above code:
- A numeric list and a string list is initialized in the program in a variable named “Num_List” and “Str_List”, respectively.
- The “min()” function accepts the value of the input variable as an argument and retrieves the smallest number and string.
Output:
The output shows the smallest number, “22”, and the smallest string, “Linux”.
Example 3: Python min() to Find Smallest Key and Key Value in Dictionary
In the below code Python “min()” function is used to find the minimum value of the dictionary without using the “key” parameter and with the “key” parameter.
Code 1: (Dictionary Containing Numbers)
Dict = {5: 14, 13: 9, -1: 42, -22: 34}
min_dict = min(Dict)
print("Smallest Key Value:", min_dict)
min2 = min(Dict, key = lambda k: Dict[k])
print("The key with the smallest value:", min2)
print("Smallest value:", Dict[min2])
In the following code:
- The dictionary variable containing numbers is initialized.
- The “min()” function accepts the Dictionary variable value as a parameter and retrieves the smallest value of the key.
- The “key” parameter is passed with the lambda function, the “key” dictionary is compared, and the key with the smallest value is printed on the screen. A lambda is a function that is defined without any name.
- The statement “Dict[min2]” shows the value of the key with the minimum value.
Output:
The above output shows the smallest key, the key with the smallest value, and the smallest value using the “min()” function.
Code 2: (Dictionary Containing String)
Dict_1 = {'Name': 'Alex', 'Sex': 'Male', 'Designation': 'Developer'}
min_dict1 = min(Dict_1)
print("Smallest Key Value:", min_dict1)
min3 = min(Dict_1, key = lambda k: Dict_1[k])
print("The key with the smallest value:", min3)
print("Smallest value:", Dict_1[min3])
In the code mentioned above:
- The dictionary value containing string data type is initialized.
- The same “min()” function is used to get the lowest alphabet order string. The working of this example code is similar to the previous code-1 example.
Output:
The above output shows that the value of the Dictionary having the smallest key was printed on the screen.
That’s it from this post!
Conclusion
Python’s “min()” function is used to find the minimum value of an iterable object such as a list, tuple, or dictionary. The min() function accepts alphabetic or numeric values and retrieves the minimum value from the given values. This blog post presented an in-depth guide on the Python “min()” function with appropriate examples.