Python min() function
The min() function in Python returns the item with the lowest value or the item with the lowest value in the specified iterable. If the given values are strings, an alphabetical comparison is done.
Syntax
1 2 3 |
min(iterable, *[, key, default]) |
or
1 2 3 |
min(arg1, arg2, *args[, key]) |
Parameter
arg1, arg2, *args : This parameter represents one or more items to compare.
iterable: This parameter represents an iterable, with one or more items to compare.
Return
This function returns the smallest item in an iterable or the smallest of two or more arguments.
Raises
If the iterable is empty and the default is not provided, a ValueError is raised.
Example 1
1 2 3 4 5 6 7 8 9 10 |
# Python program explaining # the min() function val1= 25 val2=89 # calling the min() function min_val = min(val1, val2) #printing the min value print("The min value between",val1,"and",val2,"is",min_val) |
Output
1 2 3 |
The min value between 25 and 89 is 25 |
Example 2
1 2 3 4 5 6 7 8 9 10 11 |
# Python program explaining # the min() function #passing string value str1= "Apple" str2="Banana" str3="Orange" #For string values, an alphabetically comparison is done. min_num = min(str1, str2,str3) print("The min value between",str1,", ",str2,"and ",str3," is ",min_num) |
Output
1 2 3 |
The min value between Apple , Banana and Orange is Apple |