python3.x与python2.7.x都是比较流行的版本,虽然建议现在的初学者开始学习python3.x的版本,但是还有很多的工程使用的是python2.7.x版本。观看代码的时候难免会出现一些问题。
在google上搜到比较新的python2与python3的区别。

主要区别:

1、__future__模块

如果你想在python2.x上使用python3.x的一些功能,只需要导入__future__模块里面的具体功能即可。

from __furute__ import division #比如你想在Python2中用python3中的division功能

2、print功能函数

python2中的print语句被python3中的函数print()代替,使用的时候我们必须使用括号将内容包起来,如果没有加括号的话,会被认为是语法错误:

>>> print 'Hello, World!'  #在python3中是语法错误File "<ipython-input-3-139a7c5835bd>", line 1print 'Hello, World!'^
SyntaxError: invalid syntax

3、整数除法

这个细节区别很重要,如果你的Python2的程序在python3上跑的话会出现严重错误,而解释器不会将这个看成语法错误。
所以,建议在python3中这样写:float(3)/2 或者 3/2.0,别写成3/2。这样可以避免程序从python3到python2转移过程中发生错误(当然也可以在python2中引入from __future__ import 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.0Python 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

4、Unicode

python2中有ASCII str()类型,与unicode()类型区别开,但是没byte类型
在python3中全部变为Unicode(utf-8) str 类型了,还有两个位类型:byte、bytearray

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

5、xrange函数

xrange()在python2.x中非常受欢迎,它一般用于创造一个可以迭代的object来实现循环或者list/set-dictionary相关等功能。
在python3中,函数range()有了xrange()的功能,故xrange()在python3中不再存在。python3中的range相当于生成器,如果想让它返回list则必须list(range(temp))

>>> import timeit>>> n = 10000
>>> def test_range(n):return for i in range(n):pass>>> def test_xrange(n):for i in xrange(n):pass
# Python 2>>> print 'Python', python_version()>>> print '\ntiming range()'
>>> %timeit test_range(n)>>> print '\n\ntiming xrange()'
>>> %timeit test_xrange(n)
# Python 2.7.6
timing range()
1000 loops, best of 3: 433 µs per loop
timing xrange()
1000 loops, best of 3: 350 µs per loop
Python 3
>>> print('Python', python_version())
>>> print('\ntiming range()')
>>> %timeit test_range(n)
Python 3.4.1
timing range()
1000 loops, best of 3: 520 µs per loop
print(xrange(10))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)<ipython-input-5-5d8f9b79ea70> in <module>()
----> 1 print(xrange(10))NameError: name 'xrange' is not defined

Python3中range()的contains方法

range在python3中拥有了一个新的方法__contains__。这个方法可以加速对元素的”访问”,尤其对整数或者布尔型变量。

>>> x = 10000000
>>> def val_in_range(x, val):return val in range(x)
>>> def val_in_xrange(x, val):return val in xrange(x)
>>> print('Python', python_version())
>>> assert(val_in_range(x, x/2) == True)
>>> assert(val_in_range(x, x//2) == True)
>>> %timeit val_in_range(x, x/2)
>>> %timeit val_in_range(x, x//2)
Python 3.4.1
1 loops, best of 3: 742 ms per loop
1000000 loops, best of 3: 1.19 µs per loop

6、触发异常

在python2中支持两种语法表示(新的和旧的),但是在python3中则不行,使用旧语法会触发“语法错误”:

# Python 2>>> print 'Python', python_version()
Python 2.7.6
>>> raise IOError, "file error"
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)<ipython-input-8-25f049caebb0> in <module>()
----> 1 raise IOError, "file error"IOError: file error
>>> raise IOError("file error")
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)<ipython-input-9-6f1c43f525b2> in <module>()
----> 1 raise IOError("file error")IOError: file error# Python 3>>> print('Python', python_version())
Python 3.4.1
>>> raise IOError, "file error"File "<ipython-input-10-25f049caebb0>", line 1raise IOError, "file error"^
SyntaxError: invalid syntax
# The proper way to raise an exception in Python 3:>>> print('Python', python_version())
>>> raise IOError("file error")
Python 3.4.1---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)<ipython-input-11-c350544d15da> in <module>()1 print('Python', python_version())
----> 2 raise IOError("file error")OSError: file error

