Fundamentals 5 min read

Common Python Built-in Functions and Their Usage

This article introduces a collection of essential Python built-in functions—such as print, len, type, range, sum, sorted, abs, round, input, any/all, zip, enumerate, filter, map, reversed, and setdefault—explaining their typical use cases and providing concise code examples for each.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Common Python Built-in Functions and Their Usage

print() – Used to output information to the console, helpful for debugging.

print("Hello, Python世界!")

len() – Calculates the length of a sequence or collection.

my_list = [1, 2, 3, 4, 5]
print(len(my_list))  # 输出 5

type() – Returns the data type of a variable.

x = 10
print(type(x))  # 输出

range() – Generates a numeric sequence, commonly used in for‑loops.

for i in range(5):
print(i)

sum() – Quickly computes the sum of an iterable of numbers.

numbers = [1, 2, 3, 4, 5]
print(sum(numbers))  # 输出 15

sorted() – Returns a new list containing all items from the iterable in ascending order.

unsorted_list = [3, 1, 4, 1, 5, 9]
print(sorted(unsorted_list))  # 输出 [1, 1, 3, 4, 5, 9]

abs() – Returns the absolute (non‑negative) value of a number.

print(abs(-42))  # 输出 42

round() – Rounds a floating‑point number to a given number of decimal places.

print(round(3.14159, 2))  # 输出 3.14

input() – Reads a line of text entered by the user.

user_input = input("请输入你的名字:")
print("你好," + user_input + "!")

any() & all() – Test whether any or all elements of an iterable satisfy a condition.

list1 = [0, False, None]
list2 = [1, True, "Yes"]
print(any(list1))  # 输出 False
print(all(list2))  # 输出 True

zip() – Combines multiple iterables element‑wise into tuples.

names = ["Alice", "Bob", "Charlie"]
ages = [24, 30, 22]
paired_data = zip(names, ages)
print(list(paired_data))  # 输出 [('Alice', 24), ('Bob', 30), ('Charlie', 22)]

enumerate() – Provides both index and value when iterating over a sequence.

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

filter() – Filters elements of an iterable based on a predicate function.

numbers = [1, 2, 3, 4, 5, 6]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers))  # 输出 [2, 4, 6]

map() – Applies a function to every item of an iterable and returns an iterator of the results.

squares = map(lambda x: x**2, range(5))
print(list(squares))  # 输出 [0, 1, 4, 9, 16]

reversed() – Returns an iterator that yields the items of the sequence in reverse order.

original_list = [1, 2, 3, 4, 5]
reversed_list = list(reversed(original_list))
print(reversed_list)  # 输出 [5, 4, 3, 2, 1]

setdefault() – Retrieves a value for a key in a dictionary, inserting a default if the key is absent.

my_dict = {'a': 1, 'b': 2}
value = my_dict.setdefault('c', 3)  # 'c'不存在,设置默认值3
print(value)  # 输出 3
print(my_dict)  # 输出 {'a': 1, 'b': 2, 'c': 3}
Pythoncode examplesprogramming fundamentalsbuilt-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.