How to pass a dynamic amount of arguments to a function in Python

2 Answers

0 votes
def sumAll(*args): 
    total = 0
    for n in args: 
        total += n 
    return total
  
print(sumAll(1, 2, 3, 4))
print(sumAll(1, 2, 3))
print(sumAll(1, 2, 3, 4, 5, 6))



  
'''
run:
  
10
6
21
         
'''

 



answered May 16, 2020 by avibootz
0 votes
def f(*arg):  
    for s in arg:  
        print(s, end=' ') 
    
f('python', 'php', 'java')  
print()
f('python', 'php', 'java', 'c', 'c++')  


  
'''
run:
  
python php java 
python php java c c++
         
'''

 



answered May 16, 2020 by avibootz
...