7、处理异常

同样,python3和python2有一点小的区别,多了一个“as”小尾巴:

# Python 2>>> print 'Python', python_version()
>>> try:let_us_cause_a_NameErrorexcept NameError, err:print err, '--> our error message'
Python 2.7.6
name 'let_us_cause_a_NameError' is not defined --> our error message# Python 3>>> print('Python', python_version())
>>> try:let_us_cause_a_NameErrorexcept NameError as err:print(err, '--> our error message')
Python 3.4.1
name 'let_us_cause_a_NameError' is not defined --> our error message

8、next()函数和.next()方法

因为next()(.next())是一个常用的功能函数,所有在python3中进行了小的修改(或者说小的提升),在python2中上面两个都可以用,但是在python3中,next()可以用但是.next()则会引起 AttributeError 错误

# Python 2>>> print 'Python', python_version()>>> my_generator = (letter for letter in 'abcdefg')>>> next(my_generator)
>>> my_generator.next()
>
Python 2.7.6
'b'# Python 3>>> print('Python', python_version())>>> my_generator = (letter for letter in 'abcdefg')>>> next(my_generator)
Python 3.4.1
'a'>>> my_generator.next()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)<ipython-input-14-125f388bb61b> in <module>()
----> 1 my_generator.next()AttributeError: 'generator' object has no attribute 'next'

9、for-loop循环中的变量不再影响全局空间中的变量

在Python3.x中for-loop中的变量不再对全局变量有影响。
另外在python3的介绍中:“List解释器不再支持这样的形式[… for var in item1,item2].应该换成[… for var in (item1,item2,…)]。新版的LIst解释器可以说在list()构造器里面有一个生成器表达式,因此循环中变量不再影响上下文中的变量”

# Python 2>>> print 'Python', python_version()
>>> i = 1
>>> print 'before: i =', i
>>> print 'comprehension: ', [i for i in range(5)]
>>> print 'after: i =', iPython 2.7.6
before: i = 1
comprehension:  [0, 1, 2, 3, 4]
after: i = 4# Python 3>>> print('Python', python_version())
>>> i = 1
>>> print('before: i =', i)
>>> print('comprehension:', [i for i in range(5)])
>>> print('after: i =', i)Python 3.4.1
before: i = 1
comprehension: [0, 1, 2, 3, 4]
after: i = 1

10、比较非有序类型

在python3中如果对不可排序的类型进行比较时,会触发TypeError类型错误。

# Python 2>>> print 'Python', python_version()
>>> print "[1, 2] > 'foo' = ", [1, 2] > 'foo'
>>> print "(1, 2) > 'foo' = ", (1, 2) > 'foo'
>>> print "[1, 2] > (1, 2) = ", [1, 2] > (1, 2)Python 2.7.6
[1, 2] > 'foo' =  False
(1, 2) > 'foo' =  True
[1, 2] > (1, 2) =  False# Python 3>>> print('Python', python_version())
>>> print("[1, 2] > 'foo' = ", [1, 2] > 'foo')
>>> print("(1, 2) > 'foo' = ", (1, 2) > 'foo')
>>> print("[1, 2] > (1, 2) = ", [1, 2] > (1, 2))Python 3.4.1---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)<ipython-input-16-a9031729f4a0> in <module>()1 print('Python', python_version())
----> 2 print("[1, 2] > 'foo' = ", [1, 2] > 'foo')3 print("(1, 2) > 'foo' = ", (1, 2) > 'foo')4 print("[1, 2] > (1, 2) = ", [1, 2] > (1, 2))TypeError: unorderable types: list() > str()

