Min and Max Function in Python

Intro

Python has so many built-in functions. These are really helpful. In this article, we will know about two useful built-in functions.

Before getting into the main topic, let’s see two examples first.

Code for finding the largest number out of three.

def find_max(num1, num2, num3):
	if num1 > num2:
		if num1 > num3:
			print(num1)
		else:
			print(num3)
	else:
		if num2 > num3:
			print(num2)
		else:
			print(num3)
		
find_max(5, 10, 3) # 10

Code for finding the smallest number out of three.

def find_min(num1, num2, num3):
	if num1 < num2:
		if num1 < num3:
			print(num1)
		else:
			print(num3)
	else:
		if num2 < num3:
			print(num2)
		else:
			print(num3)
		
find_min(1, 2, 0) # 0

Look at the previous two code examples. To find the largest or smallest number from three, how many codes we have to write. If we have to find the largest or smallest manually from more than three numbers? For that, we have to write more than the previous.

In Python, we can solve these types of problems more easily. Let’s see about that.

Common Concept

min() and max() functions take either iterable or independent value as a parameter. We can pass an iterable to get the result. We also can pass one or more values as parameters. These two function names are self-explanatory. So I hope you can easily guess what these actually do.

min()

This function returns the minimum value. In our previous example, we wrote code to find the smallest number out of three. But we can get the same result using this function by writing much less code. Not only three, but we can also find the smallest number from more than three numbers. Let’s see.

result = min(1, 2, 0)
print(result) # 0

result = min(1, 2, 3, 0, 4, 5)
print(result) # 0

This is really less and easy to understand. We can also pass the numbers through an iterable. And we will get the same result. Example below.

lst = [1, 2, 3, 0, 4, 5]
result = min(lst)
print(result) # 0

We can also pass a string as a parameter. In the case of the string, this function gives results based on ASCII values. Let’s see some examples.

# Example 1

result = min('abcd')
print(result) # a

# Example 2

lst = ['e', 'f', 'g', 'h']
result = min(lst)
print(result) # e

# Example 3

lst = ['Hello', 'World']
result = min(lst)
print(result) # Hello

max()

If you know about the min function, this one is not a big deal. This function returns the maximum value. I am not going to repeat similar explanations. Just look at the example. I hope you will get the idea.

# Example 1

result = max(5, 10, 3)
print(result) # 10

# Example 2

lst = [10, 15, 20, 100, 50]
result = max(lst)
print(result) # 100

# Example 3

lst = ['Hello', 'World']
result = max(lst)
print(result) # World

We can also pass a keyword argument in these functions. For example, we have a string array. And we want to get the largest string regarding the basis of length. We can get this result using keyword argument. Let’s see the example.

lst = ['Python', 'is', 'Awesome']
result1 = max(lst, key=len)
result2 = min(lst, key=len)

print(result1) # Awesome
print(result2) # is

To know more about keyword argument, you can visit Python’s official documentation.

Conclusion

This was the simple overview of the min and max function in Python. One is for getting the minimum value, and the other is for getting the maximum value. By this simple code, or you can say simple functions, we can achieve a pretty beautiful result.

I hope this article helped you. Keep learning.

Scroll to Top