What is the python parameter? Examples of prerequisites and keyword parameters

Source: Internet
Author: User
In this article we will look at the parameters in Python, the parameters in Python is a more important and commonly used knowledge points. Example python3 keyword ParametersLater use also more, in Baidu on a search python keywordIt will pop out. So in this article, let's study. python keyword Parametersand python required parameters.

First we need to know the type of the parameter, as follows:

Type of parameter:

Required Parameters

Keyword parameters

Default parameters

Indefinite length parameter

Essential functions

The required parameters must be passed into the function in the correct order. The number of calls must be the same as when declared. Call the Printme () function, you must pass in a parameter, or a syntax error will occur:

#!/usr/bin/python#-*-coding:utf-8-*-#可写函数说明def printme (str):   "Print any incoming string" Print   str;   Return #调用printme函数printme ();

The result of the above example output:

Traceback (most recent):  File "test.py", line one, in <module>    printme ();  Typeerror:printme () takes exactly 1 argument (0 given)

Let's take a look at the keyword parameter

Keyword parameters:

Keyword arguments are closely related to function calls, and function calls use keyword parameters to determine the values of the parameters passed in.

Using the keyword argument allows a function call when the order of the arguments is inconsistent with the Declaration, because the Python interpreter can match the parameter values with the name of the argument.

The following instance uses the parameter name when the function Printme () is called:

#!/usr/bin/python#-*-coding:utf-8-*-#可写函数说明def printme (str):   "Print any incoming string" Print   str;   Return #调用printme函数printme (str = "My string");

The result of the above example output:

My string

The following example shows that the keyword parameter order is not important to show more clearly:

#!/usr/bin/python#-*-coding:utf-8-*-#可写函数说明def printinfo (name, age):   "Print any incoming string" Print   "name:", name;
  
   print "age", age;   Return #调用printinfo函数printinfo (age=50, name= "Miki");
  

The result of the above example output:

Name:  mikiage  50

The above is what we want to explain today, the classification of parameters and the parameters of the necessary parameters and keywords. Although the above knowledge point to give an example, but still need to try it yourself, after all, they do not do the same as on paper, hands-on practice is the best way to verify the learning. Finally, I hope that this article will give you a little help in learning about Python.

For more information, please visit the PHP Chinese Web Python tutorial section.

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.