Common Python Built‑in Functions: 68 Examples with Code
This article introduces 68 commonly used Python built‑in functions across numeric, string, list, dictionary, tuple, set, and file operations, providing clear explanations and code examples to help programmers efficiently manipulate data types and improve coding productivity.
Python is a popular programming language with a powerful standard library. This article presents 68 frequently used built‑in functions, covering numeric, string, list, dictionary, tuple, set, and file operations, each accompanied by concise code examples.
Numeric Functions
### abs()
# Returns the absolute value of a number.
a = -5
print(abs(a)) # 5
### divmod()
# Returns the quotient and remainder of two numbers.
a, b = 10, 3
print(divmod(a, b)) # (3, 1)
### pow()
# Calculates the power of a number.
a, b = 2, 3
print(pow(a, b)) # 8
### round()
# Rounds a number to the nearest integer.
a = 2.8
print(round(a)) # 3String Functions
### chr()
# Converts an ASCII code to its character.
print(chr(97)) # 'a'
### ord()
# Returns the ASCII code of a character.
print(ord('a')) # 97
### len()
# Returns the length of a string.
s = 'hello world'
print(len(s)) # 11
### str()
# Converts a value to a string.
a = 123
print(str(a)) # '123'
### capitalize()
# Capitalizes the first character.
s = 'hello world'
print(s.capitalize()) # 'Hello world'
### lower()
# Converts all characters to lowercase.
s = 'Hello World'
print(s.lower()) # 'hello world'
### upper()
# Converts all characters to uppercase.
s = 'Hello World'
print(s.upper()) # 'HELLO WORLD'
### swapcase()
# Swaps case of each character.
s = 'Hello World'
print(s.swapcase()) # 'hELLO wORLD'
### title()
# Capitalizes the first letter of each word.
s = 'hello world'
print(s.title()) # 'Hello World'
### strip()
# Removes leading/trailing spaces or specified characters.
s = ' hello world '
print(s.strip()) # 'hello world'
### replace()
# Replaces specified characters.
s = 'hello world'
print(s.replace('l', 'i')) # 'heiioworid'
### split()
# Splits a string by a delimiter.
s = 'hello,world'
print(s.split(',')) # ['hello', 'world']
### join()
# Joins an iterable of strings with a separator.
lst = ['hello', 'world']
print('-'.join(lst)) # 'hello-world'List Functions
### len()
# Returns the length of a list.
lst = [1, 2, 3, 4, 5]
print(len(lst)) # 5
### max()
# Returns the maximum value in a list.
lst = [1, 2, 3, 4, 5]
print(max(lst)) # 5
### min()
# Returns the minimum value in a list.
lst = [1, 2, 3, 4, 5]
print(min(lst)) # 1
### sum()
# Returns the sum of all elements.
lst = [1, 2, 3, 4, 5]
print(sum(lst)) # 15
### sorted()
# Returns a sorted list.
lst = [3, 1, 4, 2, 5]
print(sorted(lst)) # [1, 2, 3, 4, 5]
### reversed()
# Returns an iterator that yields the list in reverse order.
lst = [1, 2, 3, 4, 5]
print(list(reversed(lst))) # [5, 4, 3, 2, 1]
### list()
# Converts another iterable to a list.
t = (1, 2, 3)
print(list(t)) # [1, 2, 3]
### append()
# Appends an element to the end of the list.
lst = [1, 2, 3]
lst.append(4)
print(lst) # [1, 2, 3, 4]
### insert()
# Inserts an element at a specified position.
lst = [1, 2, 3]
lst.insert(1, 4)
print(lst) # [1, 4, 2, 3]
### remove()
# Removes the first occurrence of a value.
lst = [1, 2, 3]
lst.remove(2)
print(lst) # [1, 3]
### pop()
# Removes and returns the last element.
lst = [1, 2, 3]
print(lst.pop()) # 3
print(lst) # [1, 2]
### extend()
# Extends a list by appending elements from another iterable.
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
lst1.extend(lst2)
print(lst1) # [1, 2, 3, 4, 5, 6]
### index()
# Returns the first index of a value.
lst = [1, 2, 3, 4, 5]
print(lst.index(3)) # 2
### count()
# Returns the number of occurrences of a value.
lst = [1, 2, 2, 3, 3, 3]
print(lst.count(2)) # 2Dictionary Functions
### len()
# Returns the number of key‑value pairs.
d = {'a': 1, 'b': 2, 'c': 3}
print(len(d)) # 3
### keys()
# Returns all keys.
print(d.keys()) # dict_keys(['a', 'b', 'c'])
### values()
# Returns all values.
print(d.values()) # dict_values([1, 2, 3])
### items()
# Returns all key‑value pairs.
print(d.items()) # dict_items([('a', 1), ('b', 2), ('c', 3)])
### get()
# Returns the value for a key, or a default.
print(d.get('a')) # 1
print(d.get('d', 0)) # 0
### pop()
# Removes a key and returns its value.
print(d.pop('b')) # 2
print(d) # {'a': 1, 'c': 3}
### update()
# Updates a dictionary with another's key‑value pairs.
d1 = {'a': 1, 'b': 2}
d2 = {'b': 3, 'c': 4}
d1.update(d2)
print(d1) # {'a': 1, 'b': 3, 'c': 4}Tuple Functions
### len()
# Returns the number of elements.
t = (1, 2, 3)
print(len(t)) # 3
### max()
# Returns the maximum element.
print(max(t)) # 3
### min()
# Returns the minimum element.
print(min(t)) # 1
### tuple()
# Converts an iterable to a tuple.
lst = [1, 2, 3]
print(tuple(lst)) # (1, 2, 3)Set Functions
### len()
# Returns the number of elements.
s = {1, 2, 3}
print(len(s)) # 3
### max()
# Returns the maximum element.
print(max(s)) # 3
### min()
# Returns the minimum element.
print(min(s)) # 1
### set()
# Converts an iterable to a set.
lst = [1, 2, 3]
print(set(lst)) # {1, 2, 3}
### add()
# Adds an element to the set.
s.add(4)
print(s) # {1, 2, 3, 4}
### remove()
# Removes an element from the set.
s.remove(2)
print(s) # {1, 3}File Operation Functions
### open()
# Opens a file and returns a file object.
f = open('test.txt', 'r')
### close()
# Closes an opened file.
f.close()
### read()
# Reads the entire file content.
f = open('test.txt', 'r')
print(f.read())
f.close()
### write()
# Writes a string to a file.
f = open('test.txt', 'w')
f.write('hello world
')
f.close()Other Useful Functions
### isinstance()
# Checks if an object is an instance of a class.
x = 10
print(isinstance(x, int)) # True
### range()
# Generates a sequence of integers.
print(list(range(5))) # [0, 1, 2, 3, 4]
### zip()
# Aggregates elements from multiple iterables.
lst1 = [1, 2, 3]
lst2 = ['a', 'b', 'c']
print(list(zip(lst1, lst2))) # [(1, 'a'), (2, 'b'), (3, 'c')]
### map()
# Applies a function to each item of an iterable.
lst = [1, 2, 3]
print(list(map(lambda x: x * 2, lst))) # [2, 4, 6]
### filter()
# Filters items based on a predicate.
lst = [1, 2, 3, 4, 5]
print(list(filter(lambda x: x % 2 == 0, lst))) # [2, 4]
### reduce()
# Reduces an iterable to a single value.
from functools import reduce
lst = [1, 2, 3]
print(reduce(lambda x, y: x * y, lst)) # 6
### round()
# Rounds a number.
a = 2.8
print(round(a)) # 3
### open()
# Opens a file (repeated for emphasis).
f = open('test.txt', 'r')
print(f.read())
f.close()In summary, these 68 built‑in functions provide convenient tools for handling common data types and file operations in Python, helping developers write clearer and more efficient code.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
