这段代码中最后一个逗号的用法是什么?

lam*_*988 9 python

for x in range(1, 11):
     print repr(x).rjust(2), repr(x*x).rjust(3),
     # Note trailing comma on previous line
     print repr(x*x*x).rjust(4)
Run Code Online (Sandbox Code Playgroud)

结果:

 1   1    1
 2   4    8
 3   9   27
 4  16   64
 5  25  125
 6  36  216
 7  49  343
 8  64  512
 9  81  729
10 100 1000
Run Code Online (Sandbox Code Playgroud)

如果它是一个行继续符号,为什么作者可以再次写入Print语句?

如果我删除打印:

for x in range(1, 11):
     print repr(x).rjust(2), repr(x*x).rjust(3),
     # Note trailing comma on previous line
     repr(x*x*x).rjust(4)
Run Code Online (Sandbox Code Playgroud)

结果:

 1   1  2   4  3   9  4  16  5  25  6  36  7  49  8  64  9  81 10 100
Run Code Online (Sandbox Code Playgroud)

显然最后一行是忽略的.为什么?是因为它不是声明吗?

如果我把最后一个表达式放回第二行:

for x in range(1, 11):
     print repr(x).rjust(2), repr(x*x).rjust(3), repr(x*x*x).rjust(4)
Run Code Online (Sandbox Code Playgroud)

结果:

 1   1    1
 2   4    8
 3   9   27
 4  16   64
 5  25  125
 6  36  216
 7  49  343
 8  64  512
 9  81  729
10 100 1000
Run Code Online (Sandbox Code Playgroud)

bra*_*ers 19

它停止print在文本末尾打印换行符.

正如戴夫指出的那样,文件说:...... "除非print语句以逗号结尾,否则最后会写出'\n'字符."

  • 文档:http://docs.python.org/reference/simple_stmts.html#grammar-token-print_stmt."除非print语句以逗号结尾,否则最后会写出'\n'字符." (4认同)