8 Examples to Use Python min() Function in the Best Way

Introduction

Python has several built-in functions. These all functions are the global functions of python, which can be called from python code. We do not have to import any library for it. In this tutorial, wie will be studying the min() function in python. There are a lot of functions in python like max(), abs(), next(), pow(), etc.

What is min() in python?

The min() function in python is used to return the minimum values passed in its argument and lexicographically smallest value if the strings are passed as arguments.

Syntax of Python min()

min(iterables, key,default)

Parameter

  • Iterables – It is the type of objects like list, tuple, dictionary, etc.
  • Key – It is optional input in the min() function. This is the parameter where the iterables are passed, and the comparison is performed.
  • Default – It is also the optional input in the min() function. In this, we pass the default value if the given iterable is empty.

Return value of Python min() Function

It returns the minimum value from the iterables passed.

Exceptions

It also returns exceptions sometimes when we used to compare the conflicting types of iterables.

Examples

Let us understand the min() function of python in details with the help of examples:

1. using Python min() function in string

In this example, we will be taking the input as a string and finding the string’s minimum value.

#using input as a string

str = "LatrAcaLsSolutions"
Minimum = min(str)

print("Minimum value in the string: ", Minimum)

Output:

Minimum value in the string :  A

Explanation:

Here firstly, we will take the input as a string. Secondly, we will apply the min() function and store the output in the Minimum variable. At last, we will print the output. Hence, we will see the minimum value in the string.

2. using Python min() function in list of integers

In this example, we will be taking the input as a list containing integer values in it. Then, we will try to find the minimum value of the integer in the list.

#using a list of integers

lst = [5, 4, -5, 0, 4]
minimum = min(lst)
print("Minimum in the list is : ",minimum)

Output:

Minimum in the list is :  -5

Explanation:

Here firstly, we will take the input as a list of integers. Secondly, we will apply the min() and store the output in the minimum variable. At last, we will print the output. Hence, we will see the minimum value in the list of integers.

3. using min() function in list of strings

In this example, we will be taking the input as a list containing string values in it. Then, we will try to find the minimum value of the string in the list.

#using a list of string

lst = ["Latracal", "Solutions", "USA"]
minimum = min(lst)
print("Minimum in the list is : ",minimum)

Output:

Minimum in the list is :  USA

Explanation:

Here firstly, we will take the input as a list of strings. Secondly, we will apply the min() and store the output in the Minimum variable. At last, we will print the output. Hence, we will see the minimum value in the list of strings.

4. using Python min() function in tuple

In this example, we will be taking the input as a tuple. Then, we will try to find the minimum value present in the tuple.

#using tuple as an input

arr = (1, 5, 3, 9)
MIN = min(arr)

print("minimum value : ",MIN)

Output:

minimum value :  1

Explanation:

Here firstly, we will take the input as a tuple. Secondly, we will apply the min() and store the output in the Minimum variable. At last, we will print the output. Hence, we will see the minimum value in the tuple.

5. using min() function in dictionary

In this example, we will be taking the input as a dictionary. Then, we will try to find the minimum value present in the dictionary.

#using a dictionary as input

dict = {1: 4, 2: 9, 3: 1, 4: 4}
MIN = min(dict)

print("Minimum value : ",MIN)

Output:

Minimum value :  1

Explanation:

Here firstly, we will take the input as a dictionary. Secondly, we will apply the min() function and store the output in the MIN variable. At last, we will print the output. Hence, we will see the minimum value in the dictionary.

6. using Python min() function with a default value

In this example, we will be taking the input as an empty list. Then, we will try to find the minimum empty list with the default value.

#using default value

lst = []
x= min(lst, default =20)

print("Minimum value : ",x)

Output:

Minimum value :  20

Explanation:

Here firstly, we will take the input as an empty list as lst= []. Secondly, we will apply the min() function with the default value = 20 and store the x variable’s output. At last, we will print the output. Hence, we will see the minimum value in the empty string with the default value = 20.

7. using min() function with arguments and key function

In this example, we will be using min() with arguments and key function. we will define the function for calculating the string’s length.

#using the key function

def str_length(s):
    return len(s)
lst = ['LAtra', 'abc', 'Solu']
x = min(lst)

print(x, key=str_length))

Output:

abc

Explanation:

Here firstly, we will take the input as a list. Secondly, we will apply the min() function and store the output in the x variable. Thirdly, we will define the def function with str_length(s) as the name of it. Fourthly, we will try to print the min value with the key value = str_length. At last, we will print the output. Hence, we will see the minimum value in the string.

8. using Python min() function with multiple iterables

In this example, we will be using the multiple iterables as the input. Then we will print the minimum value of the iterables.

#using multiple iterables

lst1 = [1, 2, 3]
lst2 = [5, 15, 40, 25, 35, 45]
Minimum = min(lst1, lst2, key = len)
print("Minimum value : ", Minimum)

Output:

Minimum value :  [1, 2, 3]

Explanation:

Here firstly, we will take the input as multiple iterables with two lists as lst1 and lst2. Secondly, we will apply the min() function with both the list and the key-value = len. Thirdly, we will store the output in the Minimum variable. At last, we will print the output. Hence, we will see the minimum value in the tuple.

Conclusion

In this tutorial, we have learned how to calculate the minimum value using the min() python function. All the parameters and types are explained in detail with the help of examples. You can use any data such as list, tuple, dictionary, etc., as required. You can also use different types of parameters as required in the program.

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments