Python 6-period job function

Source: Internet
Author: User

Function Overview Table (i)

 

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:

  Create a new function

def mylen ():    ' calculates the length of the S1 '    s1 = ' Hello world '    length = 0 for    i in s1:        length = length + 1    prin T (length)

  Initial knowledge function Call:  

 Calculate the length of the Hello Word by calling the Mylen function

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   #将函数体内的值赋给mylenstr_len = Mylen () print (' str_len:%s '%str_len)

 The function of the return keyword:

Return is a keyword

1 Accept return value

2 when a function executes to the return step, it terminates the execution of the entire function and how much code will not Execute.

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: ( real participation Parameter)

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):    the_max = x if x > y else y    return the_maxma = 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):    #此时x =10,y=20    the_max = x if x > y else y    return the_maxma = Mymax (10,20) print (ma)

2, according to the key word value

def Mymax (x, y):    #此时x = 20,y = Ten    print (x, y)    The_max = x if x > y else y    return the_maxma = Mymax (y = 10,x =) Print (ma)

3, the position, the key word form mix uses

def Mymax (x, y):    #此时x = 10,y =    Print (x, y)    The_max = x if x > y else y    return the_maxma = 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

def Mymax (x, y):    #此时x = 10,y =    Print (x, y)    The_max = x if x > y else y    return the_max# call Mymax do not pass parameter Ma = Mymax () print (ma) #结果TypeError: 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 stu_info (name,sex = "male"): "" "    Print student information function, Since most of the students in the class are boys,        the default value for setting the default parameter sex 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 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 ') stu_info (name = ' alex ', sex = ' 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: The DEF 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 Definitions. 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 argument, **kwargs): "" " comment: 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 parameters defined by the function 3. Return value    If the function has a return value, you should also define "variable" to receive the return value    if the return value has more than one, You can also use multiple variables to receive, the number of variables should be consistent with the number of return values no return value of the Case: function name () has a return value: variable = function name () Multiple variables receive multiple return value: variable 1, variable 2, ... = function name ()

Python 6-period job 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.