Home >Backend Development >Python Tutorial >Basic exercises for function transfer in python language

Basic exercises for function transfer in python language

巴扎黑
巴扎黑Original
2017-04-01 13:32:121813browse

First of all, when writing the Python script, how to use Chinese
at the beginning of the script, you can add the following code segment:
# _*___ 8 _*_ its effect and# coding=utf-8 is the same


Basic exercises for function transfer in python language


The following is a summary of how to pass parameters when learning Python to define functions:
1. First of all It is the positional parameter and the default value:

Define a function func_name, where the formal parameters are arg, arg1, arg2, arg3, arg4
Among these parameters, arg3 and arg4 have default values. If only three values ​​are given when calling the function, then the default values ​​​​of arg3 and arg4 will be used. When the parameters given are four or five, the corresponding formal parameters will be assigned to the corresponding parameters at a time


Basic exercises for function transfer in python language


The following is the result of executing the program:

Basic exercises for function transfer in python language


PS: The position parameter is the most The main thing is that the number of parameters must be consistent, and the order is very important. The corresponding value is assigned to the formal parameter at the corresponding position.

Passing parameters with keywords:
You can also use keywords to pass parameters here, that is, in When calling a function, you specify which actual parameter corresponds to the formal parameter. At this time, the position is not so important. However, if there are both positional parameters and keyword parameters, you still need to define the positional parameters first. This is in the definition. Be careful when calling functions, otherwise it will cause unnecessary trouble when calling functions.


2. Collect remaining redundant parameters and collect redundant dictionary parameters

Define a function that can be called by the normal method of passing in only value or key-value. But if the first parameter passed in is the key-value method, then the subsequent ones must all be key-value methods. If the first one is not, then the subsequent values ​​can be passed in according to the situation.

The following is a specific example program:

Basic exercises for function transfer in python language


#In the above function func_name, the parameter it specifies is uncertain, and may be 0, or maybe n, so use *arg to collect all remaining parameters

When calling a function, if the parameter is a list, using *Li is actually to solve the list and then add each element in the list in sequence To pass parameters, *arg is generally used for parameter collection; if *Li is not used, Li is called directly, which is to pass the entire list as a whole

**arg is used to collect key-value class parameters Yes, this type of parameter is specified and defined at the end of the function parameter. When actually calling the function, you can use the form 'key'='value' or {'key1'='value1', 'key2'='value2'} Calling by value

The definition of the function cat is to put all the parameters together so that everyone can see the effect and how the parameters are passed when calling the cat function


Basic exercises for function transfer in python language



Basic exercises for function transfer in python language


The following is the output result of the function. Based on the comparison of the results, you can feel the difference between different parameter calls:

Basic exercises for function transfer in python language


3. Obtain variables from standard input, replace variables in statements, and use dictionary parameters in the function body:
raw_input is used Get the value of standard input and assign it to a variable, and you can also get a prompt message
In Python, add a variable to the statement and use % to replace it

Basic exercises for function transfer in python language


In When defining and calling functions, you must pay attention to the two preceding * signs when passing parameters in the key-value dictionary. After the function is collected, it is saved in a dictionary. After passing it in, the corresponding dictionary method will be used when calling, here I won’t go into details;

The following is the result of running the program. You can compare it based on the results to see the specific effects of passing parameters:

Basic exercises for function transfer in python language

##4 , The return value of the function

The most important thing in a function is its return value. When calling a function, the result of its return value is used to perform the next step of operation. The following is a simple example:


Basic exercises for function transfer in python language

The results are as follows:


Basic exercises for function transfer in python language

5. Use functions to implement a factorial (recursive and automatic Definition)


Basic exercises for function transfer in python language
The following is the output result, the results of the two functions are consistent:

Basic exercises for function transfer in python language

6. The following is a function call, and a comparison of the similarities and differences between the function name and function result assignment. Calling the return value of another function in one function

Basic exercises for function transfer in python language

The following is the output result. By comparing the script, you can see the difference and function

Basic exercises for function transfer in python language


7. The following is the basic function of some built-in functions. , are some simple exercises for everyone’s reference and study. Please give us your valuable opinions

Basic exercises for function transfer in python language

The results are as follows:

Basic exercises for function transfer in python language


The above are some of my summaries and reference exercises for everyone to learn from. Please give me your valuable opinions! ! !
This article comes from the "ptallrights" blog, please be sure to keep this source http://ptallrights.blog.51cto.com/11151122/1788896

The above is the detailed content of Basic exercises for function transfer in python language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn