Notes

`max()` Function in Python [ English ]

< Prev Next >

1. Introduction

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.

2. Syntax

Syntax 1: Using multiple arguments

max(value1, value2, value3, ...)

Syntax 2: Using an iterable

max(iterable, key=None)

3. Parameters

  1. value1, value2, ... Two or more values to compare.

  2. iterable A sequence such as a list, tuple, or set whose largest element needs to be found.

  3. key (optional) A function that specifies the rule for comparison.

4. Return Value

5. Basic Example

print(max(10, 5, 20, 3))

Output

20

Explanation

6. Example with a List

numbers = [8, 2, 15, 4, 10]

print(max(numbers))

Output

15

Explanation

7. Example with a Tuple

numbers = (12, 7, 25, 5)

print(max(numbers))

Output

25

Explanation

8. Example with a String

text = "python"

print(max(text))

Output

y

Explanation

9. Example Using key Parameter

words = ["apple", "banana", "kiwi", "cherry"]

print(max(words, key=len))

Output

banana

Explanation

10. Practical Example

marks = [85, 90, 78, 88]

highest = max(marks)

print("Highest Marks:", highest)

Output

Highest Marks: 90

Explanation

11. Summary

< Prev Next >