future 模块
python 3.x引进了一些python2.7.x不兼容的关键字和功能,所以我们可以通过在python 2中导入future模型进行使用python3的特性.
如果我们想在python 2中使用python 3的整除,那么就可以导入下列:
from __future__ import division
print 函数
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