[python] 从python2.x迁移到python3.x的常见操作

  1. print 'something' > print('something')
  2. xrange() > range()
  3. 除法division
    python 2.x : 整数 / 整数
    python 3.x : 整数 // 整数
    这个很难发现,因为python弱类型。
    python2.x中/是classic division,和C++一样,所以两个整数操作数会有整数结果,特别是可以自动取整。在python3.x中/是浮点除法,结果是浮点数,//是整数除法。

    python 2.x : integer / integer
    python 3.x : integer // integer
    this bug is hard to find as we cannot check the types since python does not require explicit declaration.
    / in python 2.x is the class division the same as in C++. Hence two integers as operands result in an integer. If the inherent flooring feature is desired, awareness should be raised when transferring to python 3.x. In python 3, / is used as merely float division and will produce a float in the above case, so // should be used to replace it because it supports the integer division.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值