Python Full Stack road DAY18

Source: Internet
Author: User

first edited October 23, 2017, Monday

Summary
I. Last Class review
Two. Defining functions
Three. Variable parameters
Homework
Summary today

Summary

References: Alex

    1. Defining functions
    2. Calling functions
    3. function return value
    4. Parameters of the function
I. Last Class review
    1. Write supplement
      • F.write (' 111\n ')
        F.write (' 111\n ')
      • F.writelines ([' 111\n ', ' 222\n ']) #writelines usage equivalent to write multiple lines
        • The Writelines () method is used to write a sequence of strings to a file.
          This sequence string can be generated by an iterative object, such as a list of strings.
          Line break required to make a newline \ n
    2. Truncate
      • The truncate () method is used to truncate from the beginning of the first line of the file, truncate the file to size characters, no size to truncate from the current position, and all characters after the truncation after the V are deleted, where the line break on the Widnows system represents a 2-character size
Two. Defining functions
  1. function definition: A function refers to the collection of a set of statements by a name (function name) to encapsulate, to execute this function, just call their function name to
    • Characteristics:
      Reduce duplicate code
      To make the program extensible
      Make programs easier to maintain
    • Note: The definition of a function is equivalent to the assignment of a variable, the function name equals the variable name, the function's code is equivalent to the value of the variable, and the definition phase detects only the correct syntax of the function, but the call stage is different
  2. function classification
    1. Built-in functions
    2. Custom functions
      • def function name (ARG1,ARG2,ARG3):
        ' Description information '
        function body
        Return 1
  3. Definition and invocation of functions
    1. Define an parameterless function: The statement form for a function call when calledfoo()
    2. Define a parameter function: There is a parameter in the definition, the call must pass the parameter, and generally have the return value; The expression form of the function call when calledres=bar(1,2)
    3. Defining an empty function
  4. return value of the function
    1. Does not specify return in the function, the return value of this function is None
    2. If return returns a value of multiple, it is returned as a tuple
    3. Summarize:
      • can return any type
      • Return 1----> 1
      • Return-----> (a)
      • No return-----> None
      • function execution to return will end
  5. Decompression of variables
    • ‘_‘、‘*_‘
  6. Parameters of the function
    1. Formal parameters: In the function definition phase, the parameter defined in parentheses is essentially the variable name
    2. Arguments: In the function call phase, the parameters defined in parentheses must have a definite value that can be used as the value of the variable
      • Note: The value of the argument is immutable type
      • Do not modify the global variables in the function
def foo(x,y)        #形参    print(x)    print(y)foo(1,2)        #实参
    1. Three ways of transmitting values at the angle of an argument

      1. Pass Value by Location:

    def foo(x,y):        print(x)        print(y)    foo(1,2)
      1. Pass Value by keyword

    def foo(x,y):        print(x)        print(y)    foo(x=1,y=2)
      1. mixed with
      • The value must be passed in front of the value by the keyword.
      • Can only be assigned once for a parameter and cannot be assigned repeatedly

    def foo(x,y):        print(x)        print(y)    foo(1,y=2)
    1. Analysis from the angle of formal parameters

      1. Positional parameters, parameters that must be passed to the value

def foo(x,y):print(x)print(y)foo(1,2)
      1. Default parameters
        1. The default parameter must be placed after the position parameter
    def foo(x,y=1):    print(x)    print(y)    foo(2)
        • The default parameter in the function definition stage, his value has been determined

        sex =‘male‘    def register(sex=x):        #register-----[sex = ‘male‘]        print(sex)    sex = None    register()    #输出为male
        • Do not modify the global variables in the function (not recommended in the following way)
        x=[]    def register(name,name_list=x):        name_list.append(name)    register(‘ASB‘)    register(‘YSB‘)    register(‘WSB‘)    print(x)        #[‘ASB‘,‘YSB‘,‘WSB‘]
      Three. Variable parameters
        1. *args
          def foo(x,*args):    #args = (2,3,4,5,6,‘a‘,‘b‘)        print(x)                print(args)    foo(1,2,3,4,5,6,‘a‘,‘b‘)        
          • Args is mixed with positional parameters and default parameters: *args to be placed after the position parameter
          def foo(x,*args,y=2)        print(x)        print(args)        print(y)    foo(1,2,3,4,5,y=1000)    #>>1;(2,3,4,5);1000
        1. From the angle of the formal parameter
      def foo(*args):    #args=(1,2,3)    print(args)foo(1,2,3)
        1. Angle of argument
      def bar (x,y,z)    print(x)    print(y)    print(z)bar(*(1,2,3))    #bar(1,2,3)
        1. **kwargs
      def foo(x,**kwargs):    print(x)    print(kwargs)foo(1,y=2,a=3,b=4)    #<<1;{‘b‘:4,‘y‘:2,‘a‘:3}
        • Note: **kwargs (by keyword), the rest will be given after the location of the * *, processing the result of the {} dictionary form
          * Args (value by location), by keyword, the rest will give *, processing results as () tuple form
          1. mixed with
      def foo(x,*args,**kwargs):    print(x)    print(args)    print(kwargs)foo(1,y=1,z=2)    #>>1,(),{‘y‘:1,‘z‘:2}
        1. From the angle of the formal parameter
      def foo(**kwargs):    #{‘x‘: 1, ‘y‘: 2, ‘z‘: 3}    print(kwargs)foo(x=1,y=2,z=3)

      3. Angle of argument

      def foo(x,y,z=3):    print(x)    print(y)    print(z)foo(**{‘x‘:1,‘y‘:2,‘z‘:3})    #foo(x=1,y=2,z=3)
        1. Nesting
      def auth(name,password,sex=‘male‘):    print(name)    print(password)    print(sex)def foo(*args,**kwargs):    #args =(‘egon‘,‘123‘) kwargs= {}    print(‘from foo‘)    auth(*args,**kwargs)    #auth(*(‘egon‘,‘123‘),**{})---------auth(‘egon‘,‘123‘)foo(‘egon‘,‘123‘)    
      Homework
        1. No
      Summary today
        1. Pending additions

      Python Full Stack road DAY18

      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.