11、通过input()解释用户输入

Python3对input()函数进行了修整。这样它就可以将用户的输入保存为str类型。而在python2中,为了避免使用input()读入非strings类型的危险操作,通常使用raw_input()函数。

# Python 2>>> my_input = input('enter a number: ')
enter a number: 123
>>> type(my_input)
<type 'int'>
>>> my_input = raw_input('enter a number: ')
enter a number: 123
>>> type(my_input)
<type 'str'>
Python 3# Python 3>>> my_input = input('enter a number: ')
enter a number: 123
>>> type(my_input)
<class 'str'>

12、返回可迭代的对象,而不是list对象

在上面我们已经对xrange的变化有所了解,一些函数或者方法在python3中会返回可迭代的对象,不像在python2中,是返回一个list。
如果Python2程序中有返回list类型的函数,移植到Python3中就需要进行修改,这很简单,只需使用list()函数即可。

# Python 2>>> print 'Python', python_version()
>>> print range(3)
>>> print type(range(3))Python 2.7.6
[0, 1, 2]
<type 'list'># Python 3>>> print('Python', python_version())
>>> print(range(3))
>>> print(type(range(3)))
>>> print(list(range(3)))Python 3.4.1
range(0, 3)
<class 'range'>
[0, 1, 2]

注意,有一些常用的函数在python3中不再返回list类型:
zip()
map()
filter()
dictionary’s .keys() method
dictionary’s .values() method
dictionary’s .items() method

13、Round函数

在python3中,四舍五入更“倾向于”到偶数的那一边。

# Python 2>>> print 'Python', python_version()
Python 2.7.12
>>> round(15.5)
16.0
>>> round(16.5)
17.0# Python 3>>> print('Python', python_version())
Python 3.5.1
>>> round(15.5)
16
>>> round(16.5)
16

其他区别

1、编码

Python3.x源码文件默认使用utf-8编码,这就使得以下代码是合法的:

    >>> 中国 = 'china' >>>print(中国) china 

2、语法

1、去除了<>,全部改用!=
2、去除“,全部改用repr()
3、关键词加入as 和with,还有True,False,None
4、加入nonlocal语句。使用noclocal x可以直接指派外围(非全局)变量
5、改变了顺序操作符的行为,例如x

# python2guess = int(raw_input('Enter an integer : ')) # 读取键盘输入的方法
# python3guess = int(input('Enter an integer : '))

7、去除掉了元组参数解包。不能def(a, (b, c)):pass这样定义函数了
8、使用了新的8进制字变量,相应地修改了oct()函数。

