Python learning 6--function definition and parameters

Source: Internet
Author: User

1. Creation of functions
def function ():
function body
2. function parameters and arguments
A formal parameter is an argument in parentheses when a function is created or defined;
An argument is an actual parameter in parentheses that is passed in when the function is invoked.
3. Function documentation
(1) Internal note: ", single quotation mark and comment body represents a row, can be" "" "" "" Print multiple lines;
(2) Print function document:
Function name function.__doc__ (double horizontal line), Direct printing functions document
Help (name of function);
4. Notes
Start with #: #xxxxx
5. Keyword parameters
To avoid errors that occur when the function does not correspond to the arguments in the calling procedure because the order of the parameters is not corresponding to the function definition.
Thus, a keyword is added to the parameter to illustrate the parameters.
Example: Def zjhfunction (A, B):
Print (A/b)
>>>zjhfunction (2,5), called correctly, if the parameter is reversed
Zjhfunction (5,2), a different result will occur.
>>>zjhfunction (b=5,a=2), it will still be done correctly.
6. Default parameters
(1) Set the default value directly when defining:
def zjhfunction (a=1,b=2):
Xxx
When called, the parameter can be empty, or the desired value is assigned.
7. Collecting parameters
(1)
def zjh (*params):
Xxx
Zjh (' Zjh ', 2.3)
Run time: Package The parameters Cheng Yuanju the params. The output length (here is the number, starting from 1) is 4.
Params[i] represents the first element.
(2) Collect parameters and customize parameters
def zjh (*PARAMS,XXR):
Xxx
You must assign a different value to the keyword parameter at the time of the call or set the default value directly when you define it:
ZJH ("Ex", 2.3,xxr=7)
8. On Python there is only a function of the process
The difference between a simple procedure and a function:
The procedure has no return value, and the function has a return value.
Note: All functions in Python have a return of something, and none returns Nothing;
Cases:

      
Note: Dynamically determine all types in Python, including variables and return values, or Python
There are no variables, only names or codes.
Note: Multiple values can be returned in Python, in the form of lists, Ganso, etc.

Python learning 6--function definition and parameters

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.