Common Python Built-in Functions: len, sum, max/min, sorted, enumerate, zip, map, filter
This article introduces essential Python built-in functions such as len, sum, max/min, sorted, enumerate, zip, map, and filter, explaining their syntax, providing practical examples, and offering best‑practice tips for effective use in everyday programming.
len() returns the length of an object like a string, list, tuple, or dictionary.
Syntax:
len(object)Examples:
print(len("hello")) # 输出: 5 print(len([1, 2, 3])) # 输出: 3 print(len({"a": 1, "b": 2})) # 输出: 2sum() adds all numbers in an iterable, optionally starting from a given value.
Syntax:
sum(iterable, start=0)Examples:
numbers = [1, 2, 3, 4] print(sum(numbers)) # 输出: 10 print(sum(numbers, 10)) # 输出: 20max() and min() return the maximum or minimum value from given arguments or an iterable.
Syntax:
max(iterable, *[, key, default]) max(arg1, arg2, *args[, key]) min(iterable, *[, key, default]) min(arg1, arg2, *args[, key])Examples:
numbers = [1, 2, 3, 4] print(max(numbers)) # 输出: 4 print(min(numbers)) # 输出: 1 # 使用 key 参数进行自定义排序 words = ["apple", "banana", "cherry"] print(max(words, key=len)) # 输出: "banana" print(min(words, key=len)) # 输出: "apple"sorted() returns a new sorted list from any iterable without modifying the original.
Syntax:
sorted(iterable, *, key=None, reverse=False)Examples:
numbers = [3, 1, 4, 1, 5, 9] print(sorted(numbers)) # 输出: [1, 1, 3, 4, 5, 9] # 按长度排序字符串列表 words = ["apple", "banana", "cherry"] print(sorted(words, key=len)) # 输出: ['apple', 'cherry', 'banana'] # 反向排序 print(sorted(numbers, reverse=True)) # 输出: [9, 5, 4, 3, 1, 1]enumerate() yields pairs of index and value while iterating over a sequence.
Syntax:
enumerate(iterable, start=0)Example:
fruits = ["apple", "banana", "cherry"] for index, fruit in enumerate(fruits): print(f"Index {index}: {fruit}") # 输出: # Index 0: apple # Index 1: banana # Index 2: cherryzip() aggregates elements from multiple iterables into tuples until the shortest iterable is exhausted.
Syntax:
zip(*iterables)Example:
names = ["Alice", "Bob", "Charlie"] ages = [25, 30, 35] zipped = zip(names, ages) for name, age in zipped: print(f"{name} is {age} years old.") # 输出: # Alice is 25 years old. # Bob is 30 years old. # Charlie is 35 years old.map() applies a function to every item of an iterable and returns an iterator of results.
Syntax:
map(function, iterable, ...)Example:
numbers = [1, 2, 3, 4] squared = map(lambda x: x**2, numbers) print(list(squared)) # 输出: [1, 4, 9, 16]filter() constructs an iterator from elements of an iterable for which a function returns true.
Syntax:
filter(function, iterable)Example:
numbers = [1, 2, 3, 4, 5, 6] evens = filter(lambda x: x % 2 == 0, numbers) print(list(evens)) # 输出: [2, 4, 6]Best Practices and Tips
Use sorted() for flexible ordering, including custom keys and reverse sorting.
Combine enumerate() and zip() to simplify loops that need indices or parallel sequences.
Leverage map() and filter() for functional programming, remembering they return iterators in Python 3, so wrap with list() when a list is needed.
Consider list comprehensions as a more readable alternative for simple mapping or filtering tasks.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.