Python Built‑in Functions, Data Types, and Common Operations Overview
This article provides a comprehensive overview of Python's built‑in functions, including numeric, sequence, mapping, and conversion utilities, demonstrates data type usage such as bool, int, float, complex, and shows practical code examples for operations like sorting, filtering, iteration, file handling, and dynamic execution.
This guide introduces Python's built‑in functions and demonstrates how to use them with common data types and operations.
Built‑in Functions List
# 68 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()Data Types
bool – Boolean type (True, False)
int – Integer type
float – Floating‑point number
complex – Complex number
Number Base Conversion
print(bin(10)) # binary: 0b1010
print(hex(10)) # hexadecimal: 0xa
print(oct(10)) # octal: 0o12Mathematical Operations
print(abs(-2)) # absolute value: 2
print(divmod(20, 3)) # (quotient, remainder): (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)) # 15Sequences – List and 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)Additional sequence utilities:
lst = "你好啊"
it = reversed(lst) # returns an iterator, original unchanged
print(list(it)) # ['啊', '好', '你']
lst = [1,2,3,4,5,6,7]
print(lst[1:3:1]) # [2, 3]
s = slice(1,3,1)
print(lst[s]) # [2, 3]String Conversion and Formatting
print(str(123) + '456') # 123456
print(format('hello world!', '^20')) # centered
print(format('hello world!', '<20')) # left‑aligned
print(format('hello world!', '>20')) # right‑aligned
print(format(3, 'b')) # binary: 11
print(format(97, 'c')) # character: a
print(format(11, 'x')) # hex (lowercase): b
print(format(11, 'X')) # hex (uppercase): B
print(format(123456789, 'e')) # scientific notation
print(format(1.23456789, '.2f')) # 1.23Bytes and Bytearray
bs = bytes("今天吃饭了吗", encoding="utf-8")
print(bs) # b'\xe4\xbb\x8a...'
ret = bytearray("alex", encoding="utf-8")
print(ret[0]) # 97
print(ret) # bytearray(b'alex')
ret[0] = 65 # modify first byte
print(str(ret)) # bytearray(b'Alex')Character Code Utilities
print(ord('a')) # 97
print(ord('中')) # 20013
print(chr(65)) # A
print(chr(19999)) # 丢Object Representation
s = "今天
吃了%s顿\t饭" % 3
print(s) # 今天
吃了3顿 饭
print(repr(s)) # '今天
吃了3顿\t饭'Collections – len and sorted
lst = [5,7,6,12,1,13,9,18,5]
lst.sort()
print(lst) # [1, 5, 5, 6, 7, 9, 12, 13, 18]
ll = sorted(lst)
print(ll) # same as above
l2 = sorted(lst, reverse=True)
print(l2) # [18, 13, 12, 9, 7, 6, 5, 5, 1]
# sort by string length
lst = ['one','two','three','four','five','six']
def f(s):
return len(s)
print(sorted(lst, key=f)) # ['one','two','six','four','five','three']Enumeration
lst = ['one','two','three','four','five']
for index, el in enumerate(lst, 1):
print(index)
print(el)
# 1 one 2 two 3 three 4 four 5 fiveLogical Checks – all and any
print(all([1,'hello',True,9])) # True
print(any([0,0,0,False,1,'good'])) # TrueZip
lst1 = [1,2,3,4,5,6]
lst2 = ['醉乡民谣','驴得水','放牛班的春天','美丽人生','辩护人','被嫌弃的松子的一生']
lst3 = ['美国','中国','法国','意大利','韩国','日本']
for el in zip(lst1, lst2, lst3):
print(el)
# (1, '醉乡民谣', '美国') ...Filter
def func(i):
return i % 2 == 1
lst = [1,2,3,4,5,6,7,8,9]
print(list(filter(func, lst))) # [1,3,5,7,9]Map
def f(i):
return i
lst = [1,2,3,4,5,6,7]
print(list(map(f, lst))) # [1,2,3,4,5,6,7]Iterators – range, iter, 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)) # 3
print(next(it)) # 4Dynamic Execution – eval, exec, compile
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"
print(eval(compile(code2, "", mode="eval"))) # 18File Operations
f = open('file', mode='r', encoding='utf-8')
content = f.read()
f.close()Dynamic Module Import
import os
name = input("请输入你要导入的模块:")
__import__(name) # imports the module entered by the userHelp and Introspection
print(help(str)) # shows documentation for str
print(callable(10)) # False
print(callable(lambda x: x)) # True
print(dir(tuple)) # list of tuple methodsThis material serves as a practical reference for Python developers learning the language's core built‑in capabilities.
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.
