The Python max() function returns the largest item in an iterable. It can also be used to find the largest item between two or more parameters.
The max() function has two forms:
// to find the largest item in an iterable
max(iterable, *iterables, key, default)
// to find the largest item between two or more objects
max(arg1, arg2, *args, key)
1. max() with iterable arguments
To find the largest item in an iterable, we use this syntax:
max(iterable, *iterables, key, default)
max() Parameters
- iterable - an iterable such as list, tuple, set, dictionary, etc.
- *iterables (optional) - any number of iterables; can be more than one
- key (optional) - key function where the iterables are passed and comparison is performed based on its return value
- default (optional) - default value if the given iterable is empty
Example :
number = [3, 2, 8, 5, 10, 6]
largest_number = max(number);
print("The largest number is:", largest_number)
Output:
The largest number is: 10