The max() function returns the largest item in an iterable or the largest of two or more positional arguments.

ExampleEdit & Run
print(max([10, 5, 20, 50, 25]))

print(max(2, 8, 4, 0, 6))
Output:
508[Finished in 0.010695239994674921s]
Syntax:
max(iterable, *args, key = None, default = None) 
iterable An iterable object such as a list, tuple, set, etc  containing the elements to be compared. It is only necessary if the elements are not given as positional arguments.
args Arbitrary more than two values given as positional arguments. It is only necessary if the elements to be compared are not given as an iterable.
key A callable object such as a function or a lambda function to be used as the comparison criteria
default A default value that will be returned in case the iterable is empty. It is only relevant if the elements are in an iterable and not as positional arguments.

Note The iterable argument and the *args arguments are exclusive. If one is given the other one is omitted.

Parameters:

The function returns the largest item in the iterable or the largest of the given positional arguments.

ExampleEdit & Run
print( max([7, 4, 3, 8, 5]) )#using an iterable

print( max(7, 4, 3, 8, 5) )#using positional arguments

print( max(1, 0.8, 1.5, 1.2, 1.5, 0) )

print( max({'c', 'a', 'b'}) )
Output:
881.5c[Finished in 0.010759219992905855s]

Due to in-comparability of some data types, the max() function ( as well as other related functions such as min()  and sorting functions such as sorted()), is only used with items of homogenous or closely related types. For example; while we can tell which is greater between 1(int) and 1.5(float), it would be impossible to tell logically what is larger between a number and a string e.g 'a' and 1, passing such kind of items to the max() function will raise a  TypeError.

ExampleEdit & Run
max(1, 'a') # TypeError

max(1, 2, (3, 4)) #TypeError
Output:
Traceback (most recent call last):  File "<string>", line 1, in <module>TypeError: '>' not supported between instances of 'str' and 'int'[Finished in 0.010326626943424344s]

The optional keyword argument, default, is used to specify a default value which will be returned in case the iterable is empty. If the default value is not specified, a ValueError is raised.

ExampleEdit & Run
L = []

max(L, default = 'Empty')

max(L) #ValueError
Output:
Traceback (most recent call last):  File "<string>", line 5, in <module>ValueError: max() arg is an empty sequence[Finished in 0.010351746110245585s]

The optional keyword argument, key,  specifies the function which will be used as the comparison criteria. The function specified as the key gets applied to all the items and the resulting values are used for comparison instead of the elements themselves. For example if we want to get the list with the largest number of items in a two dimensional list, we can pass the len function as the key, and the lists will be compared based on their lengths.

ExampleEdit & Run
L = [[1, 2], [2, 4, 6, 8, 10], [10, 15, 20], [3, 5, 7, 11]]

print( max(L) )

print( max(L, key = len) )#with len as the key

print( max([-11, 7, 0, 9, 5], key = abs) )
Output:
[10, 15, 20][2, 4, 6, 8, 10]-11[Finished in 0.010065926006063819s]

The lambda functions are commonly used to create inline keys for the max() and related functions.

ExampleEdit & Run
L = [('C++', 9), ('Python', 11), ('CSS', 6), ('Java', 8)]

print( max(L, key = lambda x: x[1]) )

print( max(L, key=lambda x: -x[1]) )#use the max function to get the smallest value
Output:
('Python', 11)('CSS', 6)[Finished in 0.009562036022543907s]