0
Can someone explain this code and it’s output to me it was in a code challenge with Sololearn(lvl 22)
def foo(a, *b): return a,b c = foo(1,2,3) print(c) #output (1,(2,3))#
1 ответ
+ 3
Okay let's go through this code together then:
def foo(a, *b) #initialises a function named foo taking arguments a and *b
return a,b #return the value contained in a and b
c=foo(1,2,3)#declares a variable with function foo(1,2,3)
print (c) # outputs (1,(2,3)) the output is so because the second argument of the function contains *b which returns a tuple with all the values which comes after the first argument a
Similarly, if the code was :
def foo(a,*b,c)
return a,b,c
c= foo(1,2,3,4,5,6)
print(c)
#output (1,(2,3,4,5),6)