In Python, the max()
is used to find the highest item in an iterable. An iterable is an object of any type that can be iterated using a loop. For example, list or tuple. Iterables can have any type of value such as integers, strings, and so on. We pass our iterable to max()
method in Python and it returns the highest value. It can also be used to return the highest value between two values.
max(n1, n2, n3, ...)
Or
max(iterable)
The max()
function accepts an iterable as a parameter to find its maximum value.
The max()
function in python returns the highest value of the provided iterable.
These examples show the use of max()
function in Python.
# create a listscores = [50, 20, 9, 70, 45, 90, 5]# call max() function and store resultresult = max(scores)# display resultprint(f"The highest score : {result}")
scores
.max()
function to find the highest value from the scores
and store the highest value in result
variable.This example shows the use of the max()
function to find the highest string in a list.
Note: The function checks the list alphabetically
# create a listcolors = ['Red','Orange', 'Blue', 'Pink']# store resultresult = max(colors)# display resultprint(f"The highest color: {result}")
colors
.max()
function to find the highest value from the colors
and store the highest value in result
variable.