Python Learning 2 function (Liaoche)

Source: Internet
Author: User

first, the function

1. Calling functions

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

# variable a points to the ABS function # so you can also call the ABS function by a 1

2. Defining functions

Define the function with a DEF statement, write down the function name, parentheses, the arguments in parentheses, and the colon: and then write the function body in the indent block clock.

For example, define a my_abs function that evaluates to an absolute value:

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

If you have already saved the code for the My_abs () function as a funct1.py file, switch to the funct1.py storage directory with the CD C:\work after you open cmd and enter the python To start the Python interpreter and then enter the from funct1 import my_abs to import the My_abs () function, you can enter a cmd such as my_abs ( -97) to invoke the function and display the result

Note:

(1) When defining functions, you need to determine the number of function names and parameters

(2) When the function is finished and there is no return statement, auto return None

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

3. Parameters of the function

Python Learning 2 function (Liaoche)

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.