Code:
def op(*ar):
for i in ar:
print(i)
arg=['yel','5',6]
op(arg)
Output: ['yel','5',6]
Code:
def op(ar):
for i in ar:
print(i)
arg=['yel','5',6]
op(arg)
Output:
yel
\n5
\n6(This output is understood)
Code:
def op(*ar):
for i in ar:
print(i)
arg=['yel','5',6]
op(arg)
Output: ['yel','5',6]
Code:
def op(ar):
for i in ar:
print(i)
arg=['yel','5',6]
op(arg)
Output:
yel
\n5
\n6(This output is understood)
This is easy....You see *args
takes all the arguments and passes it onto the function as a tuple.
So, when you write def op(*args):
you are actually passing a tuple containing the list ['yel', '5', '6']. The argument that is passed looks actually like this: (['yel', '5', '6'])
Now when you iterate through the tuple are inside the function, i there is each element of tuple, i.e, i in this case is the whole list ['yel', '5', '6'].
This is why you are getting a list when you print ar.
In contrary, in the second case the argument is passed as list and hence you can access each element when you iterate through it.