What is the difference of (*a) and (a) in this program???
def func(*a):
for i in range (0,len(a)-1):
if a[i]==3 and a[i+1]==3:
return True
return False
print(func([1,3,3]))
Output:- False
def func(a):
for i in range (0,len(a)-1):
if a[i]==3 and a[i+1]==3:
return True
return False
print(func([1,3,3]))
Output:-True
Top comments (2)
The
*
syntax is for passing a variable number of arguments. Since the function is called with a single argument,a
is a list containing a single item:[[1,3,3]]
solen(a)
is 1.Whereas in the second version
a
is[1,3,3]
andlen(a)
is 3.There's a good explanation of *args and **kwargs on saltycrane.com/blog/2008/01/how-to....
Thanks a lot