
How to find maximum value in Python
In this lesson I'll explain how to find the maximum value in a set of numbers. In the python language you can use the max() function.
max(x)
The x parameter is a list of numbers or an iterable object. For example a list, a tuple, a dictionary, etc.
How does it work
The max() function iterates through all elements returning the maximum value as output.
Note. The iterable object must contain all numeric elements. Otherwise, the function returns an error.
I'll give you a practical example.
In this script the max() function looks for and finds the highest number in the list.
n=[3,4,7,1,5,8]
vm=max(n)
print(vm)
The maximum value is assigned to the variable vm
The output result is the following
8
The function found the maximum value in the list.
I'll give you another example
In this script, the max() function looks for the maximum value in a tuple
vm=max(3,4,7,1,5,8)
print(vm)
The numeric values are listed one after the other and separated from each other by a comma.
The output of the script is as follows:
8
The function found the maximum value in the tuple.