How does the * operator work on a tuple in Python?

PythonServer Side ProgrammingProgramming

The star(*) operator unpacks the sequence/collection into positional arguments. So if you have a tuple and want to pass the items of that tuple as arguments for each position as they are there in the tuple, instead of indexing each element individually, you could just use the * operator. 

example

def multiply(a, b):
  return a * b
values = (1, 2)
print(multiply(*values))

This will unpack the tuple so that it actually executes as −

print(multiply(1, 2))

Output

This will give the output −

2
raja
Published on 05-Jan-2018 17:15:30
Advertisements