-1

I am learning about complex function in python.

I understand the input to the complex function complex(a, b) and will return a+ bi. What I don't understand is when I input in tuples form, complex((a, b)), it will return an error but if I use complex(*(a, b)) it will return a+ bi. so in this case, is the asterisk particularly part of complex function's argument when giving it as a tuple or is it performing some kind of operation? Thanks.

JUJU derm
  • 3
  • 1

2 Answers2

0

Function complex(a,b) needs two parameters in this case. When you run this function like complex((a,b)) you are only giving the function one parameter - which is a tuple with two elements. Asterisk used like this unpacks the iterable- tuple

angroo
  • 1
0

The *-operator is used for argument unpacking.

You're not calling the function in the same way. Think of it this way:

complex(real=a, imaginary=b)
# vs.
complex(real=(a, b))

This is also why you see people write def fn(*args, **kwargs), it means that all arguments and keyword arguments will be unpacked.

ades
  • 227
  • 2
  • 14