Menu Close

Python min() Function

python min function

In this article, we are going to learn all about the Python min function to find the lowest value from the iterable ( list, tuple, set, etc ) and multiple values. If you have a large iterable object, Then this will best function to find the lowest value from them.

In the previous tutorial, we have seen all about the Python max() function to find the highest value from the iterable.

Python min() Function Introduction

Python min() function is a Python built-in function that comes with Python default. min() function returns the lowest value from the iterable or the smallest value from the iterable. If the value is a string, then an alphabetical comparison will be done.

Syntax

The syntax of the min() function in Python is:-


min(iterable, *iterables, key, default)

Parameter

Python min() function accepts various parameters.

  • iterable:- iterable to find lowes value.
  • *iterables:- More than one iterable.
  • key:- function to define the sort order.
  • default:- Return value, If the given iterable is empty.

Return Value

The return value of the min() function in Python is the lowest value from the iterable or total numbers.

Python min() function example

Here, I am going to explain the min() function with different-different examples so that you can understand it easily.

Example: find the smallest value from integer values


# Find lowest value from the numbers.
result = min(12, 23, 43, 10)
print(result)

The output will be:- 10

Example: find the smallest value from string values


# Return the name with lowest value, order alphabetically.
result = min('Python', 'Java', 'PHP', 'HTML')
print(result)

The output will be:- PHP

Example: find the smallest value from the list of the integer values

# Find smallest value from the list iterable.
my_list = [12, 10, 23, 40, 100]
print(min(my_list))

The output will be:- 10

Example: find the smallest value from the list of the string values

# Find smallest value from the list iterable.
students_name= ['Python', "C++", 'Java', 'Dot Net']
print(min(students_name))

The output will be:- C++

Example: Using the min() function with a default value

If the given iterable is empty, the value of the default parameter will return.


# returning default value from the min.
students_name = []
print(min(students_name, default="Programming Funda"))

The output will be:- Programming Funda

Example: Using the min() function key parameter with built function

In this article, we want to get the smallest value from the list of string items. The smallest value will return according to their length.


# returning default value from the min.
students_name = ['Python', "C++", 'Java', 'Dot Net', "R"]
result = min(students_name, key=len)
print(result)

The Output will be:- R

Conclusion

So, In this article, we have seen how to find the lowest value from the iterable using the Python min() function. You can use multiple values inside the min function separated by a comma to find the lowest value from them instead of iterable. If you like this article, please share and keep visiting for python built-in functions tutorials.

Python built-in functions


For more information:- Click Here

Reference:- Click Here

Thanks for your valuable time… ❤️❤️

Python map() Function
Python oct() Function

Related Posts