The max() function in Python is a built-in function used to find the largest element among a group of values. It can compare multiple values or find the maximum value from an iterable object such as a list, tuple, set, or string.
The function examines the given elements and returns the maximum (largest) value.
The max() function is commonly used in programs where we need to determine the highest number, maximum score, or largest value in a dataset.
Syntax 1: Using multiple arguments
max(value1, value2, value3, ...)
Syntax 2: Using an iterable
max(iterable, key=None)
value1, value2, ... Two or more values to compare.
iterable A sequence such as a list, tuple, or set whose largest element needs to be found.
key (optional) A function that specifies the rule for comparison.
print(max(10, 5, 20, 3))
Output
20
Explanation
numbers = [8, 2, 15, 4, 10]
print(max(numbers))
Output
15
Explanation
numbers = (12, 7, 25, 5)
print(max(numbers))
Output
25
Explanation
text = "python"
print(max(text))
Output
y
Explanation
'y' is the largest.key Parameterwords = ["apple", "banana", "kiwi", "cherry"]
print(max(words, key=len))
Output
banana
Explanation
key=len argument tells Python to compare words based on their length."banana" has the greatest length.marks = [85, 90, 78, 88]
highest = max(marks)
print("Highest Marks:", highest)
Output
Highest Marks: 90
Explanation
max() is a built-in Python function used to find the largest value.key parameter for custom comparison.