Fundamentals 13 min read

Common Python Built‑in Functions with Examples

This article introduces Python's most frequently used built-in functions, such as len, type, print, range, sum, and others, providing clear explanations, practical use cases, and complete code examples to help readers understand and apply these essential tools for efficient programming.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Common Python Built‑in Functions with Examples

Python, as a powerful programming language, provides a rich set of built‑in functions that simplify many tasks ranging from simple data manipulation to complex logical processing. This guide explores the most commonly used built‑in functions, offering concrete code examples and practical scenarios to help you master them.

Table of Contents

Built‑in Functions Overview

Common Built‑in Functions and Examples

Summary

1. Built‑in Functions Overview

Built‑in functions are predefined by the Python language and can be called directly without importing any modules. They cover data type conversion, mathematical operations, string handling, list manipulation, and more, greatly simplifying the programming process.

2. Common Built‑in Functions and Examples

Example 1: len() – Get the length of an object

Use len() to obtain the length of strings, lists, tuples, dictionaries, etc.

# Get string length
text = "Hello, Python!"
length = len(text)
print(f"String length is: {length}")

# Get list length
numbers = [1, 2, 3, 4, 5]
length = len(numbers)
print(f"List length is: {length}")

Output:

String length is: 13
List length is: 5

Analysis: len() returns the length of the given object and works with many data types.

Example 2: type() – Get the type of a variable

Use type() to inspect a variable's data type.

# Check variable types
a = 10
b = "Hello"
c = [1, 2, 3]
print(f"Type of a: {type(a)}")
print(f"Type of b: {type(b)}")
print(f"Type of c: {type(c)}")

Output:

Type of a:
Type of b:
Type of c:

Analysis: type() is useful for debugging and type checking.

Example 3: print() – Output content

# Print a string
print("Hello, World!")
# Print multiple variables
name = "Alice"
age = 25
print(f"Name: {name}, Age: {age}")

Output:

Hello, World!
Name: Alice, Age: 25

Analysis: print() supports various formatting methods for displaying data.

Example 4: range() – Generate a numeric sequence

# Generate numbers 0‑4
for i in range(5):
    print(i, end=" ")

Output:

0 1 2 3 4

Analysis: range() creates sequences from 0 to n‑1 and can accept start and step arguments.

Example 5: sum() – Compute the sum of an iterable

# Sum a list of numbers
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print(f"Total sum is: {total}")

Output:

Total sum is: 15

Analysis: sum() directly adds all numeric elements in an iterable.

Example 6: max() and min() – Find maximum and minimum values

# Get max and min
numbers = [10, 20, 30, 40, 50]
max_value = max(numbers)
min_value = min(numbers)
print(f"Max: {max_value}")
print(f"Min: {min_value}")

Output:

Max: 50
Min: 10

Analysis: Both functions compare elements and return the extreme values.

Example 7: sorted() – Sort an iterable

# Sort a list
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
sorted_numbers = sorted(numbers)
print(f"Sorted list: {sorted_numbers}")

Output:

Sorted list: [1, 1, 2, 3, 4, 5, 6, 9]

Analysis: sorted() returns a new sorted list, leaving the original unchanged.

Example 8: map() – Apply a function to each element

# Square each number
numbers = [1, 2, 3, 4, 5]
squared = map(lambda x: x ** 2, numbers)
print(f"Squared results: {list(squared)}")

Output:

Squared results: [1, 4, 9, 16, 25]

Analysis: map() transforms each element using the provided function.

Example 9: filter() – Filter elements by a condition

# Filter even numbers
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(f"Even numbers: {list(even_numbers)}")

Output:

Even numbers: [2, 4, 6]

Analysis: filter() returns only elements that satisfy the predicate.

Example 10: zip() – Combine iterables

# Combine names and ages
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
combined = zip(names, ages)
print(f"Combined: {list(combined)}")

Output:

Combined: [('Alice', 25), ('Bob', 30), ('Charlie', 35)]

Analysis: zip() pairs elements from multiple iterables into tuples.

Example 11: enumerate() – Add index to an iterable

# Enumerate fruits
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
    print(f"Index: {index}, Fruit: {fruit}")

Output:

Index: 0, Fruit: apple
Index: 1, Fruit: banana
Index: 2, Fruit: cherry

Analysis: enumerate() provides both the index and the element during iteration.

Example 12: input() – Receive user input

# Get user name
name = input("Please enter your name: ")
print(f"Hello, {name}!")

Typical interaction:

Please enter your name: Alice
Hello, Alice!

Analysis: input() reads a line from the console and returns it as a string.

Example 13: int() , float() , str() – Type conversion

# Convert string to int, float, and back to string
text = "123"
number = int(text)
print(f"Integer: {number}")
decimal = float(text)
print(f"Float: {decimal}")
text_again = str(number)
print(f"String again: {text_again}")

Output:

Integer: 123
Float: 123.0
String again: 123

Analysis: These functions allow conversion between basic data types.

Example 14: abs() – Absolute value

# Absolute value of a number
number = -10
absolute_value = abs(number)
print(f"Absolute value of {number} is {absolute_value}")

Output:

Absolute value of -10 is 10

Analysis: abs() returns the non‑negative magnitude of a number.

Example 15: round() – Round a float

# Round to two decimal places
number = 3.14159
rounded_number = round(number, 2)
print(f"Rounded result: {rounded_number}")

Output:

Rounded result: 3.14

Analysis: round() rounds a floating‑point number to the specified number of decimal places.

3. Summary

Python's built‑in functions are indispensable tools that provide rich functionality, enabling developers to accomplish a wide variety of tasks efficiently. By reviewing the examples above, you should now have a deeper understanding of the most useful built‑in functions, allowing you to write cleaner and more effective code.

If you have further questions or topics you would like to explore, feel free to leave a comment for discussion.

code examplesTutorialProgramming Basicsbuilt-in-functions
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.