Comprehensive Guide to Python Built-in Functions
This article presents a detailed overview of Python's 68 built-in functions up to version 3.6.2, categorizing them into twelve groups such as numeric operations, data structures, scope, iterators, I/O, memory, file handling, modules, and more, with code examples illustrating each function's usage.
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 such functions, which are organized here into twelve thematic categories for easier learning.
Numeric related functions include conversions and calculations:
print(bin(10)) # binary: 0b1010
print(hex(10)) # hexadecimal: 0xa
print(oct(10)) # octal: 0o12
print(abs(-2)) # absolute value: 2
print(divmod(20, 3)) # quotient and remainder: (6, 2)
print(round(4.51)) # rounding: 5
print(pow(10, 2, 3)) # 10**2 % 3 => 1
print(sum([1,2,3,4,5,6,7,8,9,10])) # sum: 55
print(min(5,3,9,12,7,2)) # minimum: 2
print(max(7,3,15,9,4,13)) # maximum: 15Data structure related functions cover sequences, collections and related utilities:
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)
print(reversed('你好啊')) # iterator that yields ['啊', '好', '你']
print(slice(1,3,1)) # slice object for list[1:3]
print(str(123) + '456') # '123456'
print(format('hello world!', '^20')) # centered within 20 chars
print(bytes('今天吃饭了吗', encoding='utf-8')) # b'...'
print(bytearray('alex', encoding='utf-8')) # mutable byte array
print(ord('a')) # 97
print(chr(65)) # 'A'
print(repr('今天
吃了%s顿\t饭' % 3)) # "'今天
吃了3顿\t饭'"Scope functions allow inspection of local and global namespaces:
def func():
a = 10
print(locals()) # {'a': 10}
print(globals()) # global symbols
print('今天内容很多')
func()Iterator and generator utilities such as range, iter, and next:
for i in range(15, -1, -5):
print(i) # 15 10 5 0
lst = [1,2,3,4,5]
it = iter(lst)
print(it.__next__()) # 1
print(next(it)) # 2
print(next(it)) # 3String execution functions eval, exec and compile enable dynamic code evaluation:
s1 = input('请输入a+b:') # e.g., 8+9
print(eval(s1)) # 17
s2 = "for i in range(5): print(i)"
exec(s2) # prints 0 1 2 3 4
code = "5+6+7"
obj = compile(code, '', mode='eval')
print(eval(obj)) # 18Input/Output functions: print('hello', 'world', sep='*', end='@') # hello*world@ Memory related utilities such as hash and id:
s = 'alex'
print(hash(s)) # hash value
print(id(s)) # memory addressFile handling with open:
f = open('file', mode='r', encoding='utf-8')
content = f.read()
f.close()Module utilities using __import__ for dynamic imports:
import os
name = input('请输入你要导入的模块:')
__import__(name)Help and introspection functions:
print(help(str)) # shows documentation for str
print(callable(10)) # False
print(callable(lambda x: x)) # True
print(dir(tuple)) # list of tuple methodsThe article concludes with a reminder to explore the provided code snippets and practice using these built‑in functions to strengthen 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.
