This tutorial focuses on the topic Python min() function and simplifies its implementation through example programs.
Python is a very flexible language and acquires a lot of users nowadays. To help you be one of the Python programmers (users), let us learn its built-in functions.
One of the language functions that we’re about to discuss is the min() function.
What does min mean in python?
Python min() function which means minimum is used to return the lowest value among the elements in an object or set (iterable). But in the case of the comparison is based on alphabetical order.
min() is one of the functions that handle arguments and calls the lowest value among the iterable. To clarify how the function works with the available example programs below.
Also read: Permute In Python (Explanation With Examples)
Python min()
See the following examples to know how the function works.
Example 1: Numbers
sampleNumbers = (9, 5, 7, 3, 1, 4, 6)
print(min(sampleNumbers))
Output:
1
Example 1 shows how the Python min() works with numbers. Upon executing the program, it directly returns the lowest value among the numbers which is one (1) in the output.
Example 2: Strings
sampleString = ("bbb", "oppa", "mama", "baba")
print(min(sampleString))
Output:
baba
The output in Example 2 shows how Python min() functions with strings. T=This function works as getting the minimum or lowest letters from the alphabet. It is clear that the lowest value in the alphabet is a and the highest is z.
Moreover, you can practice with Python strings through how to print words Python full tutorial with Examples.
Now, let us proceed with the functions of min().
Two min() functions
Python min() is a function that is applicable in two forms:
- To return the lowest value among objects with iterable arguments (default function),
- To return the lowest value among objects without iterable arguments.
min() Python with iterable arguments
When we use Python min() with iterable arguments, it simply calls the smallest key value among the iterable. However, the function will return the default value if the argument iterable is empty.
Nevertheless, min() function always returns the smallest value among the iterable arguments.
Syntax
The syntax of the min() function for objects with iterable arguments is…
min(iterable, *iterables, key, default)
Parameters
The parameters of min() function include:
- iterable – this parameter can be in form of a list, tuple, set, dictionary, etc.
- *iterable – is the parameter that works for numbers and is usually more than one value.
- key – is the parameter by which iterable is provided and whose return value is used for comparison.
- default – is the parameter used when the iterable argument is empty.
Return Value
The Python min() function typically returns the smallest (lowest) value among the iterable.
Example program to get the smallest item in a list
Now let us test the min() function with the list.
sampleList = [111, 143, 555, 50, 99, 33]
print(min(sampleList))
Output:
33
Among the given iterable (list), 33 is the smallest item. This example proves that the function works fine with item lists.
Example program to get the smallest string in a list
Another example will show you how the function works with a list of strings.
sampleList = ["John", "Jessa", "Jane", "Jay"]
print(min(sampleList))
Output:
Jane
The second example provides the output “Jane” which indicates that it is the lowest value among the strings. To analyze why the output calls Jane the lowest give the conclusion that the lowest among the alphabet is A and the highest is B.
Example program to get min() in dictionaries
The example below will test the min() function with Python dictionaries.
numbers = {1: 5, 2: 4, 3: 3, 4: 2}
num1 = min(numbers)
print("Smallest Number:", num1)
num2 = min(numbers, key = lambda a: numbers[a])
print("Number with smallest value:", num2)
print("Smallest value:", numbers[num2])
Output:
Smallest Number: 1 Number with smallest value: 4 Smallest value: 2
The given example shows three different outputs from the dictionary. All the numbers in this example have a corresponding value. Among the given numbers, one (1) is the smallest, four 4 is the number with the smallest value, and 2 is the smallest value.
So basically, You can explore more about the min() function with the dictionary key. You can try these methods on your program this instant and enjoy the tutorial.
However, you may also explore how the Python min() works without iterable arguments in the next topic.
min() Python without iterable arguments
In the case of using Python min() with the absence of iterable, its function will still work the same.
Before trying how the min() applies its function without iterable arguments, let us first discuss the following:
Syntax
min(arg1, arg2, *args, key)
The syntax of Python min() for arguments with or without iterable is the same but the parameters are not.
Parameters
- arg1/arg2 – this parameter may include numbers, strings, or any form of an object.
- *args – this parameter is optional and could be in multiple arguments forms of objects.
- key – the parameter key optional and the min() function is done on each key before the comparison which depends on the smallest value.
Return value
min() function returns the smallest value among the given arguments.
To justify the statements above, let us now see the examples below.
Find the minimum python functions among the given numbers
Example 1: Numbers
numbers = (98, 87, 96, 67, 56, 63)
print(min(numbers))
Output:
56
The program reads the function and returns the smallest value among the numbers. The output reveals that the number 56
is the lowest among the numbers.
Python min((), default)
The default function of Python min() is to return the minimum (lowest) value among the given objects or iterable. It reads all of the given inputs regardless of how many iterable arguments are present. Then the function compares the values and displays the result afterward.
However, the program may raise an error if the arguments are empty. This happens when the function cannot perform its method because it cannot compare any values.
On the other hand, the min() has a corresponding opposite function which is the max().
max() returns the highest value among the iterable or given arguments. This explains that max() is the opposite of min().
Python Min/Max list
The Python Min and Max are quite similar but opposite in function at the same time. To test their functionalities, let us use them in the example below:
Example
values = [23, 22, 24, 25, 26]
print("Lowest value:", min(values))
print("Highest value:", max(values))
Output:
Lowest value: 22 Highest value: 26
The simple explanation in the given example is that the Python min() returns the lowest value and the Python max() returns the highest.
Conclusion
In conclusion, the full tutorial on the Python min() function with example programs fills every detail regarding how the function works.
Additionally, this tutorial provided various examples to try how the min() function handles different types of objects or arguments. It also gives a glimpse of knowledge about the opposite function of Python min(), the Python max().
I know that all of you now know how to use Python min() in your programs and how it works. If you have any clarification on this tutorial, you may leave a comment below.