function definition and invocation
Functions are the most basic way of code abstraction (Python has many built-in functions)
Call: To invoke a function, you need to know the name and parameters of the function
The comparison function cmp (x, y) requires two parameters, if x<y, returns 1, if x==y, returns 0 if X>y returns 1
A data type conversion function is not as good as an int () function to convert other data types to integers
>>int (' 123 ')
123
>int (12.34)
12
For example:
>>STR (123)
' 123 '
>>str (12.3)
' 12.3 '
To write a function:
In Python, you define the use of a def statement with a bad function, write out the function name, parentheses, the arguments in parentheses, and a colon, and then write the function body in the indent block, returning the return value of the function with the return statement
For example, define a my_abs function that evaluates to an absolute value.
def my_abs (x):
If x>=0
return x
Else
Return-x
Note: If you do not have a return statement, the function will return the result, except that the structure is none
function returns multi-value
But in fact this is just an illusion, the Python function returned is still a single value, Python function returns a multivalued value is actually return a tuple
Recursive function: If a function calls itself internally, this function is a recursive function
Example: Calculate factorial n!
def fact (N):
If N==1
Return N*fact (n-1)
>>fact (1)
1
>>fact (5)
120
Knowledge Points: The use of recursive functions requires attention to prevent stack overflow. In the computer, when the function is called through the data structure of the stack (stack), the stack will add a stack frame whenever a function call is entered, and the stack will lose a stack frame whenever the function returns. Because the size of the stack is not infinite, too many recursive calls can cause the stack to overflow.
Defining Default Parameters
For example, Python's own int () function, in fact, there are two parameters, you can pass a parameter, you can pass two parameters
>>int (' 123 ')
123
>>int (' 123 ', 8)
83
Note: The second parameter of the Int () function su is converted into a system, if not passed, the default is decimal (base=10)
Knowledge points: Because the parameters of the function match from left to right. So the default parameter can only be defined after the required function
Defining mutable parameters
If you want a function to accept any of the arguments, we can define a mutable parameter:
DEF fn (args)
Print AGRs
The variable parameter has a number in front of it, and we can pass any parameter to the variable parameter:
>>FN ()
()
>>FN (' a ')
(' a ')
>>FN (' A ', ' B ', ' C ')
(' A ', ' B ', ' C ')
Mutable parameters are also not very mysterious, the Python interpreter will assemble a set of parameters passed in a tuple is passed to the variable parameter, so within the function, directly the variable args as a tuple just fine.
Slice operation:
Taking a partial element of a list is a very common operation
>>l=[' Adma ', ' Lisa ', ' Bart ', ' Paul ']
>>r=[]
>>n=3
>>for I in range (n)
... r.append (L[i])
...
>>r
[' Adma ', ' Lisa ', ' Bart ']
Slice operator
>>l[0:3] means that starting at index 0 and knowing index 3, Rage includes 3, or 0,1,2, which is exactly three elements
>>l[:3] If the first index is 0, you can also omit the
>>l[:] Use only one:, which means from beginning to end
The slice operation can also make a third parameter
>>l[::2] The third parameter indicates that each n takes one, and l[::2] means that every two elements are taken out of one, that is, one from the other.
Reverse slices:
Remember that the index of the first element of the countdown is-1, the reverse slice contains the starting index, does not contain the end index
Python Learning notes (4)