Python3 built-in function
Python provides a large number ofbuilt-in functionsthat can be used directly without importing any library. These functions coverdata type conversion, mathematical computation, iteration, and reflectionand other common scenarios; they are essential tools every Python beginner must master.
Why master the built-in functions?
- Less code: one line replaces many (for example
sum()、max()) - Better readability: clear semantics, clearer than handwritten logic
- Better performance: implemented in C, usually faster than Python loops
Common categories
-
Numeric computation:
abs()、round()、min()、max()、sum() -
Type conversion:
int()、float()、str()、list()、tuple() -
Iteration and functional:code>map()、
filter()、zip()、enumerate() -
Reflection and objects:
type()、isinstance()、getattr()、setattr() -
Input and output:
print()、input()、open()
Examples of common functions
# 求和
sum([1, 2, 3]) # 6
# 最大值
max([3, 6, 2]) # 6
# Type casting
int("123") # 123
# 枚举
for i, v in enumerate(['a', 'b']):
print(i, v)
# map
list(map(lambda x: x * 2, [1,2,3])) # [2,4,6]
Function quick reference table
| Built-in functions |
|---|
| abs() |
| all() |
| any() |
| ascii() |
| bin() |
| bool() |
| bytearray() |
| bytes() |
| callable() |
| chr() |
| classmethod() |
| compile() |
| complex() |
| delattr() |