"python017--function Object 1"

Source: Internet
Author: User

First, the function

1. Define function: def function name ():

2. Calling function: The name of the direct write function ()

>>> def myfirstfunction ():
Print (' This my first function ')
Print (' I'm excited .... ‘)

>>> myfirstfunction ()
This my first function
I am very excited ....
>>>

3. Parameters of the function

>>> def myfirstfunction (name):
Print (name+ ' I love you! ‘)

>>> myfirstfunction (' dusty ')
Dust-laden I love you!
>>>

To set multiple parameters, just separate them with commas

>>> def Add (num1,num2):
result = Num1+num2
Print (Result)

>>> add
3
>>>

Second, exercises

1. What is dry

It is emphasized that the same code should not be repeated when programming, it is best to write it only once, and then it can be referenced elsewhere. The advantage is that you can increase the code reuse rate, reduce the amount of code, and also help improve the readability and maintainability of your code. When you need to make changes, you only need to change one place

2. Can a function have more than one parameter?

Theoretically yes, except that if the function has too many parameters, the chance of error will be greatly improved when the call is called, preferably a thin parameter

3. What keywords to use to create functions, and what to pay attention to
Keywords: def It is important to note that the function name is preceded by a parenthesis and a colon

"python017--function Object 1"

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.