Latest Code Tutorials

Python Min and Max Inbuilt Function Example


Python min() is an inbuilt function that returns the smallest of all the input values. Python max() is an inbuilt function that returns the largest of the input values. If the min() method is called on an iterable, then it returns the smallest item from that iterable object. If the iterable object is empty then the default value is returned if it is provided. Otherwise, the ValueError exception is raised.

Python Min and Max Function Example

If the min() function is called with the multiple arguments, it returns the smallest one. Same for the max() method, if it is called with multiple arguments, then it returns the largest one.

See the syntax of the Python min() function.

min(iterable[, default=obj, key=func])

In the above function, one argument is required which is an iterable object.

Let’s see the following example.


listA = [18, 19, 21, 22]
print('The smallest number from listA is:',min(listA))
print('The largest number from listA is:',max(listA))

The output of the above code is following.

Python Min and Max Built-in Function Tutorial

Python min and max method for String

Let’s take a scenario where we find the min and max value for the String object.

Write the following code inside an file.


strA = 'AppDividend'
print('The smallest character from strA is:', min(strA))
print('The largest character from strA is:', max(strA))

See the output.

Python min and max method for String

In the String, the min function and max function will see the ASCII value of each character on the screen.

Whose ASCII value is largest will print that character and whose ASCII character is lowest will print that character.

Multiple Arguments in min and max Function

Let’s take another example of String.

In this demo, we will provide multiple arguments to the min and max function and see the output.


strA = 'AppDividend'
strB = 'Facebook'
strC = 'Amazon'
print('The smallest string is:', min(strA, strB, strC))
print('The largest string is:', max(strA, strB, strC))

See the output.

Multiple Arguments in min and max Function

The reason for Amazon is the smallest string is because the ASCII value of A is 65 which is lowest compared to all the other Alphabetical characters.

But, also AppDividend is starting from A also. Then it will check the second character from both of the String.

In the case of Amazon, it is m, and in the case of AppDividend, it is p. That means p is greater than a and hence the Amazon has the lowest value. 

Facebook has the highest value because F is greater than A regarding their ASCII values.

You can find any character’s ASCII value by using ord() function in Python.

Finally, Python Min and Max Built-in Function Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.