python 2和3版本的区别

版权声明:如需转载或引用,请注明出处。 https://blog.csdn.net/weixin_39278265/article/details/82985036

前言

虽然已经有很多网站都在讨论python 2和3版本的区别 [1] [2] [3] [4],但是我觉得还是不够全面,不够原汁原味。所以,这里打算通过探索,了解一下python2和3的区别。

1 python 总共有多少版本

根据 https://www.python.org/download/releases/ :

Python 3.5.6 2018-08-02  Download Release Notes
Python 3.4.9 2018-08-02  Download Release Notes
Python 3.7.0 2018-06-27  Download Release Notes
Python 3.6.6 2018-06-27  Download Release Notes
Python 2.7.15 2018-05-01  Download Release Notes
Python 3.6.5 2018-03-28  Download Release Notes
Python 3.4.8 2018-02-05  Download Release Notes
Python 3.5.5 2018-02-05  Download Release Notes
Python 3.6.4 2017-12-19  Download Release Notes
Python 3.6.3 2017-10-03  Download Release Notes
Python 3.3.7 2017-09-19  Download Release Notes
Python 2.7.14 2017-09-16  Download Release Notes
Python 3.4.7 2017-08-09  Download Release Notes
Python 3.5.4 2017-08-08  Download Release Notes
Python 3.6.2 2017-07-17  Download Release Notes
Python 3.6.1 2017-03-21  Download Release Notes
Python 3.4.6 2017-01-17  Download Release Notes
Python 3.5.3 2017-01-17  Download Release Notes
Python 3.6.0 2016-12-23  Download Release Notes
Python 2.7.13 2016-12-17  Download Release Notes
Python 3.4.5 2016-06-27  Download Release Notes
Python 3.5.2 2016-06-27  Download Release Notes
Python 2.7.12 2016-06-25  Download Release Notes
Python 3.4.4 2015-12-21  Download Release Notes
Python 3.5.1 2015-12-07  Download Release Notes
Python 2.7.11 2015-12-05  Download Release Notes
Python 3.5.0 2015-09-13  Download Release Notes
Python 2.7.10 2015-05-23  Download Release Notes
Python 3.4.3 2015-02-25  Download Release Notes
Python 2.7.9 2014-12-10  Download Release Notes
Python 3.4.2 2014-10-13  Download Release Notes
Python 3.3.6 2014-10-12  Download Release Notes
Python 3.2.6 2014-10-12  Download Release Notes
Python 2.7.8 2014-07-02  Download Release Notes
Python 2.7.7 2014-06-01  Download Release Notes
Python 3.4.1 2014-05-19  Download Release Notes
Python 3.4.0 2014-03-17  Download Release Notes
Python 3.3.5 2014-03-09  Download Release Notes
Python 3.3.4 2014-02-09  Download Release Notes
Python 3.3.3 2013-11-17  Download Release Notes
Python 2.7.6 2013-11-10  Download Release Notes
Python 2.6.9 2013-10-29  Download Release Notes
Python 3.3.2 2013-05-15  Download Release Notes
Python 3.2.5 2013-05-15  Download Release Notes
Python 2.7.5 2013-05-12  Download Release Notes
Python 3.2.4 2013-04-06  Download Release Notes
Python 2.7.4 2013-04-06  Download Release Notes
Python 3.3.1 2013-04-06  Download Release Notes
Python 3.3.0 2012-09-29  Download Release Notes
Python 3.2.3 2012-04-10  Download Release Notes
Python 2.6.8 2012-04-10  Download Release Notes
Python 2.7.3 2012-04-09  Download Release Notes
Python 3.1.5 2012-04-09  Download Release Notes
Python 3.2.2 2011-09-03  Download Release Notes
Python 3.2.1 2011-07-09  Download Release Notes
Python 2.7.2 2011-06-11  Download Release Notes
Python 3.1.4 2011-06-11  Download Release Notes
Python 2.6.7 2011-06-03  Download Release Notes
Python 2.5.6 2011-05-26  Download Release Notes
Python 3.2.0 2011-02-20  Download Release Notes
Python 3.1.3 2010-11-27  Download Release Notes
Python 2.7.1 2010-11-27  Download Release Notes
Python 2.6.6 2010-08-24  Download Release Notes
Python 2.7.0 2010-07-03  Download Release Notes
Python 3.1.2 2010-03-20  Download Release Notes
Python 2.6.5 2010-03-18  Download Release Notes
Python 2.5.5 2010-01-31  Download Release Notes
Python 2.6.4 2009-10-26  Download Release Notes
Python 2.6.3 2009-10-02  Download Release Notes
Python 3.1.1 2009-08-17  Download Release Notes
Python 3.1.0 2009-06-26  Download Release Notes
Python 2.6.2 2009-04-14  Download Release Notes
Python 3.0.1 2009-02-13  Download Release Notes
Python 2.5.4 2008-12-23  Download Release Notes
Python 2.5.3 2008-12-19  Download Release Notes
Python 2.4.6 2008-12-19  Download Release Notes
Python 2.6.1 2008-12-04  Download Release Notes
Python 3.0.0 2008-12-03  Download Release Notes
Python 2.6.0 2008-10-02  Download Release Notes
Python 2.4.5 2008-03-11  Download Release Notes
Python 2.3.7 2008-03-11  Download Release Notes
Python 2.5.2 2008-02-21  Download Release Notes
Python 2.5.1 2007-04-19  Download Release Notes
Python 2.3.6 2006-11-01  Download Release Notes
Python 2.4.4 2006-10-18  Download Release Notes
Python 2.5.0 2006-09-19  Download Release Notes
Python 2.4.3 2006-04-15  Download Release Notes
Python 2.4.2 2005-09-27  Download Release Notes
Python 2.4.1 2005-03-30  Download Release Notes
Python 2.3.5 2005-02-08  Download Release Notes
Python 2.4.0 2004-11-30  Download Release Notes
Python 2.3.4 2004-05-27  Download Release Notes
Python 2.3.3 2003-12-19  Download Release Notes
Python 2.3.2 2003-10-03  Download Release Notes
Python 2.3.1 2003-09-23  Download Release Notes
Python 2.3.0 2003-07-29  Download Release Notes
Python 2.2.3 2003-05-30  Download Release Notes
Python 2.2.2 2002-10-14  Download Release Notes
Python 2.2.1 2002-04-10  Download Release Notes
Python 2.1.3 2002-04-09  Download Release Notes
Python 2.2.0 2001-12-21  Download Release Notes
Python 2.0.1 2001-06-22  Download Release Notes

