Python Learning Note 5: A detailed description of the function parameters

Source: Internet
Author: User

First, the definition of the function format:
def function name (parameter list):
function body
Def fun1 (A, B, c):    return a + B + C

Second, position transfer: Position correspondence
Print (FUN1 (3, 2, 1))

Output:

6


Keyword delivery: positional parameters before the keyword parameter
Print (FUN1 (3, c = 1, b = 2))

Output:

6


Parameter default value: You can assign a default value to a parameter
Def fun2 (A, b, C = +):    return a + B + cprint (fun2 (1)) Print (FUN2 (1, 10, 0))
Output:
111
One


V. Parcel location Transfer: In the parameter table, all parameters are collected, merged into tuples according to location
When defining a function, you need to add * to the parameter
def fun3 (*tup):    print type (tup)    print tupfun3 (1) fun3 (1, 2) fun3 (1, 2, 3)
Output:
<type ' tuple ' >
(1,)
<type ' tuple ' >
(1, 2)
<type ' tuple ' >
(1, 2, 3)


Six, parcel keyword delivery: In the parameter table, all parameters are collected, according to the keyword merged into a dictionary
To define a function, you need to add the parameter before the argument
def fun4 (**dic):    print type (DIC)    print dic fun4 (a=1,b=9) fun4 (m=2,n=1,c=11)
Output:
<type ' dict ' >
{' A ': 1, ' B ': 9}
<type ' Dict ' >
{' C ': One, ' m ': 2, ' n ': 1}

Seven, unpacking
* Each element corresponds to a positional parameter
* * Each key value pair as a keyword
Def fun5 (A, B, c):    print (A, B, c) args = (2, 1, 3) fun5 (*args) dict = {' B ': 1, ' a ': 2, ' C ': 3}fun5 (**dict)
Output:
(2, 1, 3)
(2, 1, 3)


Eight, mixed
When defining or invoking parameters, several methods of passing parameters can be mixed. But be careful about the sequence in the process.
Basic principles: location > Keywords > Parcel location > Parcel keywords, and distinguish them according to the principles described above.

Python Learning Note 5: A detailed description of the function parameters

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.