AppDividend
Latest Code Tutorials

Python Min and Max Built-in Function Tutorial With Example

0

Python Min and Max Built-in Function Tutorial With Example is today’s topic. The min() function returns the smallest of all the input values. The max() function 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 Built-in Function Tutorial

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.

The syntax of Python min() function is following.

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.

# app.py

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 String object. Write the following code inside an app.py file.

# app.py

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

The output of the above code is following.

 

Python min and max method for String

In the String, the min function and max function will see the ASCII value of each character in 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 the multiple arguments to the min and max function and see the output.

# app.py

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

The output is following.

 

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. 

The 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.