可以看到各个版本是交叉时间开发的,上面是根据时间线来排序的,很详细。

还有更旧的版本,根据 https://www.python.org/download/releases/

Python 1.6.1 (September 2000)
Python 1.5.2 (April 1999)
Older source releases
Python 1.5 binaries
Python 1.4 binaries
Python 1.3 binaries
Python 1.2 binaries
Python 1.1 binaries

补充知识:Debug 通常称为调试版本,它包含调试信息,并且不作任何优化,便于程序员调试程序。Release 称为发布版本,它往往是进行了各种优化,使得程序在代码大小和运行速度上都是最优的,以便用户很好地使用。

2 python2.x即将退休(retire)

根据 https://pythonclock.org/ , python 2.7版本在2020年1月1日之后就不会再被维护。

What’s all this, then?


Python 2.7 will not be maintained past 2020. Originally, there was no official date. Recently, that date has been updated to January 1, 2020. This clock has been updated accordingly. My original idea was to throw a Python 2 Celebration of Life party at PyCon 2020, to celebrate everything Python 2 did for us. That idea still stands. (If this sounds interesting to you, email [email protected]).


Python 2, thank you for your years of faithful service.


Python 3, your time is now.


How do I get started?


If the code you care about is still on Python 2, that’s totally understandable. Most of PyPI’s popular packages now work on Python 2 and 3, and more are being added every day. Additionally, a number of critical Python projects have pledged to stop supporting Python 2 soon. To ease the transition, the official porting guide has advice for running Python 2 code in Python 3.

感觉开发python的人实在太强。很酷。

而且,从这个网页的语气来看,以后python2版本肯定是被放弃维护的,建议将现在的python2 程序转换成 python 3,还给出了如何转换的指导(official porting guide):https://docs.python.org/3/howto/pyporting.html
这个网页讲了很多知识,包括python2和3的区别,这里就先不细看了。

python 2 v.s. python 3

根据 https://learntocodewith.me/programming/python/python-2-vs-python-3/ ,这篇文章写得很好,值得一看,这里介绍一下里面的主要内容:

1)Python 3 取胜:

Now, in 2018, it’s more of a no-brainer: Python 3 is the clear winner for new learners or those wanting to update tudemheir skills. Here, we’ll cover why Python 3 is better, and why companies have been moving from Python 2 to 3 en masse.

2)python2和3发展历史:

Python 2.0 was first released in 2000. Its latest version, 2.7, was released in 2010.
Python 3.0 was released in 2008. Its newest version, 3.6, was released in 2016, and version 3.7 is currently in development.
Although Python 2.7 is still widely used, Python 3 adoption is growing quickly. In 2016, 71.9% of projects used Python 2.7, but by 2017, it had fallen to 63.7%. This signals that the programming community is turning to Python 3–albeit gradually–when developing real-world applications.
Notably, on January 1, 2018, Python 2.7 will “retire” and no longer be maintained. (The clock is literally ticking!)

这个时间线很有意思。

