Python's initial knowledge function

Source: Internet
Author: User

Read Catalogue

First, why use the function

Second, the definition and invocation of functions

Third, function return value

Iv. parameters of the function

V. SUMMARY of this Chapter

First, the function flowchart:

  

naming rules for function names:

  1, the function name must consist of the letter underscore number, cannot be the keyword and the number beginning

2, function name or to have a certain meaning can simply explain the function of functions

The definition of a function of first knowledge:

  To create a new Mylen function

def Mylen ():     " " calculate the length of a S1 " "   ' Helloworld'    = 0    for       in s1:        = length + 1    print(length)

  Initial knowledge function call:  

 By calling Mylen's function, the length of the Hello Word is evaluated.

Str_len = Mylen ()print('str_len:%s'%str_len)

The above code is just the completion of the call, but the function body does not return a value, so call none, return the result using the return keyword. The code is as follows:

    return length   # assigns the value of the function body to mylenStr_len = mylen ()print( ' str_len:%s '%str_len)

 The function of the return keyword:

Return is a keyword that is highlighted in Pycharm.

If the function body does not write return, it will return a none by default, notice that a space between return and return values is required, and return returns the value of any data type.

Returns multiple values:

You can return multiple values, which are returned by the organization Cheng Yuanju, or can be accepted with multiple values.

Parameters of the function:

We're going to tell the Mylen function to calculate who the string is, and this process is called the pass parameter, referred to as the argument, we call the function to pass this ' Hello World ' and define the function of the S1 is the parameter .

Actual participation Parameters:

This ' Hello World ', which we pass when we call the function, is called the actual argument, because this is the content of the actual song function, referred to as the argument.

When defining a function, the S1 is simply the name of a variable, called the formal parameter. Because it is only a form when defining a function, it means that there is a parameter, called the formal parameter.

Parameter passing:

Passing Multiple parameters

Parameters can be passed multiple, separated by commas between multiple parameters. (Example below)

def Mymax (x, y    ): if Else y     return  = Mymax (10,20)print(MA)

  Position parameters

  Stand at the angle of the argument

1, according to the location of the value  

def Mymax (x, y    ): # at this time x=10,y=20    if Else y     return  = Mymax (10,20)print(MA)

2, according to the key word value

def Mymax (x, y    ): # at this point x = 20,y = Ten    Print (x,    y) if Else y     return  = Mymax (y = 10,x =)print(MA)

3, the position, the key word form mix uses

def Mymax (x, y    ): # at this point x = 10,y =    Print (x,    y) if Else y     return  = Mymax (10,y =)print(MA)

Correct usage

Problem one: The positional parameter must precede the key sub-parameters

Problem two: Only one parameter can be assigned once

  Standing at the angle of the formal parameter

Positional parameters must be passed value

defMymax (x, y):#at this point x = 10,y =    Print(x, y) The_max= XifX > YElseyreturnThe_max#calling Mymax does not pass parametersMa =Mymax ()Print(MA)#ResultsTypeerror:mymax () Missing 2 required positional arguments:'x'  and 'y'

  Default parameters

  1. Normal use

How to use

Why do I have a default parameter: Set the value of the smaller change to the default parameter

2. Definition of default parameters  

def " male " ):    "" " print student information function, because most of the students in the class are boys,        so set the default parameter sex's default value is    ' Male '" "    Print(name,sex) stu_info ('Alex') stu_info (  'Eva','female')

3. Parameter trap: The default parameter is a mutable data type

def defult_param (a,l = []):    l.append (a)    print(l) defult_param ('  Alex') defult_param ('Egon')

Dynamic parameters

Excess parameters by location are uniformly accepted by args and saved in the form of a meta-ancestor

 def  mysum (*args): The_sum  = 0  for  i in  Span style= "COLOR: #000000" > args:the_sum  +=i  return   the_sumthe_sum  = MySum (1,2,3,4 )  print  (the_sum) 
def Stu_info (* *Kwargs)    :print(Kwargs    )print(kwargs['  name'],kwargs['sex'Alex  'male')

Summary of this chapter

Process-oriented programming problems: Code redundancy, poor readability, poor scalability (not easy to modify)

Rules for defining functions:

1. Definition: Thedef  keyword begins with a space followed by the function name and parentheses (). 2. Parameters: Parentheses are used to receive parameters.    If multiple parameters are passed in, the parameters are separated by commas.    Parameters can be defined in multiple or undefined. There are many parameters, if the definition of a variety of parameters, should always follow the positional parameters,
*args, default parameters, * *Kwargs order definition. If a parameter type defaults during the above definition, the other parameters follow the above sort 3. Note: The first line of the function statement should add a comment. 4. Function Body: function content begins with a colon and is indented. 5. Return value: return [expression] End function. Return without an expression is equivalent to returning the Nonedef function name (parameter 1, parameter 2,*args, default parameter, * *Kwargs ): "" " Note: function function and parameter description "" " function body ... return value

Rules for calling functions:

1. The function name ()    after the function name + parentheses is the function's call. 2. Parameters:    parentheses are used to receive parameters.    if more than one parameter is passed in: the        value should be passed in the first position, and then        the specific incoming order should be determined according to the parameter condition defined by the function 3. Return value    If the function has a return value, you should also define "variable" to receive the return value    == function name ()

Python's initial knowledge function

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.