Variable Length Parameter (*, **), Variable Parameter

Source: Internet
Author: User

Variable Length Parameter (*, **), Variable Parameter

Variable Length Parameter

Python also supports variable-length parameter lists. Variable Length parameters can be tuples or dictionaries.

1. tuples

When a parameter starts with *, the variable length parameter is considered as a tuples in the following format:

Def func (* t ):

In the func () function, t is treated as a tuples and every variable length parameter is obtained using t [index.

For example:

1 def func1 (* t): 2 print ("variable length parameter quantity:") 3 print (len (t) 4 print ("in sequence :") 5 for x in range (len (t): 6 print (t [x]); 7 8 func1 (1, 2, 4 ); 9 10 # output 11 variable length parameters: 12 413 in sequence: 14 115 216 317 4
2. Dictionary

When the parameter starts with **, it indicates that the variable length parameter will be treated as a dictionary in the following format:

Def func (** t ):

You can use the func () function for any number of arguments. The format of arguments is as follows:

Key = value # For example sum (a = 1, B = 2, c = 3)

Example:

Def sum (** t): print (t) sum (a = 1, B = 2, c = 3) # output {'A': 1, 'B ': 2, 'C': 3}

 

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.