举例说明python2.7.X和python3.X以上版本的区别记录学习

future 模块

python 3.x引进了一些python2.7.x不兼容的关键字和功能,所以我们可以通过在python 2中导入future模型进行使用python3的特性.

如果我们想在python 2中使用python 3的整除,那么就可以导入下列:
from __future__ import division

future模块的另外一些功能

python 2
print 'Python', python_version()
print 'Hello, World!'
print('Hello, World!')
print "text", ; print 'print more text on the same line'
Python 2.7.6
Hello, World!
Hello, World!
text print more text on the same line
python 3
print('Python', python_version())
print('Hello, World!')

print("some text,", end="")  ##notice:不换行的方式.
print(' print more text on the same line')
Python 3.4.1
Hello, World!
some text, print more text on the same line

整数除法

特别注意的地方是在将python2代码移植到python 3的时候,整数除法是不会引起任何报错信息的,但它却会改变数值的精度. 所以在python 2使用除法时,建议使用future模块的division.

python 2
print 'Python', python_version()
print '3 / 2 =', 3 / 2
print '3 // 2 =', 3 // 2
print '3 / 2.0 =', 3 / 2.0
print '3 // 2.0 =', 3 // 2.0
Python 2.7.6
3 / 2 = 1
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0
python 3
print('Python', python_version())
print('3 / 2 =', 3 / 2)
print('3 // 2 =', 3 // 2)
print('3 / 2.0 =', 3 / 2.0)
print('3 // 2.0 =', 3 // 2.0)
Python 3.4.1
3 / 2 = 1.5
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0

Unicode

python 2 没有byte类型,而python 3中,我们有Unicode(utf-8) str, 和两种byte类型:byte和bytearray.

python 2
print type(unicode('this is like a python3 str type'))
print type(b'byte type does not exist')
print 'they are really' + b' the same'
print type(bytearray(b'bytearray oddly does exist though'))
<type 'unicode'>
<type 'str'>
they are really the same
<type 'bytearray'>
python 3
print('Python', python_version())
print('strings are now utf-8 \u03BCnico\u0394é!')
print('Python', python_version())
print('strings are now utf-8 \u03BCnico\u0394é!')
print('and Python', python_version(), end="")
print(' also has', type(bytearray(b'bytearrays')))
'note that we cannot add a string' + b'bytes for data'
Python 3.4.1
strings are now utf-8 μnicoΔé!

Python 3.4.1 has <class 'bytes'>

and Python 3.4.1 also has <class 'bytearray'>

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

<ipython-input-13-d3e8942ccf81> in <module>()
----> 1 'note that we cannot add a string' + b'bytes for data'


TypeError: Can't convert 'bytes' object to str implicitly

xrange

使用%timeit + func 的形式来测试该函数测运行效率.注意最好在ipython环境下使用%timeit.

python 2
In [3]: def test_range(n):
   ...:     for i in range(n):
   ...:         pass
   ...:     


In [4]: def test_xrange(n):
   ...:     for i in xrange(n):
   ...:         pass
   ...:     
In [6]: %timeit test_range(1000)
100000 loops, best of 3: 15.4 µs per loop

In [7]: %timeit test_xrange(1000)
The slowest run took 10.07 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 10.7 µs per loop
python 3

而python 3只有range(),并没有xrange()

print('Python', python_version())

print('\ntiming range()')
%timeit test_range(n)

print(xrange(10))
Python 3.4.1

timing range()
1000 loops, best of 3: 520 µs per loop

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)

<ipython-input-5-5d8f9b79ea70> in <module>()
----> 1 print(xrange(10))


NameError: name 'xrange' is not defined

contains method for range objects in python 3

Raising exceptions

python 2
python 3

Handling exceptions

python 2
python 3

next() 和.next()

python 2
python 3

For-loop 和global namespace leak

python 2
python 3

比较无顺序的类型

python 2
python 3

inputs 和input()

python 2
python 3

返回迭代对象

python 2
python 3

round

参考链接:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值