let's start by explaining how to write How to use Chinese in Python script
At the beginning of the script you can add the following code snippet:
# _*_ Coding:utf-8 _*_ the effect is the same as #coding =utf-8.
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/82/B1/wKioL1depbSz9cMRAAAMOuRM_so937.png "title=" 1.png " alt= "Wkiol1depbsz9cmraaamourm_so937.png"/>
The following summarizes the learning the question of how Python can pass parameters when defining a function:
1, the first is the location of the parameter and the default value:
defines a function func_name, where the formal parameter is ARG,ARG1,ARG2,ARG3,ARG4
in these several parameters Arg3 and the Arg4 is a default value, if the function is called with only three values, the following Arg3 and the Arg4 The default value is used, and when the given parameter is four or five, it is assigned to the corresponding formal parameter one at a time to call
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/82/B3/wKiom1depRDi3FM-AAAlWA4OK-8940.png "title=" 2.png " alt= "Wkiom1deprdi3fm-aaalwa4ok-8940.png"/>
The following is the result of executing the program:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/82/B3/wKiom1depUaARsC-AAAPHP7k1KM230.png "title=" 3.png " alt= "Wkiom1depuaarsc-aaaphp7k1km230.png"/>
PS: The main parameter is the number of parameters to be consistent, and the order is important, the corresponding value assigned to the corresponding position of the formal parameter
Keyword Pass-through:
In this case, you can also use the keyword parameter, that is, when calling a function to specify the parameters and which arguments correspond to, then the position is not so important, but the location of the parameter and the keyword to pass the same argument, or to place the location of the parameter in the definition, which should be noted when defining the function, otherwise, the call to the function
2. Collecting surplus parameters and collecting redundant dictionary parameters
Defines a function that can be called by a normal pass-through value or Key-value method. However, if the first parameter is passed to the Key-value method, then the following must be the Key-value method, if the first is not, then the subsequent can be passed to the value of the case.
Here is a specific instance of the program:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/82/B1/wKioL1deponweoHAAABKvSJZvUI970.png "title=" 4.png " alt= "Wkiol1deponweohaaabkvsjzvui970.png"/>
in the above function func_name , the parameter specified is variable, possibly 0 , or n , so use *arg to collect all remaining parameters
when calling a function, if the argument is a list, using *li is actually the solution list and then each element in the list is then passed to the parameter, generally using *arg for the Parameters collection; If you do not use *li, call the Li directly , which is to pass the entire list as a whole
**arg is used to collect parameters of the Key-value class, which specifies the last side of the function parameter, which can be used when the function is actually called ' key ' = ' value ' or {' key1 ' = ' Value1 ', ' key2 ' = ' value2 ' in the form of a value call
The definition of the function cat is to put all the parameters together so that you can see the effect, and how to pass the parameters when invoking the Cat function.
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/82/B3/wKiom1depa_TYkSfAAAdMCbC-Ec132.png "title=" 5.png " alt= "Wkiom1depa_tyksfaaadmcbc-ec132.png"/>
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/82/B1/wKioL1depuzTJDEsAAATnHcxL90064.png "title=" 6.png " alt= "Wkiol1depuztjdesaaatnhcxl90064.png"/>
The following is the output of the function, according to the results of the comparison can feel the difference between the different calls:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/82/B3/wKiom1dephHSVkiQAAAjLr2pIOQ889.png "title=" 7.png " alt= "Wkiom1dephhsvkiqaaajlr2pioq889.png"/>
3.get the variable from the standard input, and replace the variable in the statement, and the application of the dictionary parameter in the function body:
Raw_input is used to get the value of the standard input assigned to the variable, and can also have a hint message
in the In Python , add a variable to the statement using % to replace it.
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/82/B4/wKiom1depkGRYl30AAA_gZtcDyY946.png "title=" 8.png " alt= "Wkiom1depkgryl30aaa_gztcdyy946.png"/>
when the function is defined and called,the Key-value class dictionary must pay attention to the previous two * number, after the function is collected in the dictionary to save, passed in after the call will be the corresponding dictionary method, here no longer repeat;
The following is the results of the program run, you can compare the results to see how the specific effects such as parameters:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/82/B4/wKiom1depneRp5efAAAaDUq_3BM880.png "title=" 9.png " alt= "Wkiom1depnerp5efaaaaduq_3bm880.png"/>
4. Return value of function
The most important thing in the function is its return value, when the function is called to use the result of its return value to proceed with the next operation, the following is a simple small example:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/82/B1/wKioL1dep7ezZZdTAAAjDj3KnZk193.png "title=" 10.png "alt=" Wkiol1dep7ezzzdtaaajdj3knzk193.png "/>
The results are as follows:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/82/B2/wKioL1deqAST90zDAAAGuH-VWsk270.png "title=" 11.png "alt=" Wkiol1deqast90zdaaaguh-vwsk270.png "/>
5. Use a function to implement a factorial (recursive and custom)
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/82/B2/wKioL1deqC7xk-ioAAApFp0d4Wg432.png "title=" 12.png "alt=" Wkiol1deqc7xk-ioaaapfp0d4wg432.png "/>
The following is the output and the two functions achieve the same result:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/82/B4/wKiom1dep0TBOP6AAAAIQeJQGHI642.png "title=" 13.png "alt=" Wkiom1dep0tbop6aaaaiqejqghi642.png "/>
6, the following is a function of the call, as well as the function of the name and the result of the functions of the comparison between the similarities and differences, in one function to call the return value of another function
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/82/B2/wKioL1deqK-D3NlJAAA7nhsUXcw056.png "title=" 14.png "alt=" Wkiol1deqk-d3nljaaa7nhsuxcw056.png "/>
Here is the output, the comparison script can see the difference and effect
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/82/B2/wKioL1deqNOAXBpOAAAKwDRYdqk682.png "title=" 15.png "alt=" Wkiol1deqnoaxbpoaaakwdrydqk682.png "/>
7, the following is the implementation of some of the basic functions of the built-in functions, are some simple exercises for everyone to refer to the study, please make a lot of valuable comments
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/82/B2/wKioL1deqQKDjNksAABVecEcg4E477.png "style=" float: none; "title=" 16.png "alt=" Wkiol1deqqkdjnksaabvececg4e477.png "/>
The results are as follows:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/82/B4/wKiom1deqBCzaXeBAAAMCjwNJ0Q387.png "title=" 17.png "alt=" Wkiom1deqbczaxebaaamcjwnj0q387.png "/>
Above is my some summary and reference exercises, for everyone to learn, please put forward valuable advice!!!
This article is from the "ptallrights" blog, make sure to keep this source http://ptallrights.blog.51cto.com/11151122/1788896
Basic exercises for the transfer of functions in the Python language