Remember
max() returns the largest item according to comparison or a supplied key. An empty iterable needs a default to avoid an error.
max() returns the largest item according to comparison or a supplied key. An empty iterable needs a default to avoid an error.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
numbers = [3, 2, 5]
print(max(numbers))Top-level scope
[3, 2, 5]max() returns the largest item according to comparison or a supplied key. An empty iterable needs a default to avoid an error.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
numbers = [3, 2, 5]
print(max(numbers))
|
v
DISPLAYED RESULT
5
FINAL BINDINGS
numbers --> object 1 (list)
object 1: [3, 2, 5]Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
numbers = [3, 2, 5]
print(max(numbers))Top-level scope
[3, 2, 5]The example has finished. Compare the final output with the values and references above.
5
What do you expect this program to print?
numbers = [3, 2, 5]
print(max(numbers))max() FunctionThe max() function in Python is used to find the largest item in an iterable (like a list, tuple, set) or among two or more arguments. It’s one of the most common functions used in comparisons, data analysis, and sorting tasks.
max(iterable, *[, key, default])
max(arg1, arg2, *args[, key])
iterable – A list, tuple, set, or any iterable.arg1, arg2, *args – Two or more values to compare.key (optional) – A function to customize sorting logic.default (optional) – A value to return if the iterable is empty.numbers = [5, 12, 3, 8]
print(max(numbers))
12
print(max(10, 25, 3, 99))
99
words = ["apple", "banana", "pear", "mango"]
print(max(words, key=len))
banana
Why? The key=len means it compares word lengths, not alphabetical order.
empty = []
print(max(empty, default=0))
0
key functions)max([]) without default will raise a ValueErrorTypeErrorThe max() function is often used in DSA problems like finding the max subarray, longest word, or highest frequency. Learn how to combine it with key and lambda.
max() returns the largest value from arguments or iterable.key parameter.default to avoid errors on empty iterables.Write a program that takes three numbers from the user and prints the largest one.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("Largest number is:", max(a, b, c))
Connect what you noticed in the example with the idea below.
max() returns the largest item according to comparison or a supplied key. An empty iterable needs a default to avoid an error.
max() always returns the final item of the iterable.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.