max() and min() in Python

Finding maximum and minimum values from a given list of values is a very common need in data processing programs. Python provides built-in max() and min() functions that handle both numbers and strings efficiently.

Syntax

max(iterable, *[, key, default])
min(iterable, *[, key, default])

Using max() and min() with Numeric Values

Both functions work with integers, floats, and mixed numeric types to find the maximum and minimum values ?

numbers = [10, 15, 25.5, 3, 2, 9/5, 40, 70]
print("Maximum number is:", max(numbers))
print("Minimum number is:", min(numbers))
Maximum number is: 70
Minimum number is: 1.8

Using max() and min() with String Values

String values are compared lexicographically based on ASCII values. The comparison is done character by character ?

days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
print("Maximum string is:", max(days))
print("Minimum string is:", min(days))
Maximum string is: Wed
Minimum string is: Fri

Using with Multiple Arguments

You can also pass multiple arguments directly instead of using a list ?

print("Max of multiple values:", max(15, 8, 42, 3))
print("Min of multiple values:", min(15, 8, 42, 3))
Max of multiple values: 42
Min of multiple values: 3

Using the key Parameter

The key parameter allows custom comparison logic. Here we find the longest and shortest strings ?

words = ['Python', 'Java', 'C', 'JavaScript', 'Go']
longest = max(words, key=len)
shortest = min(words, key=len)

print("Longest word:", longest)
print("Shortest word:", shortest)
Longest word: JavaScript
Shortest word: C

Comparison

Data Type Comparison Method Example Result
Numbers Numerical value max([1, 5, 3]) = 5
Strings Lexicographical (ASCII) max(['a', 'z', 'b']) = 'z'
Custom key Based on key function max(['cat', 'elephant'], key=len) = 'elephant'

Conclusion

Python's max() and min() functions provide flexible ways to find extreme values in collections. Use the key parameter for custom comparison logic when working with complex data structures.

Updated on: 2026-03-15T17:19:52+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements