Computer >> Computer tutorials >  >> Programming >> Python

max() and min() in Python


Finding maximum and minimum values from a given list of values is a very common need in data processing programs. Python has these two functions which handle both numbers and strings. We will see both the scenarios in the below examples.

Numeric Values

We take a list of numeric values which has integers and floats. The functions work appropriately to give both the max and the min values.

Example

x=[10,15,25.5,3,2,9/5,40,70]
print("Maximum number is :",max(x))
print("\nMinimum number is :",min(x))

Output

Running the above code gives us the following result:

Maximum number is : 70
Minimum number is : 1.8

String Values

The string values are compared lexicographically to get the correct result. Internally the ascii values of the characters are compared.

Example

x=['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
print("Maximum number is :",max(x))
print("\nMinimum number is :",min(x))

Output

Running the above code gives us the following result:

Maximum number is : Wed
Minimum number is : Fri