Common Python Built‑in Functions with Examples
This article introduces a collection of frequently used Python built‑in functions, explains their purpose, provides concise code examples for each, and shows the corresponding output to help readers write more efficient and readable Python code.
Python provides many built‑in functions that help developers write code more efficiently. Familiarity with these functions can improve programming productivity and make code cleaner and easier to read.
1. len() : Returns the length of a string, list, tuple, etc.
my_list = [1, 2, 3, 4, 5]
print("Length of the list:", len(my_list))Output: Length of the list: 5
2. sorted() : Returns a new sorted list from an iterable.
my_list = [3, 1, 4, 1, 5, 9]
sorted_list = sorted(my_list)
print("Sorted list:", sorted_list)Output: Sorted list: [1, 1, 3, 4, 5, 9]
3. sum() : Calculates the sum of a numeric list.
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print("Sum of the list:", total)Output: Sum of the list: 15
4. min() : Returns the smallest item in an iterable.
numbers = [3, 1, 4, 1, 5, 9]
smallest = min(numbers)
print("Minimum value:", smallest)Output: Minimum value: 1
5. max() : Returns the largest item in an iterable.
numbers = [3, 1, 4, 1, 5, 9]
largest = max(numbers)
print("Maximum value:", largest)Output: Maximum value: 9
6. abs() : Returns the absolute value of a number.
x = -5
print("Absolute value:", abs(x))Output: Absolute value: 5
7. round() : Rounds a float to a specified number of decimal places.
x = 3.14159
print("Rounded value:", round(x, 2))Output: Rounded value: 3.14
8. pow() : Computes the power of a number.
x = 2
y = 3
result = pow(x, y)
print("Power result:", result)Output: Power result: 8
9. range() : Generates a sequence of integers.
for i in range(5):
print(i, end=' ')
print()Output: 0 1 2 3 4
10. enumerate() : Returns an indexed enumeration of an iterable.
fruits = ['apple', 'banana', 'cherry']
for index, fruit in enumerate(fruits):
print(index, fruit)Output: 0 apple 1 banana 2 cherry
11. zip() : Aggregates elements from multiple iterables into tuples.
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
zipped = zip(names, ages)
print("Zipped list:", list(zipped))Output: Zipped list: [('Alice', 25), ('Bob', 30), ('Charlie', 35)]
12. reversed() : Returns an iterator that accesses the given sequence in the reverse order.
my_list = [1, 2, 3, 4, 5]
reversed_list = list(reversed(my_list))
print("Reversed list:", reversed_list)Output: Reversed list: [5, 4, 3, 2, 1]
13. str() : Converts an object to its string representation.
num = 42
str_num = str(num)
print("String representation:", str_num)Output: String representation: 42
14. list() : Converts an iterable to a list.
tup = (1, 2, 3)
lst = list(tup)
print("Converted list:", lst)Output: Converted list: [1, 2, 3]
15. tuple() : Converts an iterable to a tuple.
my_list = [1, 2, 3]
my_tuple = tuple(my_list)
print("Converted tuple:", my_tuple)Output: Converted tuple: (1, 2, 3)
16. set() : Creates a set of unique elements.
my_list = [1, 2, 2, 3, 4, 4, 5]
unique_elements = set(my_list)
print("Deduplicated set:", unique_elements)Output: Deduplicated set: {1, 2, 3, 4, 5}
17. dict() : Creates a dictionary from two iterables.
keys = ['a', 'b', 'c']
values = [1, 2, 3]
my_dict = dict(zip(keys, values))
print("Created dictionary:", my_dict)Output: Created dictionary: {'a': 1, 'b': 2, 'c': 3}
18. all() : Returns True if all elements of the iterable are true.
my_list = [True, True, True]
print("All true?", all(my_list))Output: All true? True
19. any() : Returns True if any element of the iterable is true.
my_list = [False, False, True]
print("Any true?", any(my_list))Output: Any true? True
20. bin() : Converts an integer to its binary string representation.
print("Binary of 10:", bin(10))Output: Binary of 10: 0b1010
21. bool() : Converts a value to a Boolean.
print("1 as bool:", bool(1))
print("0 as bool:", bool(0))Output: 1 as bool: True 0 as bool: False
22. chr() : Returns the character represented by a Unicode code point.
print("Character for code 65:", chr(65))Output: Character for code 65: A
23. divmod() : Returns a tuple of (quotient, remainder).
print("Divmod of 10 and 3:", divmod(10, 3))Output: Divmod of 10 and 3: (3, 1)
24. eval() : Evaluates a string as a Python expression.
expression = "3 + 4"
print("Result of expression:", eval(expression))Output: Result of expression: 7
25. filter() : Filters elements of an iterable based on a function.
numbers = [1, 2, 3, 4, 5]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print("Even numbers:", even_numbers)Output: Even numbers: [2, 4]
26. float() : Converts a value to a floating‑point number.
print("3 as float:", float(3))Output: 3 as float: 3.0
27. format() : Formats a string using placeholders.
print("Hello, {name}!".format(name="World"))Output: Hello, World!
28. input() : Reads a line of input from the user (interactive use).
user_input = input("Please enter your name: ")
print("Hello,", user_input)Example interaction: Please enter your name: Zhang San Hello, Zhang San
29. int() : Converts a value to an integer.
print("'123' as int:", int("123"))Output: '123' as int: 123
30. find() : Returns the lowest index of the substring if found, otherwise -1.
text = "Hello, welcome to the world of Python."
index_of_world = text.find("world")
print("Index of 'world':", index_of_world)Output: Index of 'world': 22
31. map() : Applies a function to all items in an iterable.
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x ** 2, numbers))
print("Squared list:", squared_numbers)Output: Squared list: [1, 4, 9, 16, 25]
32. ord() : Returns the Unicode code point of a character.
print("Unicode of 'A':", ord('A'))Output: Unicode of 'A': 65
33. slice() : Creates a slice object to specify a range of indices.
my_list = [1, 2, 3, 4, 5]
s = slice(1, 4)
print("Sliced list:", my_list[s])Output: Sliced list: [2, 3, 4]
34. open() : Opens a file and returns a file object.
with open('example.txt', 'w', encoding='utf-8') as file:
file.write('Hello, world!')
with open('example.txt', 'r', encoding='utf-8') as file:
content = file.read()
print("File content:", content)Output: File content: Hello, world!
35. print() : Prints the given objects to the console.
print("Hello, world!")Output: Hello, world!
In summary, these common Python built‑in functions cover basic type conversion, arithmetic, string manipulation, file handling, and advanced iteration utilities. Mastering them can greatly boost coding efficiency and readability.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.