python2的print和python3的print()

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.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值