python--calculates the product of n number by variable parameters __python

Source: Internet
Author: User
the product of the number of n calculated by variable parameters:

The code is as follows:

List = []
def the_input (Input ("Enter the total number of multipliers:")): for


    I in range (count):
        n=eval (Input ("Enter multiplier:"))
        list.append (N)
    print ("A total", count, "number to multiply")
    print ("Put these multiply in the list:", list)



The_input ()


def get_ Mul (*num):
    sum =1
    for n in num:
        sum = SUM * n
    ' return sum



print ("The end result of this shoe number multiplication is:", Get_mul (*list))
Run Results

Variable parameter Interpretation

In a Python function, you can also define variable parameters. As the name suggests, the variable parameter is the number of parameters passed in is variable, can be one, 2 to any, or 0.

We take the mathematics titled example, given a set of numbers a,b,c ..., please compute A2 + b2 + C2 + ....

To define this function, we must determine the input parameters. Because the number of parameters is not certain, we first think that we can put a,b,c ... As a list or tuple, so that the function can be defined as follows:

def calc (numbers):
sum = 0
For N in numbers:
sum = SUM + N * n
return sum

But when you call, you need to assemble a list or tuple first:

Calc ([1, 2, 3])
14
Calc ((1, 3, 5, 7))
84

If you take advantage of variable parameters, the way you call a function can be simplified as follows:

Calc (1, 2, 3)
14
Calc (1, 3, 5, 7)
84

So, we change the parameter of the function to the variable parameter:

Def calc (*numbers):
sum = 0
For N in numbers:
sum = SUM + N * n
return sum

When defining a variable parameter and defining a list or tuple parameter, only a * number is added to the parameter. Inside the function, the parameter numbers receives a tuple, so the function code is completely unchanged. However, when you call the function, you can pass in any of the arguments, including 0 parameters:

Calc (1, 2)
5
Calc ()
0

If you already have a list or tuple, what to do if you want to invoke a variable parameter. You can do this:

Nums = [1, 2, 3]
Calc (nums[0], nums[1], nums[2])
14

This kind of writing is certainly feasible, the problem is too cumbersome, so python allows you to add a * number in front of the list or tuple, the list or tuple elements into variable parameters to pass in:

Nums = [1, 2, 3]
Calc (*nums)
14

*nums says that all the elements of the Nums list are passed in as variable parameters. This kind of writing is quite useful and very common.

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.