Python - what is the use of * as part of a function's parameter? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Python - what is the use of * as part of a function's parameter?

I'm comparing two chunks of code: 1) def mmm(x, y): print(type(x)) print(type(y)) mmm(1, 2, 3) #outputs TypeError: mmm() takes 2 positional arguments but 3 were given 2) def zzz(p1, *p2): print(type(p1)) print(type(p2)) zzz('a','b','c','d') #outputs <class 'str'> <class 'tuple'> Why is p2 of type tuple? Why doesn't the second code give a type error? What does * in the parameter do?

28th Sep 2020, 6:56 PM
Solus
Solus - avatar
1 Answer
+ 7
* is used as an assignment to any extra arguments that come after the main one in your case. you'll usually see *args. Any extra parameter would go in the iterable called p2. Here's an example of its use https://code.sololearn.com/cbaQlASs402g/?ref=app
28th Sep 2020, 7:03 PM
Slick
Slick - avatar