This article simply analyzes the usage of the built-in functions commonly used in Python and shares them for your reference. The specific analysis is as follows:
In general, there are a lot of useful functions built into python, and we can call them directly.
To call a function, you need to know the name and parameters of the function, such as the absolute value of the function abs, only one parameter. Documents can be viewed directly from the official website of Python: http://docs.python.org/2/library/functions.html#abs
You can also view the Help information for the ABS function on the interactive command line by helping (ABS).
Call the ABS function:
>>> ABS (100>>>) ABS ( -20) 20>>> abs (12.34) 12.34
When calling a function, if the number of arguments passed in is incorrect, the TypeError error is reported, and Python will explicitly tell you that ABS () has only 1 parameters, but gives two:
>>> ABS (1, 2) Traceback (most recent call last): File "
", line 1, in
typeerror:abs () t Akes exactly one argument (2 given)
If the number of arguments passed in is correct, but the parameter type cannot be accepted by the function, the TypeError error is reported, and an error message is given: STR is the wrong parameter type:
>>> ABS (' a ') Traceback (most recent call last): File "
", line 1, in
Typeerror:bad oper and type for abs (): ' str '
The comparison function cmp (x, y) requires two parameters, if x y, returns 1:
>>> CMP (1, 2) -1>>> CMP (2, 1) 1>>> CMP (3, 3) 0
Data type conversions
Python's built-in common functions also include data type conversion functions, such as the Int () function, which converts other data types to integers:
>>> Int (' 123 ') 123>>> Int (12.34) 12>>> float (' 12.34 ') 12.34>>> str (1.23) ' 1.23 ' >>> Unicode (+) U ' >>> bool (1) true>>> bool (") False
The function name is actually a reference to a function object, and it is possible to assign a function name to a variable, which is equivalent to giving the function an "alias":
>>> a = ABS # variable a points to ABS function >>> A (-1) # So you can also call the ABS function by a 1
Summary:
To invoke a Python function, you need to pass in the correct parameters according to the function definition. If the function call error, must learn to read the error message, so English is very important!
It is hoped that the examples described in this article will help you with Python programming.