Python’s design philosophy is “small core language” + “large standard library”, so when Python wants to add a new feature, it is more likely to think about whether to add it to the core language support or put it into the library as an extension. library is very large and contains many modules. To use a function, you must import the corresponding module in advance, otherwise the function is invalid. And built-in functions are part of the interpreter; they take effect as the interpreter is started. So the number of built-in functions must be strictly controlled, or the Python interpreter will become large and bloated. In general, only functions that are used frequently or are more tightly bound to the language itself will be promoted as built-in functions.

There are currently 79 built-in functions provided by Python.

abs() delattr() hash() memoryview() set()
all() dict() help() min() setattr()
any() dir() hex() next() slice()
ascii() divmod() id() object() sorted()
bin() enumerate() input() oct() staticmethod()
bool() eval() int() open() str()
breakpoint() 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()

The above content is sorted alphabetically, and in order to better understand and master this knowledge, a simple classification is made, and some of the more difficult ones are analyzed with emphasis.

Mathematical operations

  • abs(): returns the absolute value
  • divmod(): returns the quotient and remainder
  • max(): returns the maximum value
  • min(): return the minimum value
  • pow(): power calculation
  • round(): rounds the number
  • sum(): summation

Type conversion

  • bool(): returns the Bool value
  • int(): return integer
  • float(): return floating point number
  • dict(): return dictionary
  • list(): return list
  • tuple(): return tuple
  • set(): return set
  • str(): return string
  • complex(): return complex number
  • bytearray(): return byte array
  • bytes(): return immutable byte array
  • memoryview(): create a new memoryview object based on the passed parameters
  • ord(): return the integer corresponding to the Unicode character
  • chr(): return the Unicode character corresponding to the integer
  • bin(): convert an integer to a binary string
  • oct(): convert an integer to an octal string
  • hex(): convert the integer to a hexadecimal string
  • frozenset(): create an immutable set based on the incoming arguments
  • enumerate(): create an enumeration based on iterable objects
  • range(): create a new range object based on the passed arguments
  • iter(): create a new iterable object based on the passed arguments
  • slice(): create a new slice object based on the passed arguments
  • super(): creates a new proxy object with subclass and parent relationship based on the passed arguments
  • object(): creates a new object object

enumerate()

1
2
3
seasons = ['Spring', 'Summer', 'Fall', 'Winter']
print(list(enumerate(seasons)))
print(list(enumerate(seasons, start=1)))  # 指定起始值

super()

The super() function is used to provide access to the methods and properties of a parent or sibling class. super() returns an object representing the parent class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Parent:
    def __init__(self, txt):
        self.message = txt

    def print_message(self):
        print(self.message)


class Child(Parent):
    def __init__(self, txt):
        super().__init__(txt)


x = Child("Hello, and welcome!")
x.print_message()

Sequence Operations

  • all(): determines if each element is True
  • any(): Determine if there are elements of True
  • filter(): use the specified method to filter the elements
  • map(): use the specified method to function on each element of the incoming iterable object to generate a new iterable object
  • next(): return the next element value in the iterable object
  • reversed(): reverses the sequence to generate a new iterable object
  • sorted(): sort the iterable object and return a new list
  • zip(): aggregates the elements in the same position in each iterable passed in, returning a new tuple type iterator

filter()

1
2
3
4
5
6
7
def if_odd(x):  # 定义奇数判断函数
    return x % 2 == 1


a = list(range(1, 10))  # 定义序列
print(list(filter(if_odd, a)))  # 筛选序列中的奇数
# [1, 3, 5, 7, 9]

map()

1
2
3
a = map(ord, 'abcd')
print(list(a))
# [97, 98, 99, 100]

zip()

1
2
3
4
x = [1, 2, 3]  # 长度3
y = [4, 5, 6, 7, 8]  # 长度5
print(list(zip(x, y)))  # 取最小长度3
# [(1, 4), (2, 5), (3, 6)]

Object Operations

  • help(): return the help information of the object
  • dir(): returns a list of properties within the object or current scope
  • id(): return the unique identifier of the object
  • hash(): get the hash value of the object
  • type(): type: return the type of the object
  • len(): return the length of the object
  • ascii(): return the object’s printable table string representation
  • format(): formatting display value

dir()

1
2
3
4
import math

print(dir(math))
# ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']

format()

see Python String Formatting Tool

Reflex operation

  • __import__().
  • isinstance(): determines if the object is an instance of any class element in a class or type tuple
  • issubclass(): determines if the class is a subclass of another class or an element of any class in the type tuple
  • hasattr(): check if the object contains attributes
  • getattr(): get the value of an attribute of an object
  • setattr(): set the value of the object’s attribute
  • delattr(): remove the object’s attributes
  • callable(): check if the object can be called

Variable operations

  • globals(): returns a dictionary of global variables and their values in the current scope
  • locals(): returns a dictionary of local variables and their values in the current scope
  • vars(): returns a dictionary of local variables and their values in the current scope, or a list of properties of the object

Interaction

  • print(): print output to the standard output object
  • input(): reads user input values

File Operations

  • open(): opens the file with the specified pattern and encoding, returns the file read/write object

Compile and execute

  • compile(): compile the string into code or AST object, so that it can be executed by exec statement or eval for evaluation
  • eval(): execute dynamic expressions to evaluate
  • exec(): execute dynamic statement block
  • repr(): return a string representation of an object (to the interpreter)

Decorator

  • @property: Decorator that marks properties
  • @classmethod: Decorator that marks methods as class methods
  • @staticmethod: decorator that marks methods as static methods

Other Functions

  • breakpoint()