Python Learning Note (c) function

Source: Internet
Author: User

  Function

  Call Function:

Python has built in a lot of useful functions that we can call directly. To know the details of the function, you can view the documentation for the Python website: https://docs.python.org/3/library/functions.html. You can also view the details of a function on the interactive command line through Help (function name).

When calling a function, the number of arguments passed in and the type of arguments passed in are not typeerror errors, and an error message is given.

The function name is actually a reference to a function object, and it is possible to assign the function name to a variable, which is equivalent to giving the function an "alias".

  To define a function:

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.

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 .

Null function: If you want to define an empty function that does nothing, you can use a pass statement. Easy to add code later.

Parameter check: When calling a function, if the number of arguments is incorrect, the Python interpreter will automatically check it out and throwTypeError;但是如果参数类型不对,Python解释器就无法帮我们检查。数据类型检查可以用内置函数isinstance()实现。

Returns multiple values: The Python function returns a tuple, but it is more convenient to write.

  

Python Learning Note (c) function

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.