python2的print和python3的print()
前言
今天试着分别在python2和python3环境下运行如下.py文件
def yield_test(n):
for i in range(n):
yield call(i)
print("i=",i)
print("do something.")
print("end.")
def call(i):
return i*2
for i in yield_test(5):
print(i,",")
结果如下:
很明显可以看出,python2.7下print把全部东西都输出,包括 括号;而在python3.6中就不会。然后上网查证,其实,在python2.x中的print不是个函数,输出格式如下:
>>> print "There is only %d %s in the sky."%(1,'sun')
There is only 1 sun in the sky.
而在python3.x中的print成了函数,输出格式如下:
>>> print("There is only %d %s in the sky."%(1,'sun'))
There is only 1 sun in the sky.
本文通过示例对比了Python2与Python3中print的不同之处。在Python2中,print不是一个真正的函数,而在Python3中,print成为了标准函数。文章展示了不同版本下print的使用方法及输出格式。
2267

被折叠的 条评论
为什么被折叠?



