Python functions using parameter tip notes

Source: Internet
Author: User

1, the modification of parameters

Normal use
    • Tuples, strings, int types are immutable, passed in as arguments, and do not change their own values

    • list, the dictionary is a mutable data structure, passed in as a parameter, and if the function executes, the value itself is changed by the action inside the function.

Exceptional cases
    • If you want to modify an immutable data structure:
      The value can only be re-assigned by the value returned by the function.

    • If you do not want to change the parameters of a mutable data structure:
      Can only copy the structure, name[:]

2. Keyword parameters and default values

The parameters are passed by default, depending on where the function is defined, or you can specify it manually:

def hello (greeting,name): print "%s,%s!"% (greeting,name) Hello (greeting= "Hello", name= "Qujun")
3. Collect parameters

When there are a large number of arguments that need to be passed, the parameters of the function can be defined using *params, such as:

def muli_paramter (*params): Print params

Note: Multiple parameters collected in this way will be stored in tuple format
To collect the keyword parameters, you need to use the **params

def print_params (x,y,z=3, *pospar,**keypar) L print x, y, z print pospar print Keypar

Note: The parameters collected by **params are dictionary format

4, the inverse use of parameters
def add (x, y): return x + yparams= (*params) def hello (greeting= "Hello", name= "Qujun"): print "%s,%s!"% (Greeti ng,name) keypar={' name ' = "Xixi", "greeting" = "haha") Hello (**keypar)


This article is from the "Danielqu" blog, make sure to keep this source http://qujunorz.blog.51cto.com/6378776/1905246

Python functions using parameter tip notes

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.