Fundamentals 11 min read

36 Frequently Used Python Built-in Functions with Usage Scenarios and Code Examples

This article presents 36 commonly used Python built‑in functions, describing each function’s purpose, typical use cases such as debugging, data conversion, iteration, and includes concise code snippets demonstrating how to apply them, helping readers improve programming efficiency and understanding.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
36 Frequently Used Python Built-in Functions with Usage Scenarios and Code Examples

print() prints output, useful for debugging, logging, and displaying information. Example: print("Hello, World!")

len() returns the length or number of items of an object, such as strings, lists, tuples, or dictionaries. Example: my_list = [1, 2, 3, 4, 5] print(len(my_list)) # 输出: 5

type() returns the type of an object, useful for checking variable types. Example: x = 10 print(type(x)) # 输出:

str() converts an object to a string, e.g., numbers or lists. Example: num = 123 print(str(num)) # 输出: '123'

int() converts an object to an integer, useful for strings or floats. Example: num_str = "123" print(int(num_str)) # 输出: 123

float() converts an object to a floating‑point number. Example: num_str = "123.45" print(float(num_str)) # 输出: 123.45

bool() converts an object to a boolean value. Example: empty_list = [] print(bool(empty_list)) # 输出: False

list() converts an iterable to a list. Example: my_tuple = (1, 2, 3) print(list(my_tuple)) # 输出: [1, 2, 3]

tuple() converts an iterable to a tuple. Example: my_list = [1, 2, 3] print(tuple(my_list)) # 输出: (1, 2, 3)

dict() creates a dictionary from a sequence of key‑value pairs. Example: my_list = [('name', 'Alice'), ('age', 30)] print(dict(my_list)) # 输出: {'name': 'Alice', 'age': 30}

set() converts an iterable to a set, useful for deduplication. Example: my_list = [1, 2, 2, 3, 4, 4] print(set(my_list)) # 输出: {1, 2, 3, 4}

range() generates an integer sequence, often used in loops. Example: for i in range(5): print(i) # 输出: 0 1 2 3 4

enumerate() yields index‑value pairs while iterating. Example: my_list = ['a', 'b', 'c'] for index, value in enumerate(my_list): print(index, value) # 输出: 0 a 1 b 2 c

zip() aggregates elements from multiple iterables into tuples. Example: names = ['Alice', 'Bob', 'Charlie'] ages = [25, 30, 35] for name, age in zip(names, ages): print(name, age) # 输出: Alice 25 Bob 30 Charlie 35

map() applies a function to each item of an iterable. Example: numbers = [1, 2, 3, 4, 5] squared = map(lambda x: x**2, numbers) print(list(squared)) # 输出: [1, 4, 9, 16, 25]

filter() filters elements of an iterable based on a predicate. Example: numbers = [1, 2, 3, 4, 5] even_numbers = filter(lambda x: x % 2 == 0, numbers) print(list(even_numbers)) # 输出: [2, 4]

reduce() performs cumulative computation on an iterable (requires functools ). Example: from functools import reduce numbers = [1, 2, 3, 4, 5] product = reduce(lambda x, y: x * y, numbers) print(product) # 输出: 120

sorted() returns a sorted list from an iterable. Example: numbers = [3, 1, 4, 1, 5, 9] sorted_numbers = sorted(numbers) print(sorted_numbers) # 输出: [1, 1, 3, 4, 5, 9]

reversed() returns an iterator that accesses the given sequence in reverse order. Example: my_list = [1, 2, 3, 4, 5] reversed_list = list(reversed(my_list)) print(reversed_list) # 输出: [5, 4, 3, 2, 1]

sum() computes the total of an iterable of numbers. Example: numbers = [1, 2, 3, 4, 5] total = sum(numbers) print(total) # 输出: 15

min() returns the smallest item of an iterable. Example: numbers = [1, 2, 3, 4, 5] minimum = min(numbers) print(minimum) # 输出: 1

max() returns the largest item of an iterable. Example: numbers = [1, 2, 3, 4, 5] maximum = max(numbers) print(maximum) # 输出: 5

any() checks if any element of an iterable is true. Example: numbers = [0, 1, 2, 3, 4] has_non_zero = any(numbers) print(has_non_zero) # 输出: True

all() checks if all elements of an iterable are true. Example: numbers = [1, 2, 3, 4, 5] all_positive = all(numbers) print(all_positive) # 输出: True

abs() returns the absolute value of a number. Example: num = -10 absolute_value = abs(num) print(absolute_value) # 输出: 10

round() rounds a number to a given precision. Example: num = 3.14159 rounded_num = round(num, 2) print(rounded_num) # 输出: 3.14

pow() computes exponentiation. Example: base = 2 exponent = 3 result = pow(base, exponent) print(result) # 输出: 8

divmod() returns a tuple of (quotient, remainder). Example: a = 10 b = 3 quotient, remainder = divmod(a, b) print(quotient, remainder) # 输出: 3 1

chr() returns the character represented by an integer Unicode code point. Example: ascii_code = 65 char = chr(ascii_code) print(char) # 输出: A

ord() returns an integer representing the Unicode code point of a character. Example: char = 'A' ascii_code = ord(char) print(ascii_code) # 输出: 65

bin() returns the binary representation of an integer. Example: num = 10 binary_representation = bin(num) print(binary_representation) # 输出: 0b1010

hex() returns the hexadecimal representation of an integer. Example: num = 255 hex_representation = hex(num) print(hex_representation) # 输出: 0xff

oct() returns the octal representation of an integer. Example: num = 10 octal_representation = oct(num) print(octal_representation) # 输出: 0o12

id() returns the unique identifier of an object. Example: a = [1, 2, 3] b = a print(id(a) == id(b)) # 输出: True

input() reads a line from input. Example: name = input("请输入您的名字: ") print(f"您好,{name}!")

open() opens a file and returns a file object. Example: with open('example.txt', 'r') as file: content = file.read() print(content)

In summary, mastering these 36 high‑frequency Python built‑in functions can greatly improve coding efficiency and problem‑solving capability.

programmingfundamentalsbuilt-in-functionscode-examples
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.