Python Learning -9.python function definition

Source: Internet
Author: User

First, define a basic function as an example:

1 def Print (msg): 2     Print (msg)

The function name is print, the parameter has one, for MSG, the function body calls the print system function, Output MSG.

The next step is the variable parameter, which is a special feature, like the params in C #, but different.

Example 1:

1 def Printtuple (*args):2for in       args:3         print (ARG) 4 5 printtuple ('Tom','Mary','  Lucy')

Output Tom Mary Lucy in turn.

It is important to note that the args parameter in the Printtuple function is a tuple, that is, a tuple, which is not modifiable. That

1 args[0]='other'

It is worth noting that an exception will occur, unlike the params in C # that encapsulates the parameter as an array.

Example 2:

1 def Printdict (* *args):2for in       args:3         print ("key:"", Value:" + str (args[arg])) 4 5 printdict (tom=18,mary=20)

With two asterisks, args becomes a list.

Note, the STR function converts the argument to a string. In this example, the value of the args dictionary is not implicitly converted to string in Integer,python, so the STR function is used to convert.

Python Learning -9.python function definition

Related Article

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.