"Python learning" function parameter passing method four kinds (position, keyword, default value, parcel location, parcel keyword pass)

Source: Internet
Author: User

1. Location Transfer:

#def  Send (name,address):    return'packagewas sent to%s, Located in%s' %(name, address)print('Winnie' ,'Shanghai'))

2. Keyword Delivery

defRun (name,length, date):return '%s ran%s on%s'%(name,length,date)#When the keyword is passed, the parameter order can be ignored, and the name is the right line. Print(Run (length ='5km', date ='11/29/2016', name ='Winnie'))#keywords and locations are mixed, but be aware that the positional parameters are in front of the keyword. If not, the compiler does not know the order of the positions except for a few names. Print(Run ('Winnie', length ='5km', date ='11/29/2016'))
Winnie ran 5km on 11/29/201611/29/2016 in 0.2s]

3. Default value parameter

# 3. Default value parameter: can give the parameter default value #  We run the regiment every Tuesday routine about running def'Tuesday 5:00pm'  ):    return'%s ran%s on%s' % (name,length,time)

Call:

Print(Runweekly ('Jin','5km'))Print(Runweekly ('Jin','5km','Wednesday'))Print(Runweekly ('Jin', time ='5km','Wednesday'))#Error


" C:\pytest\Sele\tem1111.py ", line print(runweekly ('jin'5km') ,'Wednesday')) Syntaxerror:non- in 0.2s With exit code 1]

4. Parcel delivery

# 4. Package delivery (*/**)#  parameters are collect collected, Type:tupledef subselect (*collect):     Print Collect     Print type (collect) # parameters are collected by AA, Type:dict def Packing (* *aa)    :#print test    print  type ( AA)    print AA

Call:

Subselect (['ppe-test-1','dddd']) packing (a=1,b=2 , sub=[11,22])

Output:

<type ' tuple ' ><type ' dict ' >{' A ': 1, ' B ': 2, ' Sub ': [One, all]}[finished in 0.2s] 

5. Unpacking

#5.Unpacking with */**Tuple1 = ['test1','test2','test3']dictionary1= {' at':'88wi','b':'Secondparam','Third':'Winnie'}Print 'Dictionary1', Dictionary1defusedict (at,b,third):PrintAt , B, Thirdusedict (**dictionary1)#To unpack the dictionary parameter, the parameter name that is equivalent to the keyword argument passing the name and function definition must correspondUsedict (*tuple1)#unpacking a tuple at this point is equivalent to a positional parameter passing

"Python learning" function parameter passing method four kinds (position, keyword, default value, parcel location, parcel keyword pass)

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.