Python has built a lot of useful functions that we can call directly.
To call a function, you need to know the function name and parameters, such as the absolute value of the function abs, only one parameter. You can view documents directly from Python’s official website:
You can also view the help information for the exc function via help (abs) on the interactive command line.
Call the abs function:
>>> abs(100) 100 >>> abs(-20) 20 >>> abs(12.34) 12.34
Call the function, if the number of incoming parameters is not correct, will report TypeError error, and Python will tell you explicitly: abs () and only one parameter, but given two:
>>> abs(1, 2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: abs() takes exactly one argument (2 given)
If the number of passed arguments is correct, but the parameter type can not be accepted by the function, it will also report TypeError error, and give the error message: str is the wrong parameter type:
>>> abs('a') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: bad operand type for abs(): 'str'
While the max function max () can receive any number of arguments and return the largest one:
>>> max(1, 2) 2 >>> max(2, 3, 1, -5) 3
Data type conversion
Python built-in commonly used functions also include data type conversion functions, such as int () function can be converted to other data types of integers:
>>> int('123') 123 >>> int(12.34) 12 >>> float('12.34') 12.34 >>> str(1.23) '1.23' >>> str(100) '100' >>> bool(1) True >>> bool('') False
The name of a function is actually a reference to a function object, the function name can be assigned to a variable, the equivalent of this function played a “alias”:
>>> a = abs >>> a(-1) 1