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.