The python_day09_ function of iron Music learning

Source: Internet
Author: User

Today we're going to learn about functions.
Produced by the original, if there is no function, will appear to repeat the code is many, poor readability, will also cause the situation of repeating the wheel.
Therefore, a function is created to encapsulate the good one function, which is function-oriented.

1. "Style of function"

For example, you define a function:
Def my_len ():
A keyword in def python used to declare and define a function;
My_len function name, the naming specification is the same as the variable name;
Def and function names are separated by a space;
Function name + "()" + ":", parentheses with a colon, enclosed in parentheses, you want to set the parameters passed by the function caller;
followed by the indentation of the function body (the main function realization part);

example, the following custom calculations can iterate over the length of an object element:

my_len():def my_len(l):    =0    forin l:        +=1    return= [12345]print(my_len(li))

function execution, the function name + "()" will go to invoke execution.
The return value of the function, when defining the function, do not write the print statement in the function (this can be done when debugging), because the function is function-oriented and needs to return the output value when returning the value to the caller of the function.
The execution function is ended when a return is encountered in a function (in combination with the IF statement there is a sense of break in the loop).

The value returned by the function has the following types:
1) No return statement, returns none;
2) return is used and none is returned;
3) return specifies that a value is returned and that the value is returned directly to the function caller;
4) return returns multiple values, placing multiple values in a tuple and returning to the function caller.

2, "ternary operation"

example, a ternary operation is used in the following custom function body for comparing two integer sizes:

# 普通的比较两数大小,谁的值大返回谁的函数:def max_min(a, b):    if> b:        return a    else:        return bprint(max_min(290,355))# 使用了三元运算优化代码:def max_min(a,b):    returnif>else bprint(max_min(4556if>else> b 就返回a,否则返回b。
3, "function of the parameter"

There are two main aspects: arguments and formal parameters.
In the above example, Max_min (45,56), where 45 and 56 belong to the actual passing of the past parameters, so is the real parameter;
The Max_min (A, B) that appears in Def defines a function as a formal parameter for reference, that is, the formal parameter.

Actual parameter angle:
1, the position of the parameter, and the position of the parameter and the number one by one corresponding, in order, the number can not be less or more;
2, the key word, the same as the formal parameter one by one corresponding, but can be out of order, because there are keywords, such as the above example in the actual argument directly filled in b=56,a=45 this way.
3, mixing the parameter, is the positional parameters and keyword parameters mixed use, but note that all the key parameters are placed on the last side, that is, the positional parameters before the keyword parameters.

Formal parameter angle:
1, the position of the parameter, according to the order and number and the actual parameters to form one by one corresponding relationship;
2, the default parameters, important! When the default parameter is set, if there is a pass parameter in the argument, follow the parameters of the argument, and if not, follow the default preset value directly. Convenient and flexible. The default parameter is always after the position parameter.
3, dynamic parameters, not to know for the time being today.

End
2018-3-28

The python_day09_ function of iron Music learning

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.