Python Learning note 003_ function

Source: Internet
Author: User

>>> # Functions
>>> def myfirstfunction (Params1,params2 ...):
Print ("This is my first function!")
Print ("How are you, Python?")


>>> #调用函数
>>>
>>> myfirstfunction ()
This is my first function!
How are you, Python?

The return value of the >>> # function return

# function Documentation , which is the description of the function, as in Java/** */
>>> # It is represented in the form of a string
>>>
>>> def myFunction1 (name):
' This is the function document: the name in the function definition process is called the parameter '
# because it's just a form that means occupying a parameter position
Print ("Pass in +name+" is called an argument, because it is the parameter value of the specific point! ")


>>> myFunction1 (' CxN ')
The CXN passed in is called an argument, because it is the parameter value of the specific point!
>>>
>>> # Functions also have some special properties, such as displaying the properties of a function document
>>> # Special properties are basically the end of __
>>> myfunction1.__doc__
' This is the function document: the name in the function definition process is called the parameter '
>>>

>>>
>>> # keyword parameter to specify a name when passing a value to avoid passing an error
>>>
>>> def myFunction2 (name,words):
Print (name+ "--" +words)


>>> myFunction2 ("Yangw", "Eat?")
Have you eaten the yangw-->?
>>> MyFunction2 ("Eat it?", "Yangw")
Did you eat?-->yangw
>>> myFunction2 (words= "ate?", name= "Yangw")
Have you eaten the yangw-->?
>>>

>>> #默认参数, is the parameter that defines the default value
>>> def myFunction3 (name= "Yangw", words= "eat"):
Print (name+ "--" +words)


>>> MyFunction3 ()
Yangw-->, did you eat?
>>> MyFunction3 ("CxN")
Cxn-->, did you eat?
>>>

The >>> # keyword argument is that when a function is called, it specifies the parameter that needs to be assigned by the parameter name, so it's not afraid to make an error by confusing the order of the arguments
>>> #默认参数是在函数定义的时候, the initial value is assigned to the parameter, and when the function call forgets to pass the argument, the initial value is used

>>> collect parameters (also called variable parameters), just precede the parameters with *

def test (*params):
Print ("parameter length is:", Len (params))
Print ("The value of the first parameter:", Params[0])

>>> Test (1, "Yangw", "haha")
Parameter length is: 3
The value of the first parameter: 1
>>> If there are parameters after the collection parameter, the function call is passed with the keyword argument

>>> to prevent errors, use the default parameters when defining functions

>>> It's best to define the collection parameters at the end of the

>>> def test2 (lala,*params):
Print ("parameter length is:", Len (params), Lala)


>>> test2 ("Nini", 2, "we")
The parameter length is: 2 Nini
>>>

Python Learning note 003_ 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.