Basic python and python tutorials

Source: Internet
Author: User

Basic python and python tutorials

I. parameter input rules

Variable parameters allow the input of 0 or any parameter, and are automatically assembled into a tuple during function call;

The keyword parameter allows 0 or any parameters to be passed in. It is automatically assembled into a dict during function calling;

1. input variable parameters:

1 def calc(*numbers):2     sum = 03     for n in numbers:4         sum = sum + n * n5     return sum

Use the following defined functions:

Input Multiple parameters,

Calc (1, 2, 3, 4) 30 # function return value

Input a list,

Nums = [1, 2, 3] calc (* nums) # pass the element in the list as a variable parameter to function 14 # function return value

 

2. Input keyword parameters:

>>> def person(name, age, **kw):...     print('name: ', name, 'age: ', age, 'other: ', kw)... >>> >>> person('luhc', 24, city='Guangzhou')name:  luhc age:  24 other:  {'city': 'Guangzhou'}

Similarly, pre-defined dict can be passed into the above functions as parameters:

>>> info = {'city': 'Guangzhou', 'job': 'engineer'}>>> >>> person('luhc', 24, **info)name:  luhc age:  24 other:  {'city': 'Guangzhou', 'job': 'engineer'}

Note: The function person obtains a copy of the info parameter. Modifying the parameter in the function does not affect the value of info.

 

3. In the keyword parameter, you can limit the name of the keyword parameter:

# Use * to specify the keyword Parameter name> def person (name, age, *, city, job ):... print ('name: ', name, 'Age:', age, 'city: ', city, 'job:', job)... >>>>> person ('luhc ', 24, city = 'guangzhou', job = 'engine') name: luhc age: 24 city: Guangzhou job: engineer # If the parameter is input, if the parameter name is not defined, an exception> person ('luhc ', 24, city = 'guangzhou', jobs = 'engine') is thrown ') traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: person () got an unexpected keyword argument 'job' >>>

In addition, if variable parameters have been specified in the function, * can be omitted as follows:

# If * is omitted, specify the keyword Parameter name> def person (name, age, * args, city, job ):... print ('name: ', name, 'Age:', age, 'args: ', args, 'city:', city, 'job :', job)... >>>>>> person ('luhc ', 24, 'A',' B ', city = 'guangz', job = 'engineer') name: luhc age: 24 args: ('A', 'B') city: Guangz job: engineer >>> # Similarly, if the unspecified parameter name is input, an exception is thrown. >>> person ('luhc ', 24, 'A',' B ', city = 'guangz', job = 'engine', test = 'A ') traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: person () got an unexpected keyword argument 'test' >>>

 

Ii. Combination of parameters:

The order of parameter definitions must be: mandatory parameter, default parameter, variable parameter, name keyword parameter, and keyword Parameter

def f1(a, b, c=0, *args, **kw):    print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw)def f2(a, b, c=0, *, d, **kw):    print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)

 

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.