Python provides several built-in functions to make programming easier. This lesson covers some of the most commonly used functions with examples to help you understand their usage.
sum()
Description: Computes the sum of elements in an iterable.
numbers = [1, 2, 3, 4]
print(sum(numbers)) # Output: 10
map()
Description: Applies a given function to all items in an iterable and returns a new iterable.
numbers = [1, 2, 3]
squared = map(lambda x: x**2, numbers)
print(list(squared)) # Output: [1, 4, 9]
filter()
Description: Filters elements from an iterable based on a condition (returns only elements for which the condition is True
).
numbers = [1, 2, 3, 4, 5]
evens = filter(lambda x: x % 2 == 0, numbers)
print(list(evens)) # Output: [2, 4]
sorted()
Description: Returns a new sorted list from the elements of an iterable.
numbers = [3, 1, 4, 1, 5]
print(sorted(numbers)) # Output: [1, 1, 3, 4, 5]
reduce()
(from functools
module)Description: Applies a function cumulatively to the items of an iterable to reduce it to a single value.
from functools import reduce
numbers = [1, 2, 3, 4]
product = reduce(lambda x, y: x * y, numbers)
print(product) # Output: 24
any()
Description: Returns True
if any element in the iterable is True
.
numbers = [0, 1, 2]
print(any(numbers)) # Output: True
all()
Description: Returns True
if all elements in the iterable are True
.
numbers = [1, 2, 3]
print(all(numbers)) # Output: True
zip()
Description: Combines multiple iterables into a single iterable of tuples.
names = ['Alice', 'Bob']
scores = [90, 85]
zipped = zip(names, scores)
print(list(zipped)) # Output: [('Alice', 90), ('Bob', 85)]
enumerate()
Description: Adds a counter to an iterable and returns it as an enumerate object.
letters = ['a', 'b', 'c']
enumerated = enumerate(letters, start=1)
print(list(enumerated)) # Output: [(1, 'a'), (2, 'b'), (3, 'c')]
reversed()
Description: Returns an iterator that accesses the given sequence in the reverse order.
numbers = [1, 2, 3]
reversed_numbers = reversed(numbers)
print(list(reversed_numbers)) # Output: [3, 2, 1]
max()
Description: Returns the largest element from an iterable.
numbers = [1, 2, 3]
print(max(numbers)) # Output: 3
min()
Description: Returns the smallest element from an iterable.
numbers = [1, 2, 3]
print(min(numbers)) # Output: 1
len()
Description: Returns the number of items in an object.
letters = ['a', 'b', 'c']
print(len(letters)) # Output: 3
type()
Description: The type()
function returns the type of the specified object. It is a very useful function for debugging and understanding the types of variables or objects in Python.
type(object)
print(type(10)) # Output: <class 'int'>
print(type("Hello")) # Output: <class 'str'>
print(type([1, 2, 3])) # Output: <class 'list'>
You can also use type()
to create new types dynamically (though this is an advanced feature).
Objective: Use Python's built-in functions to solve problems.
sum()
and len()
to calculate the average of a list of numbers.numbers = [10, 20, 30, 40]
average = sum(numbers) / len(numbers)
print(average) # Output: 25.0
filter()
to extract only the odd numbers from a list.numbers = [1, 2, 3, 4, 5]
odds = filter(lambda x: x % 2 != 0, numbers)
print(list(odds)) # Output: [1, 3, 5]
zip()
to combine two lists: names and ages.names = ['Alice', 'Bob']
ages = [25, 30]
print(dict(zip(names, ages))) # Output: {'Alice': 25, 'Bob': 30}
reduce()
to compute the cumulative product of a list.from functools import reduce
numbers = [1, 2, 3, 4]
product = reduce(lambda x, y: x * y, numbers)
print(product) # Output: 24
max()
to find the maximum value in a list of dictionaries based on a key.data = [{'name': 'Alice', 'score': 90}, {'name': 'Bob', 'score': 85}]
top_scorer = max(data, key=lambda x: x['score'])
print(top_scorer) # Output: {'name': 'Alice', 'score': 90}