3)python2和3的主要区别:

  1. PYTHON 2 IS LEGACY, PYTHON 3 IS THE FUTURE. 因为python2即将被放弃维护。所以python2即将成为过去式(legacy)是显而易见的。
  2. PYTHON 2 AND PYTHON 3 HAVE DIFFERENT (SOMETIMES INCOMPATIBLE) LIBRARIES
    二者支持的库不一样,而且转换起来也比较麻烦(complicated)
  3. THERE IS BETTER UNICODE SUPPORT IN PYTHON 3
    In Python 3, text strings are Unicode by default. In Python 2, strings are stored as ASCII by default–you have to add a “u” if you want to store strings as Unicode in Python 2.x.


    This is important because Unicode is more versatile than ASCII. Unicode strings can store foreign language letters, Roman letters and numerals, symbols, emojis, etc., offering you more choices.
  4. PYTHON 3 HAS IMPROVED INTEGER DIVISION:
    In Python 2, if you write a number without any digits after the decimal point, it rounds your calculation down to the nearest whole number.


    For example, if you’re trying to perform the calculation 5 divided by 2, and you type 5 / 2, the result will be 2 due to rounding. You would have to write it as 5.0 / 2.0 to get the exact answer of 2.5.


    However, in Python 3, the expression 5 / 2 will return the expected result of 2.5 without having to worry about adding those extra zeroes.


    This is one example of how Python 3 syntax can be more intuitive, making it easier for newcomers to learn Python programming.
  5. THE TWO VERSIONS HAVE DIFFERENT PRINT STATEMENT SYNTAXES
    This is only a syntactical difference–and some may consider it trivial–so it doesn’t affect the functionality of Python. That said, it is still a big and visible difference you should know about.


    Essentially, in Python 3, the print statement has been replaced with a print () function.

4)为什么公司会选择从python2转到python3

As mentioned earlier, most companies are still using Python 2 for legacy reasons, but more and more companies are using Python 3, or beginning to make the switch from 2 to 3.


So, let’s look at Instagram and Facebook–two companies that have switched from Python 2 to 3 or are in the process of doing so–and why they chose to do so.

重点介绍Instagram和Facebook。

Instagram:

Facebook:
在这里插入图片描述

5)该学python2还是3?
作者推荐的是python3

The simple answer? Learn the latest version (which at time of writing is Python 3.7.0).


Some of the changes made in Python 3 have actually made it easier for beginners to understand, so it’s the best way to learn Python for the first time.

6)什么时候会选择用python2?

Here are some situations where you might need to know Python 2:


If you want to become a DevOps engineer and work with configuration management tools like Fabric or Ansible, you might have to work with both Python 2 and 3 (because parts of these libraries don’t have full Python 3 support).
If your company has legacy code written in Python 2, you’ll need to learn to work with that.
If you have a project that depends on certain third-party software or libraries that can’t be ported to Python 3, you’ll have no choice but to use Python 2 for it.

python2和3的详细区别

这篇文章: https://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html 讲的非常详细,这里不细说了,具体可以看看这篇文章。

具体有以下几个方面:

  • The future module
  • The print function
  • Integer division
  • Unicode
  • xrange
  • The contains method for range objects in Python 3
  • Note about the speed differences in Python 2 and 3
  • Raising exceptions
  • Handling exceptions
  • The next() function and .next() method
  • For-loop variables and the global namespace leak
  • Comparing unorderable types
  • Parsing user inputs via input()
  • Returning iterable objects instead of lists
  • Banker’s Rounding

想了解更多细节,也可以参考:https://wiki.python.org/moin/Python2orPython3#What_are_the_differences.3F

为什么python3比2好?

具体参考[12] [13] [14]

实在不想写了,太多了。

具体我也没有太看明白,但是大多讨论的是unicode和print。还有的认为python 3 是一个新的开发语言。

参考文献

[1] Python2.x与3​​.x版本区别. http://www.runoob.com/python/python-2x-3x.html

[2] python3和Python2的区别(被坑太久了). https://www.cnblogs.com/hanggegege/p/5840005.html

[3] 官方推荐python3,为何还有人在用python2呢?差异总结. https://baijiahao.baidu.com/s?id=1591637336603862699&wfr=spider&for=pc

[4] Python2 和 Python3 的主要区别. https://blog.csdn.net/t46414704152abc/article/details/79089451

[5] Releases|Python. https://www.python.org/download/releases/

[6] Looking for a specific release? https://www.python.org/downloads/

[7] Python 2.7 will retire in… https://pythonclock.org/

[8] Porting Python 2 Code to Python 3. https://docs.python.org/3/howto/pyporting.html

[9] WHAT SHOULD I LEARN AS A BEGINNER: PYTHON 2 OR PYTHON 3? https://learntocodewith.me/programming/python/python-2-vs-python-3/

[10] The key differences between Python 2.7.x and Python 3.x with examples. https://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html

[11] Should I use Python 2 or Python 3 for my development activity? https://wiki.python.org/moin/Python2orPython3#What_are_the_differences.3F

[12] EIL5: Why is Python 3 better than Python 2? https://www.reddit.com/r/Python/comments/3h19c6/eil5_why_is_python_3_better_than_python_2/

[13] Should I use Python 2 or Python 3 for my development activity? https://wiki.python.org/moin/Python2orPython3

[14] Why is Python 3 (or later) better than Python 2? https://stackoverflow.com/questions/3384361/why-is-python-3-or-later-better-than-python-2

猜你喜欢

转载自blog.csdn.net/weixin_39278265/article/details/82985036