请在Python中解释以下代码的工作原理 2. end有什么用?还有 __future__ 个图书馆

Please explain the working of the following code in Python 2. What is the use of end? Also __future__ library

from __future__ import print_function
if __name__ == '__main__':
    n = int(raw_input())
    for n in range(n):
        print(((n)+1),end='')

还有为什么我们不能使用下面的:

for n in range(n):
    print n,

从顶部开始:

在Python 2.x中,print是一个语句,不是一个函数,因此有一定的局限性。随着 Python 3 的发展,创建了 print() 函数。这个函数非常有用,以至于 Python 2.x 人员提供了一种方法,可以通过使用 从 __future__ 导入 print_function 能力(注意:你需要双下划线)。

from __future__ import print_function

print() 函数中包含的功能之一是能够包含字符串以分隔多个值(sep=' ' 是默认值)并包含字符串以附加到打印的末尾值(end='\n' 是默认值)。

在这种情况下,end='' 在打印值的末尾放置一个空字符串。

if name == 'main': 
    n = int(raw_input()) 
    for n in range(n): 
        print(((n)+1),end='')

下一个:

一旦您导入了 print() 函数,它实际上会覆盖 print 语句,这就是您不能再使用它的原因。

>>> from __future__ import print_function
>>> for n in range(7):
...    print n,
  File "<stdin>", line 2
    print n,
          ^
SyntaxError: invalid syntax