在Python中使用星号(star)运算符,并附加了多个含义。
对于数字数据类型,*用作乘法运算符
>>> a=10;b=20 >>> a*b 200 >>> a=1.5; b=2.5; >>> a*b 3.75 >>> a=2+3j; b=3+2j >>> a*b 13j
对于字符串,列表和元组之类的序列,*是重复运算符
>>> s="Hello" >>> s*3 'HelloHelloHello' >>> L1=[1,2,3] >>> L1*3 [1, 2, 3, 1, 2, 3, 1, 2, 3] >>> T1=(1,2,3) >>> T1*3 (1, 2, 3, 1, 2, 3, 1, 2, 3)
函数声明中使用的单个星号允许从调用环境传递可变数量的参数。在函数内部,它表现为元组。
>>> def function(*arg): print (type(arg)) for i in arg: print (i)
>>> function(1,2,3) <class 'tuple'> 1 2 3