Basic exercises for the transfer of functions in the Python language

Source: Internet
Author: User

Let's start by explaining how to use Chinese in writing Python scripts.
At the beginning of the script you can add the following code snippet:
# _*_ Coding:utf-8 _*_ The effect and #coding=utf-8 are the same





Here's a summary of how to pass a parameter when learning about Python's defining functions:
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 parameters Arg3 and ARG4 have default values, if the function is called only three values, then the ARG3 and ARG4 will use the default value, when the given argument is four or five, then one time by the position assigned to the corresponding parameter to call





The following is the result of executing the program:




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:




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 to collect parameters; If you do not use *li, call 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.









The following is the output of the function, according to the results of the comparison can feel the difference between the different calls:




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
Add a variable to the statement in Python using% to replace it.




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:




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:




The results are as follows:




5. Use a function to implement a factorial (recursive and custom)



The following is the output and the two functions achieve the same result:



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



Here is the output, the comparison script can see the difference and effect




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



The results are as follows:




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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.