# python2 >>> 0666 438 >>> oct(438) '0666'
# python3
>>> 0666 SyntaxError: invalid token (<pyshell#63>, line 1)
>>> 0o666 438
>>> oct(438) '0o666' 

9、增加了 2进制字变量和bin()函数

>>> bin(438) '0b110110110'
>>> _438 = '0b110110110'
>>> _438 '0b110110110' 

10、扩展的可迭代解包。在python3.x里,a, b, *rest = seq和 *rest, a = seq都是合法的,只要求两点:rest是list 对象和seq是可迭代的。
11、新的super(),可以不再给super()传参数,

>>> class C(object): def __init__(self, a): print('C', a)
>>> class D(C): def __init(self, a): super().__init__(a)  # 无参数调用super()
>>> D(8) C 8 <__main__.D object at 0x00D7ED90> 

12、新的metaclass语法:
class Foo(*bases, **kwds):
pass
13、支持class decorator类修饰。用法与函数decorator一样:

>>> def foo(cls_a): def print_func(self): print('Hello, world!') cls_a.print = print_func return cls_a
>>> @foo class C(object): pass
>>> C().print() Hello, world! 

3、字符串和字节串

1、现在字符串只有str一种类型,和python2.x版本的unicode是一样的。
2、数据类型 :python3.x去除了long类型,现在只有一种整型——int,但它的行为就像python2.x版本的long ;新增了bytes类型,对应于python2.x的八位字符,定义一个bytes字面量的方法如下:

>>> b = b'china'
>>> type(b) <type 'bytes'> 

3、str对象和bytes对象可以使用.encode() (str -> bytes) or .decode() (bytes -> str)方法相互转化。

>>> s = b.decode()
>>> s 'china'
>>> b1 = s.encode()
>>> b1 b'china' 

4、dict的.keys()、.items 和.values()方法返回迭代器,而之前的iterkeys()等函数都被废弃。同时去掉的还有 dict.has_key(),用 in替代。

4、面向对象

1、引入抽象基类(Abstraact Base Classes,ABCs)。
2、容器类和迭代器类被ABCs化,所以cellections模块里的类型比python2.x多了很多。

>>> import collections
>>> print('\n'.join(dir(collections))) Callable Container Hashable ItemsView Iterable Iterator KeysView Mapping MappingView MutableMapping MutableSequence MutableSet NamedTuple Sequence Set Sized ValuesView __all__ __builtins__ __doc__ __file__ __name__ _abcoll _itemgetter _sys defaultdict deque 

另外,数值类型也被ABCs化。关于这两点,请参阅 PEP 3119和PEP 3141。
3、迭代器的next()方法改名为next(),并增加内置函数next(),用以调用迭代器的next()方法
4、增加了@abstractmethod和 @abstractproperty两个 decorator,编写抽象方法(属性)更加方便。

5、异常

1、所以异常都从 BaseException继承,并删除了StardardError
2、去除了异常类的序列行为和.message属性
3、用 raise Exception(args)代替 raise Exception, args语法

6、模块变动

1、移除了cPickle模块,可以使用pickle模块代替。最终我们将会有一个透明高效的模块。
2、移除了imageop模块
3、移除了 audiodev, Bastion, bsddb185, exceptions, linuxaudiodev, md5, MimeWriter, mimify, popen2,
rexec, sets, sha, stringold, strop, sunaudiodev, timing和xmllib模块
4、移除了bsddb模块(单独发布,可以从http://www.jcea.es/programacion/pybsddb.htm获取)
5、移除了new模块
6、os.tmpnam()和os.tmpfile()函数被移动到tmpfile模块下
7、tokenize模块现在使用bytes工作。主要的入口点不再是generate_tokens,而是 tokenize.tokenize()

7,杂项

1、bytes对象不能hash,也不支持 b.lower()、b.strip()和b.split()方法,但对于后两者可以使用 b.strip(b’
\n\t\r \f’)和b.split(b’ ‘)来达到相同目的
2、zip()、map()和filter()都返回迭代器。而apply()、 callable()、coerce()、 execfile()、reduce()和reload ()函数都被去除了,现在可以使用hasattr()来替换 callable(). hasattr()的语法如:hasattr(string, ‘name‘)
3、string.letters和相关的.lowercase和.uppercase被去除,请改用string.ascii_letters 等
4、如果x < y的不能比较,抛出TypeError异常。2.x版本是返回伪随机布尔值的
5、getslice系列成员被废弃。a[i:j]根据上下文转换为a.getitem(slice(I, j))或 setitem
delitem调用
6、file类被废弃::

# python2
>>> file <type 'file'>
# python3
>>> file Traceback (most recent call last): File "<pyshell#120>", line 1, in <module> file NameError: name 'file' is not defined

参考链接:https://docs.python.org/3/whatsnew/3.0.html
https://www.cnblogs.com/codingmylife/archive/2010/06/06/1752807.html

python3.x与python2.x的区别汇总相关推荐

  1. Python3.x和Python2.x的区别[转]

    Python3.x和Python2.x的区别 1.性能  Py3.0运行 pystone benchmark的速度比Py2.5慢30%.Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可 ...

  2. python3.x和python2.x唯一区别_Python3.x和Python2.x的区别介绍

    1.性能 Py3.0运行 pystone benchmark的速度比Py2.5慢30%.Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可 以取得很好的优化结果. Py3.1性能比Py2 ...

  3. python3.x和python2.x唯一区别_Python3.x和Python2.x的区别 (转)

    http://www.jb51.net/article/57956.htm#more  引深:python.2x和python.3x的区别 原文链接: http://www.cnblogs.com/c ...

  4. Python3.x和Python2.x的区别

    1.性能  Py3.0运行 pystone benchmark的速度比Py2.5慢30%.Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可  以取得很好的优化结果.  Py3.1性能比 ...

  5. python3.x和python2.x唯一区别_Python3.x和Python2.x的区别

    {"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...

  6. python3.x 与python2.x print的区别 input区别

    #python3.x 与python2.x print的区别 input区别 #python3.x # print(x,y) #打印对象 # #python2.x # print x,y# #pyth ...

  7. python3的各个版本有什么区别_python不同版本的_new_不同点总结

    我们都知道python的版本不同,在使用的时候就有所区别.鉴于我们推荐小伙伴们选择python3版本,所以这方面的区别了解的不是很多.就拿_new_来说,在python2和3中的写法是不一样的,之前有 ...

  8. 显示2位小数 python3_python3+ 和 Python2+的一些区别

    一.性能 Python3.0速度比Python2.0慢一些 二.编码 Py3.X源码文件默认使用utf-8编码,这就使得以下代码是合法的: >>中国 ='china'>>pri ...

  9. python mkl freebsd_FreeBSD:在uwsgi中使用python3而不是python2

    我有一台安装了FreeBSD 10.1的服务器.我想使用uwsgi nginx python3在其上部署一个Django站点并遇到一些问题. 该网站是为python3编写的,我安装了python3.4 ...

最新文章

  1. 调驱动 一定注意 cache 表的
  2. 列了一些自己会但是不怎么精通的编程语言和知识,做个记录,空余时间加强学习...
  3. 架构为什么要以领域为核心
  4. Java面试会问的——数组、声明、初始化、冒泡、多维数组、稀疏数组
  5. 华为matebook14会不会用鸿蒙,2020年用什么笔记本上网课?浅析华为MateBook 14 2020款...
  6. 大数据_Flink_Java版_数据处理_时间语义(1)_时间语义概念---Flink工作笔记0049
  7. dart基础之异步编程
  8. 计算机硬件设备建档,捷通科技RFID 文件柜档案管理硬件方案
  9. GitLab中用户的五种权限 Guest、Reporter、Developer、Master、Owner
  10. mydisktest测试软件,MyDiskTest
  11. 旅行规划(travel)
  12. 导函数连续、可导、可微、连续、有界、可积的关系,史上最全!一张图搞定!
  13. 外贸企业邮箱是什么?大连邮箱,邮件归档系统
  14. KETTLE 列转行
  15. 流媒体学习之路(WebRTC)——GCC分析(2)
  16. 如何利用云服务器快速部署一个静态网页
  17. Cesium入门(五):加载WMTS瓦片地图服务
  18. h5发送短信以及判别用户浏览器版本
  19. python中矩阵的表示方法_在python中创建数字的二进制表示形式的矩阵 - python
  20. Spring Cloud与Docker微服务架构实战 PDF版 内含目录

热门文章

  1. 远程桌面计算机让输入密码,解决Windows远程桌面连接每次都提示输入密码的问题...
  2. 拾谈“用最有效率的方法算出2乘以8等於几?”
  3. mysql8.11安装_MySQL-mysql 8.0.11安装教程
  4. mysql5.6 icp mrr bak_【mysql】关于ICP、MRR、BKA等特性
  5. linux光驱驱动目录,linux下挂载光驱
  6. 一文帮你了解小型直流有刷电机内部结构
  7. WS2812串行可控彩色LED灯珠
  8. STC单片机功率控制下载板
  9. 深度神经网络控制的巡线智能车
  10. html中图片只是一个小图标,如何用css显示一个图片中多个小图标?