[英]What's the difference between these two scripts?
我想知道这两个脚本有什么区别,输出看起来很相似!
def test(a,b,c,*args):
print (a,b,c)
for n in args: print(n, end=' ')
test('aa','bb','cc',1,2,3,4)
输出:
aa bb cc
1 2 3 4
def test(a,b,c,*args):
print (a,b,c)
print (*args)
输出:
aa bb cc
1 2 3 4
其中一个循环单独打印每个值,但告诉print()
函数在每个值之后而不是换行符后打印一个空格。
第二个版本一次性打印所有值,中间有空格,最后是换行符。 args
每个值都作为单独的参数传递给print()
函数,就像您发出表达式print(args[0], args[1], args[2], args[3])
。
因此存在差异。 第一个版本打印:
1<space>2<space>3<space>4<space>
而第二个版本打印:
1<space>2<space>3<space>4<newline>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.