Python Beginner Learning Basics function-variable Parameters *

Source: Internet
Author: User

Variable Parameters (*)

Variable parameters, as the name implies, its parameters are variable, such as lists, dictionaries and so On. If we need a function to handle a variable number of parameters, we can use variable Parameters.

When we look at a lot of Python source code, we often see a function (* parameter 1, * * parameter 2) such a function definition, the * parameter and the * * parameter is a variable parameter, for a moment will be a bit confusing. In fact, as long as the definition of variable parameters of the function is clear, it is not difficult to understand.

When we don't know how to define a function with a few parameters, variable parameters can be Used.

In python, the parameter with * is used to accept a variable number of Parameters.

If a function is defined as Follows:

def functiontest (*args): ...      .      ....      ....  

Called when we can call This:

FunctionTest (1)  or  functiontest(  or  functiontest)

You can pass in multiple parameters Later.

Take a look at the example code and see how it applies:

def get_sum (*numbers):      = 0      for in  numbers:          + = N       return sum         # write down your code here to call get_sum for 5 numbers and output this  result Print (get_sum (1,2,3,4,5))  

The result would be?

more Learning content, on the code Bud net, Http://www.mayacoder.com/lesson/index

Python Beginner Learning Basics function-variable Parameters *

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.