Python function One

Source: Internet
Author: User

Reference: Https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 00137473843313062a8b0e7c19b40aa8f31bdc4db5f6498000

In Python, you define a function to use a def statement, write out the function name, parentheses, the arguments in parentheses, and a colon, and : then, in the indent block, write the function body, and the return value of the function is returned with a return statement.

Let's take the example of customizing a function that asks for an absolute value my_abs :

def my_abs(x):    if x >= 0: return x else: return -x

Please test it yourself and call to my_abs see if the result is correct.

Note that when the statement inside the function body executes, the function executes and returns the result as soon as it is executed return . Therefore, it is possible to implement very complex logic within a function through conditional judgments and loops.

If there is no return statement, the result is returned after the function is executed, except for the result None .

return Nonecan be abbreviated to return .

When defining functions, it is necessary to determine the number of function names and parameters;

If necessary, the data type of the parameter can be checked first;

The function body can return return the function result at any time;

Automatic when the function is finished and there are no return statements return None .

A function can return multiple values at the same time, but is actually a tuple.

Default function:

When setting default parameters, there are a few things to note:

The first is the required parameter before, the default parameter is behind, otherwise the Python interpreter will error (think about why the default parameters can not be placed in front of the required parameters);

The second is how to set the default parameters.

When the function has more than one parameter, the parameter with large change is put in front, and the parameters with small change are put back. Parameters with small variations can be used as default parameters.

What are the benefits of using default parameters? The biggest benefit is the ability to reduce the difficulty of calling a function.

The default parameter reduces the difficulty of a function call, and when more complex calls are required, more arguments can be passed. The function only needs to define one, whether it is a simple call or a complex call.

When there are multiple default parameters, when called, the default parameters can be provided in order, such as calls enroll(‘Bob‘, ‘M‘, 7) , meaning that, in addition name to gender these two parameters, the last 1 parameters are applied on the parameter age , and the city parameter is still using the default value because it is not provided.

You can also provide some default parameters in order, not sequentially. When you do not provide partial default parameters in order, you need to write the parameter names. For example, the call enroll(‘Adam‘, ‘M‘, city=‘Tianjin‘) , meaning that the city parameter is passed in the value, the other default parameters continue to use the default value.

Python function One

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.