min, max, sum

Aggregate functions

Interview Relevant: Basic operations

min, max, sum

Aggregate functions for sequences.

Code Examples

Aggregate functions.

python
1nums = [3, 1, 4, 1, 5, 9]
2
3# Basic usage
4min(nums)  # 1
5max(nums)  # 9
6sum(nums)  # 23
7
8# With key function
9words = ["apple", "banana", "cherry"]
10min(words, key=len)  # "apple"
11max(words, key=len)  # "banana"
12
13# Default for empty
14min([], default=0)  # 0 (no error)
15
16# Multiple arguments
17min(3, 1, 4)  # 1
18max(3, 1, 4)  # 9
19
20# Sum with start value
21sum([1, 2, 3], 10)  # 16 (10 + 1 + 2 + 3)
22
23# Don't use sum for strings!
24# "".join(strings)  # Use join instead

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In