Python Built-in Functions Overview and Usage
An extensive guide to Python's 68 built-in functions (up to version 3.6.2), organized into categories covering numeric operations, data types, data structures, scope, iterators, and I/O, with code examples illustrating each function's usage and behavior.
Python provides a set of built‑in functions that can be used directly without importing any modules. Up to version 3.6.2 there are 68 built‑in functions, which are listed below.
Full list of built‑in functions:
abs() dict() help() min() setattr()
all() dir() hex() next() slice()
any() divmod() id() object() sorted()
ascii() enumerate() input() oct() staticmethod()
bin() eval() int() open() str()
bool() exec() isinstance() ord() sum()
bytearray() filter() issubclass() pow() super()
bytes() float() iter() print() tuple()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod() getattr() locals() repr() zip()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round() delattr()
hash() memoryview() set()Numeric‑related functions
Data types : bool(), int(), float(), complex() Base conversion : bin(), oct(),
hex() print(bin(10)) # binary: 0b1010
print(hex(10)) # hexadecimal: 0xa
print(oct(10)) # octal: 0o12Mathematical operations : abs(), divmod(), round(), pow(), sum(), min(),
max() print(abs(-2)) # 2
print(divmod(20, 3)) # (6, 2)
print(round(4.50)) # 4
print(round(4.51)) # 5
print(pow(10, 2, 3)) # (10**2) % 3 => 1
print(sum([1,2,3,4,5,6,7,8,9,10])) # 55
print(min(5,3,9,12,7,2)) # 2
print(max(7,3,15,9,4,13)) # 15Data‑structure‑related functions
Sequences : list(),
tuple() print(list((1,2,3,4,5,6))) # [1, 2, 3, 4, 5, 6]
print(tuple([1,2,3,4,5,6])) # (1, 2, 3, 4, 5, 6)Sequence utilities : reversed(),
slice() lst = "你好啊"
it = reversed(lst) # returns an iterator, original unchanged
print(list(it)) # ['啊', '好', '你']
lst = [1,2,3,4,5,6,7]
print(lst[1:3]) # [2, 3]
s = slice(1,3,1)
print(lst[s]) # [2, 3]String conversion :
str() print(str(123) + '456') # 123456Collections : dict(), set(),
frozenset() my_dict = dict(a=1, b=2)
my_set = set([1,2,3])
my_frozen = frozenset([1,2,3])Collection utilities : len(), sorted(), enumerate(), all(), any(),
zip() lst = [5,7,6,12,1,13,9,18,5]
lst.sort()
print(lst) # [1,5,5,6,7,9,12,13,18]
print(sorted(lst, reverse=True)) # [18,13,12,9,7,6,5,5,1]
# enumerate with start index 1
for idx, el in enumerate(['one','two','three','four','five'], 1):
print(idx, el)
print(all([1, 'hello', True, 9])) # True
print(any([0,0,0,False,1,'good'])) # True
lst1 = [1,2,3,4,5,6]
lst2 = ['A','B','C','D','E','F']
lst3 = ['US','CN','FR','IT','KR','JP']
for el in zip(lst1, lst2, lst3):
print(el)Scope‑related functions
locals()– returns a dictionary of the current local symbol table. globals() – returns a dictionary of the current global symbol table.
def func():
a = 10
print(locals()) # {'a': 10}
print(globals()) # large dict with module globals
print("今天内容很多")
func()Iterator and generator utilities
range()– generates arithmetic sequences. iter() and next() – obtain an iterator and advance it.
for i in range(15, -1, -5):
print(i) # 15 10 5 0
lst = [1,2,3,4,5]
it = iter(lst)
print(next(it)) # 1
print(next(it)) # 2Executing code from strings
eval()– evaluates an expression and returns its value. exec() – executes statements; returns None. compile() – compiles source code to a code object for later execution.
s1 = input("请输入 a+b:") # e.g. 8+9
print(eval(s1)) # 17
s2 = "for i in range(5): print(i)"
exec(s2) # prints 0‑4
code1 = "for i in range(3): print(i)"
com = compile(code1, "", mode="exec")
exec(com) # 0 1 2
code2 = "5+6+7"
com2 = compile(code2, "", mode="eval")
print(eval(com2)) # 18Input/Output
print("hello", "world", sep="*", end="@") # hello*world@Memory‑related utilities
hash()– returns the hash value of an immutable object. id() – returns the memory address of an object.
s = 'alex'
print(hash(s)) # e.g. -168324845050430382
print(id(s)) # e.g. 2278345368944File operations
f = open('file', mode='r', encoding='utf-8')
content = f.read()
f.close()Module handling
# Dynamically import a module entered by the user
import os
name = input("请输入你要导入的模块:")
__import__(name)Help and introspection
print(help(str)) # shows documentation for the str typeCallable check
a = 10
print(callable(a)) # False
def f():
print("hello")
print(callable(f)) # TrueAttribute listing
print(dir(tuple)) # list of tuple methodsThis article consolidates the 68 built‑in functions into twelve logical groups, providing concise explanations and ready‑to‑run code snippets so that beginners can quickly reference and practice Python fundamentals.
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.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
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.
