max()

Math

Returns the largest item in an iterable or the largest of two or more arguments.

Signature

max(iterable, *, key, default)

Returns

any

Example

print(max([3, 1, 4, 1, 5]))  # 5
print(max('hello'))           # 'o'
words = ['cat', 'elephant', 'dog']
print(max(words, key=len))    # 'elephant'

About max()

max is a Python math function with the signature max(iterable, *, key, default). Returns the largest item in an iterable or the largest of two or more arguments. It returns a value of type any.

Python provides a rich set of built-in functions and standard library modules that cover common programming tasks. Understanding these functions helps you write more idiomatic, efficient Python code. The maxfunction is commonly used in data processing, web development, scripting, and automation tasks.

When working with max(), consider edge cases like empty inputs, None values, and type mismatches. Python's duck typing means many built-in functions work with any object that implements the required protocol (e.g., __len__ for len(), __iter__ for iteration). This flexibility is a key strength of Python's design philosophy.

Related Functions