Python Tutorial

Tuesday, July 3, 2012

Positional arguments

Sometime we need pass variable number of parameters through a function.It is very easy in python. Just use a simple asterisk(*) before an argument name, then it accept any number of positional arguments.


All source code available on github

def add(*args):
    total = 0
    for n in args:
        total += n
    return total


print add(1)
print add(1,4,5)
print add(9,3)
print add(2.9,5,3,8)

Output:
1
10
12
18.9