Python: Learn to create and call functions, python Functions

Source: Internet
Author: User

Python: Learn to create and call functions, python Functions

This article is about Python 4th. It mainly describes how to create and call functions.

Print (): The function len () used to print objects. It is the input () function that returns the object length. It is the function that allows users to input objects...

Simply put, a function is to put the object in and then get the processing result. There are a lot of built-in functions in Python, see this link: https://docs.python.org/3/library/functions.html

Python has limited built-in functions. To solve more problems, we need to learn to create functions by ourselves.

Def (define): defines a function arg (argument): parameter return: return result

Def and return are keywords. Python recognizes these specific keywords to know what programmers want. The colon following the parentheses in the first line is in English. If you do not enter Chinese characters or use them incorrectly, an error is returned.

Now that we understand the definition of a function, we can create a simple function by ourselves.

Design a weight converter:

Def a weight conversion function, input the conversion formula, and return the result. Then, call the custom function, set the parameter to 1200, and convert it to kg:

1 def weight_converter(g):2     weight = g / 10003     return str(weight) + 'kg'4 5 g2 = weight_converter(1200)6 print(g2)

Running result:

1.2kg

After this is done, you can try a function with a slightly more complex point.

Design a function to calculate the oblique edge length of a right triangle:

The two angular edges are used as parameters to determine the length of the Oblique edges:

1 def right_triangle_third_length (a, B): 2 third_length = (a ** 2 + B ** 2) ** (1/2) 3 return third_length4 5 c = right_triangle_third_length (3, 4) 6 print (c) # Call the function and print the result

In Python, you can use the ** operator to calculate the power multiplier. The running result is as follows:

5.0

Therefore, the process for creating a function is simply to use def to define the function, and then call the defined function to print the result.

Later, I will introduce the parameter transfer method and design my own functions.

Operating Environment: Python version, 3.6; PyCharm version, 2016.2; Computer: Mac

----- End -----

Author: du wangdan, Public Account: du wangdan, Internet product manager.

 

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.