Define a function
def function_name (formal parameter):
code block
Return ' Value ' #如果没有写return, default returns None
# a function to return this line is finished executing, and what is written after return does not execute
Function_name (actual parameter) # only call this function will execute the code inside the function.
function extracts the function to improve efficiency and reduce the amount of code
When defining a function, there can be more than one parameter, which can be a normal parameter, a default parameter, and a dynamic parameter. The default parameter is placed after the normal parameter, and the dynamic parameter is placed on the last face.
The actual parameters are entered in the order of the formal parameters, if not in order, you need to add the parameter name to write.
The default parameter can have a default value, and you can override the default value when you enter the actual parameter.
Dynamic parameters are usually written as ' *args ' and ' **kwargs ' two asterisks placed in the back, a star's parameters counted as a tuple, two stars counted as a dict.
Variables outside the function are global variables, inside the function are local variables, Python built-in variables. Follow the variable lookup order of the local > global> built-in.
For ease of differentiation, global variables are all capitalized. If you need to use global variables within a function, you need to precede the variables with global.
1 defTop_movies (num=5, *args, * *Kwargs):2 Print(num)3 Print(args)4 Print(Kwargs)5 6Rank = (0, 1, 2, 3, 4, 5)7Keywords = {'1':'The Shawshank Redemption','2':'Fight Club','3':'Toy Stories','4':'Radio Rock','5':'Fantastic Mr Fox'}8 9Top_movies (*rank, * *keywords)Ten One " " A after running, get the result: - 0 - (1, 2, 3, 4, 5) the {' 1 ': ' The Shawshank Redemption ', ' 5 ': ' Fantastic Mr Fox ', ' 2 ': ' Fight Club ', ' 3 ': ' Toy Stories ', ' 4 ': ' Radio Rock '}< /c7> - The first digit of the parameter is num, although it is the tuple in rank, and the remainder is args, and finally the Kwargs. Visible is strictly in the order of the specified parameters. - If you do not include ' * ' and ' * * ' before the argument, the input parameter defaults to the first value of the actual parameter. - " "
Reference:
When you can't remember what you said, please refer to the teacher's instruction notes.
Http://www.cnblogs.com/wupeiqi/articles/5453708.html
"Python Full stack Notes" 03 [module two] 16-17 Oct functions