The 18th chapter of the Python study manual 4th

Source: Internet
Author: User

" " Date: September 5-September 30 Requirements: 1. Summary of the book contents, finishing in the blog Park notes upload 2. After all the exercises in the course Note: "#" After the addition of the notes (42 pages per day, you can guarantee to read this book at the end of the month) "Key Notes" "chapter Exercises"-Heading 1, level two headings- Heading 2, Notes outline title, exercise title-Bold, 16px"
Key Notes

Summary

A parameter is the way an object is sent to a function as input. Parameters (argument)
I. Pass-through parameters

Here are some brief key points when the function passes a parameter:

    • The argument is passed by automatically assigning the object to the local variable name.
    • The assignment of the parameter name inside the function does not affect the caller
    • Changing the value of a variable object parameter of a function may have an effect on the caller

Two. Specific parameter matching model

Parameters are always passed by assignment in Python. The passed-in object is assigned a variable name on the DEF header.

    1. Basic knowledge
      Position: Match from left to right
      keyword parameter : Use the syntax of name = value
      default parameter : defines a parameter value for a parameter that does not have an incoming value, using a syntax such as name = value
      variable Parameters : Collect any number of parameters based on location or keyword, function can use specific parameters, they are starting with a string *, collect any number of extra parameters
      variable parameter unpacking : one-to-many parameters based on location or keyword
      keyword-only parameter : parameter must be passed by name
    2. Details
      Inside Python, you use the following steps to match parameters before assigning values:
      ① to assign non-keyword parameters by location;
      ② assigns keyword parameters by matching variable names;
      ③ other additional non-keyword parameters are assigned to the *name tuple;
      ④ Other additional keyword parameters are assigned to the **name dictionary;
      ⑤ with default values assigned to parameters not assigned to the header

Three. Examples of arbitrary parameters

  1. Collect parameters
    The first use: in a function definition, collect mismatched positional parameters in tuples
     >>> def  F (*args):  print   >> > F () ()  >>> F (1) ( 1,)  >>> F (1,2,3,4) ( 1, 2, 3, 4) 

     

  2. The
  3. * * attribute is similar, but it is valid only for the keyword parameter.
     >>> def  F (**args):  Span style= "color: #0000ff;" >print   >>> F () {}  >>> F (A=1,b= py   " " {  "  b  :    PY  ,    A  : 1} 

     

  4. Mixed usage
    Function head can be mixed with general parameters, * parameters and * * parameters to achieve a more flexible call mode
    def F (a,*arg,**args):    print(a,arg,args)    >>> F (1,2,3,x=1  )1 (2, 3) {'x': 1}

Four. Keyword-only parameters

You can use a * character in the argument list to indicate that a function does not accept a parameter list of variable lengths, but still expects all parameters followed by * To be passed as keywords.

This chapter exercises:

1.

Answer: 1 2 5

2.

Answer: 1 2 3

3.

Answer: 1 (2,3)

4.

Answer: 1,{c:3,b:2}

5.

Answer: 1 5 6 4

The 18th chapter of the Python study manual 4th

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.