This article brings you an introduction to the usage of * in python (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. Represents multiplication operation
2. Represents multiples
def T(msg,time=1): print((msg+' ')*time) T('hi',3)
Result: hi hi hi
3. Single*
3.1 The
*parameter that appears in the formal parameter position of a function definition is used to accept any number of parameters and put them in a tuple.
def demo(*p): print(p) demo(1,2,3)
Result: (1, 2, 3)
3.2 Appears in the actual parameter position of the function call
When the function calls multiple parameters, it appears in the list or tuple , collections, dictionaries and other iterable objects as actual parameters, and add * in front, the interpreter will automatically unpack and pass to multiple single-variable parameters (the number of parsed parameters must be equal to the number of function parameters) .
a=[1,2,3] d(*a)
Result: 1 2 3
4. Two ** appear in the formal parameter part of the function definition
For example: **parameter is used to receive keys similar to Put multiple actual parameters in the same assignment form as parameters into the dictionary (that is, convert the parameters of the function into a dictionary).
def demo(**p): for i in p.items(): print(i) demo(x=1,y=2)
Result: ('x', 1) ('y', 2)
The above is the detailed content of Introduction to the usage of * in python (code example). For more information, please follow other related articles on the PHP Chinese website!