Built-in Functions

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.


1. sum()

Description: Computes the sum of elements in an iterable.

Example

numbers = [1, 2, 3, 4]
print(sum(numbers))  # Output: 10

2. map()

Description: Applies a given function to all items in an iterable and returns a new iterable.

Example

numbers = [1, 2, 3]
squared = map(lambda x: x**2, numbers)
print(list(squared))  # Output: [1, 4, 9]

3. filter()

Description: Filters elements from an iterable based on a condition (returns only elements for which the condition is True).

Example

numbers = [1, 2, 3, 4, 5]
evens = filter(lambda x: x % 2 == 0, numbers)
print(list(evens))  # Output: [2, 4]

4. sorted()

Description: Returns a new sorted list from the elements of an iterable.

Example

numbers = [3, 1, 4, 1, 5]
print(sorted(numbers))  # Output: [1, 1, 3, 4, 5]

5. reduce() (from functools module)

Description: Applies a function cumulatively to the items of an iterable to reduce it to a single value.

Example

from functools import reduce
numbers = [1, 2, 3, 4]
product = reduce(lambda x, y: x * y, numbers)
print(product)  # Output: 24

6. any()

Description: Returns True if any element in the iterable is True.

Example

numbers = [0, 1, 2]
print(any(numbers))  # Output: True

7. all()

Description: Returns True if all elements in the iterable are True.

Example

numbers = [1, 2, 3]
print(all(numbers))  # Output: True

8. zip()

Description: Combines multiple iterables into a single iterable of tuples.

Example

names = ['Alice', 'Bob']
scores = [90, 85]
zipped = zip(names, scores)
print(list(zipped))  # Output: [('Alice', 90), ('Bob', 85)]

9. enumerate()

Description: Adds a counter to an iterable and returns it as an enumerate object.

Example

letters = ['a', 'b', 'c']
enumerated = enumerate(letters, start=1)
print(list(enumerated))  # Output: [(1, 'a'), (2, 'b'), (3, 'c')]

10. reversed()

Description: Returns an iterator that accesses the given sequence in the reverse order.

Example

numbers = [1, 2, 3]
reversed_numbers = reversed(numbers)
print(list(reversed_numbers))  # Output: [3, 2, 1]

11. max()

Description: Returns the largest element from an iterable.

Example

numbers = [1, 2, 3]
print(max(numbers))  # Output: 3

12. min()

Description: Returns the smallest element from an iterable.

Example

numbers = [1, 2, 3]
print(min(numbers))  # Output: 1

13. len()

Description: Returns the number of items in an object.

Example

letters = ['a', 'b', 'c']
print(len(letters))  # Output: 3

14. 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.

Syntax

type(object)

Example

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).


Task

Practice: Built-in Functions in Python

Objective: Use Python's built-in functions to solve problems.

  1. Calculate the Average: Use 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
  1. Filter Odd Numbers: Use 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]
  1. Combine Names and Ages: Use zip() to combine two lists: names and ages.
names = ['Alice', 'Bob']
ages = [25, 30]
print(dict(zip(names, ages)))  # Output: {'Alice': 25, 'Bob': 30}
  1. Cumulative Product: Use 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
  1. Find Maximum Value: Use 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}

Copyright © 2025 Devship. All rights reserved.

Made by imParth