Mastering Common Python Built-in Functions: len(), max(), min(), sum(), sorted(), enumerate(), zip() | Python tutorials on BeingSkilled

Python provides a powerful set of built-in functions that make everyday tasks easier, more readable, and faster to write. Among the most frequently used are len(), max(), min(), sum(), sorted(), enumerate(), and zip(). These functions work with sequences like lists, tuples, and strings to help you manipulate and analyze data efficiently.

Let’s explore each of these functions with clear explanations and examples.

1. len()

The len() function returns the number of items in a sequence (string, list, tuple, etc.).

# Example 1: len()
names = ["Ravi", "Meera", "Ajay"]
print(len(names))  # Output: 3

text = "Python"
print(len(text))  # Output: 6

2. max()

max() returns the largest item in an iterable or the largest of two or more arguments.

# Example 2: max()
numbers = [10, 25, 17]
print(max(numbers))  # Output: 25

print(max("apple", "banana", "cherry"))  # Output: cherry

3. min()

min() returns the smallest item in an iterable or the smallest of two or more arguments.

# Example 3: min()
values = [3, 8, 1, 9]
print(min(values))  # Output: 1

print(min("zebra", "elephant", "dog"))  # Output: dog

4. sum()

sum() adds all the items in an iterable. It only works with numeric data.

# Example 4: sum()
prices = [100, 200, 300]
print(sum(prices))  # Output: 600

# With a starting value
print(sum(prices, 50))  # Output: 650

5. sorted()

sorted() returns a new sorted list from the items of any iterable. The original data remains unchanged.

# Example 5: sorted()
scores = [88, 45, 67, 99]
print(sorted(scores))         # Output: [45, 67, 88, 99]
print(sorted(scores, reverse=True))  # Output: [99, 88, 67, 45]

6. enumerate()

enumerate() adds a counter to an iterable and returns it as an enumerate object (which you can loop over).

# Example 6: enumerate()
colors = ["Red", "Green", "Blue"]
for index, color in enumerate(colors):
    print(index, color)

# Output:
# 0 Red
# 1 Green
# 2 Blue

7. zip()

zip() combines two or more iterables element-wise into tuples. It stops when the shortest iterable is exhausted.

# Example 7: zip()
names = ["Amit", "Reena", "Karan"]
marks = [85, 90, 75]

for name, mark in zip(names, marks):
    print(f"{name} scored {mark}")

# Output:
# Amit scored 85
# Reena scored 90
# Karan scored 75

8. Combined Usage Example

You can combine several of these functions for practical use.

# Example 8: Combine zip, sorted, enumerate
students = ["Neha", "Raj", "Simran"]
grades = [88, 74, 93]

# Sort students by grades in descending order
for i, (name, grade) in enumerate(sorted(zip(students, grades), key=lambda x: x[1], reverse=True), start=1):
    print(f"{i}. {name}: {grade}")

# Output:
# 1. Simran: 93
# 2. Neha: 88
# 3. Raj: 74

9. Summary Table

Function Description Example
len() Returns length of a sequence len([1, 2, 3])
max() Returns the largest item max([3, 7, 2])
min() Returns the smallest item min([3, 7, 2])
sum() Returns the sum of items sum([10, 20])
sorted() Returns a sorted list sorted([4, 2, 9])
enumerate() Returns index-item pairs enumerate(['a', 'b'])
zip() Combines iterables zip([1,2], ['a','b'])

10. Final Thoughts

These built-in functions—len(), max(), min(), sum(), sorted(), enumerate(), and zip()—are the foundation for writing efficient and elegant Python code. They help reduce complexity, improve performance, and keep your code clean. As you get more comfortable with Python, you’ll find yourself using them regularly across all kinds of projects.