| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
>>> for i in range(1, 10): 
...     for j in range(1, i+1): 
...         print(i, "x", j, "=", i * j, end=' ') 
...     print("\n") 
这是什么意思啊,到end看不懂了
 
end 是 print 函数的一个默认参数,可以用 help 来看默认文档: 
- >>>help(print)
 
 - Help on built-in function print in module builtins:
 
 - print(...)
 
 -     print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
 
 -     
 
 -     Prints the values to a stream, or to sys.stdout by default.
 
 -     Optional keyword arguments:
 
 -     file:  a file-like object (stream); defaults to the current sys.stdout.
 
 -     sep:   string inserted between values, default a space.
 
 -     end:   string appended after the last value, default a newline.
 
 -     flush: whether to forcibly flush the stream.
 
  复制代码 
 
可以看出,print 中的 end 参数默认值是为 '\n' 即换行符,这也是 print 函数为什么能够自动换行的原因 
 
所以你重新赋值:print(i, "x", j, "=", i * j, end=' ')  就相当于将 end 的值改变 
 
而 end 参数的作用是 print 函数内所有参数打印结束后,追加打印一个 end ,所以此时你的代码就默认追加打印一个 ' ' 空格字符串 
 
 
 |   
 
 
 
 |