Article From:https://www.cnblogs.com/suyuloying/p/9122539.html
#!/usr/bin/python
# -*- coding: utf-8 -*-
def powersum (power,*args): #All the extra parameters will be stored in a args as a tuple.
    sum=0
    for i in args:
        sum+=pow(i,power)
    return sum
 
 
print powersum (2,10)
print powersum (2,3,4)#3And 4, the value of the args is the bottom, and the 2 is the index.
print powersum (3,10)
print powersum (3,3,4)#3And 4, the value of the args is the bottom, and the 3 is the index.
print powersum (7,3,4)#3And 4, the value of the args is the bottom, and the 7 is the index.
print powersum (10,3,4,6)#3、4、6It is the base number of the args ancestors, and the 10 is the index.
print powersum (0.5,2)#2It is the base number of the args ancestors, and the 0.5 is the index.
        
    
Running results:
100
25
1000
91
18571
61573801
1.41421356237
 
 
Remarks:
①When the function wants to receiveThe parameters of the form of a tuple or dictionaryAnd there’s a special way to do it.Use * and * * prefix
②In the example, because of the * prefix before the args variable, all the redundant function parameters will be stored in args as a tuple. If you use a * prefix, the extra parameters will be considered as a dictionary key / value pair.

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *