The difference between Python2.x and 3.x versions


The 3.0 version of Python is often called Python 3000, or Py3k for short. This is a major upgrade compared to earlier versions of Python.

In order not to bring too much burden, Python 3.0 was not designed with downward compatibility in mind.

Many programs designed for earlier Python versions cannot run properly on Python 3.0.

In order to take care of existing programs, Python 2.6 is a transitional version that basically uses the syntax and libraries of Python 2.x. It also considers the migration to Python 3.0 and allows the use of some Python 3.0 syntax and functions.


It is recommended that new Python programs use the syntax of Python 3.0 version.


Unless the execution environment cannot install Python 3.0 or the program itself uses a third-party library that does not support Python 3.0. Third-party libraries that currently do not support Python 3.0 include Twisted, py2exe, PIL, etc.

Most third-party libraries are working hard to be compatible with Python 3.0 version. Even if Python 3.0 cannot be used immediately, it is recommended to write a program that is compatible with Python 3.0 and then execute it using Python 2.6 or Python 2.7.

The changes in Python 3.0 are mainly in the following aspects:


print function

The print statement is gone and replaced by the print() function. Python 2.6 and Python 2.7 partially support this form of print syntax. In Python 2.6 and Python 2.7, the following three forms are equivalent:

print "fish" print ("fish") #注意print后面有个空格 print("fish") #print()不能带有任何其它参数

However, Python 2.6 actually supports the new print() syntax:

from __future__ import print_function print("fish", "panda", sep=', ')

Unicode

Python 2 has ASCII str() type, unicode() is separate, not byte type.

Now, in Python 3, we finally have Unicode (utf-8) strings, and a byte class: byte and bytearrays.


Since Python3.X source code files use utf-8 encoding by default, this makes the following code legal:

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

Python 2.x

>>> str = "我爱北京天安门" >>> str '\xe6\x88\x91\xe7\x88\xb1\xe5\x8c\x97\xe4\xba\xac\xe5\xa4\xa9\xe5\xae\x89\xe9\x97\xa8' >>> str = u"我爱北京天安门" >>> str u'\u6211\u7231\u5317\u4eac\u5929\u5b89\u95e8'

Python 3.x

>>> str = "我爱北京天安门" >>> str '我爱北京天安门'

Division operation

Division in Python is very high-end compared to other languages, and has a very complicated set of rules. Division in Python has two operators, / and //

First of all, /division:

In python 2.x, /division is similar to most languages we are familiar with, such as Java and C are similar. The result of integer division is an integer. The decimal part is completely ignored. Floating-point division will retain the decimal part and obtain a floating-point result.

In python 3.x/division no longer does this. For division between integers, the result will also be a floating point number.

Python 2.x:

>>> 1 / 2 0 >>> 1.0 / 2.0 0.5

Python 3.x:

>>> 1/2 0.5

As for //division, this division is called floor division, and the result of the division will be automatically performed A floor operation that is consistent in Python 2.x and Python 3.x.

python 2.x:

>>> -1 // 2 -1

python 3.x:

>>> -1 // 2 -1

Note that the decimal part is not discarded, but the floor operation is performed. If you want to intercept the decimal part , then you need to use the trunc function of the math module

python 3.x:

>>> import math >>> math.trunc(1 / 2) 0 >>> math.trunc(-1 / 2) 0

Exception

Exception handling has also changed slightly in Python 3. In Python 3 we now use as as keyword.

The syntax for catching exceptions has been changed fromexcept exc, vartoexcept exc as var.

Use the syntax except (exc1, exc2) as var to capture multiple categories of exceptions at the same time. Python 2.6 already supports both syntaxes.

  • 1. In the 2.x era, all types of objects can be thrown directly. In the 3.x era, only objects that inherit from BaseException can be thrown. .

  • 2. 2.x The raise statement uses commas to separate the thrown object type and parameters. 3.x cancels this weird writing method and directly calls the constructor to throw the object. .


In the 2.x era, exceptions in the code not only represent program errors, but also often do things that ordinary control structures should do. In 3 It can be seen from .x that the designer has made exceptions more specific. Only when errors occur can they be handled with exception catching statements.


xrange

In Python 2, the use of xrange() to create iterable objects is very popular. For example: for loop or list/set/dictionary comprehension.

This behaves very much like a generator (i.e. "lazy evaluation"). But this xrange-iterable is infinite, meaning you can traverse infinitely.

Due to its lazy evaluation, the xrange() function is faster than range() if you have to iterate over it just once (such as a for loop). However, rather than iterating once, it is not recommended that you iterate multiple times because the generator starts from scratch each time.

In Python 3, range() is implemented like xrange() so that a dedicated xrange() function no longer exists (in Python 3 xrange() throws a named exception).

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)  in () ----> 1 print(xrange(10)) NameError: name 'xrange' is not defined

Octal literal representation

The octal number must be written as 0o777, the original form 0777 cannot be used Yes; the binary must be written as 0b111.

A new bin() function is added to convert an integer into a binary string. Python 2.6 already supports both syntaxes.

In Python 3.x, there is only one way to represent an octal literal, which is 0o1000.

python 2.x

>>> 0o1000 512 >>> 01000 512

python 3.x

>>> 01000 File "", line 1 01000 ^ SyntaxError: invalid token >>> 0o1000 512

Inequality operator

There are two ways to write inequality in Python 2.x != and <>

<> was removed in Python 3.x, and there is only one way of writing !=. Fortunately, I have never been in the habit of using <>


The repr expression `` has been removed

The backtick `` in Python 2.x is equivalent to the role of the repr function

This writing method has been removed in Python 3.x , only the repr function is allowed to be used. Is this done to make the code look clearer? However, I feel that there are very few opportunities to use repr. It is usually only used during debugging. Most of the time, the str function is still used to describe objects with strings.

def sendMail(from_: str, to: str, title: str, body: str) -> bool: pass

Multiple modules were renamed (according to PEP8)

StringIO module is now merged into the new io module. New, md5, gopherlib and other modules have been deleted. Python 2.6 already supports the new io module.

httplib, BaseHTTPServer, CGIHTTPServer, SimpleHTTPServer, Cookie, cookielib are merged into the http package.

The exec statement is canceled, leaving only the exec() function. Python 2.6 already supports the exec() function.


5. Data type

1) Py3.X has removed the long type, and now there is only one integer type - int, but it behaves like the 2.X version long

2) A new bytes type is added, corresponding to the octet string of version 2.X. The method of defining a bytes literal is as follows:

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

str object and bytes object can be used. The encode() (str -> bytes) or .decode() (bytes -> str) methods convert each other.

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

3) The .keys(), .items and .values() methods of dict return iterators, while the previous iterkeys() and other functions have been abandoned. Also removed are the dict.has_key(), replace it with in.

##Old name New name _winreg winreg ConfigParser configparser copy_reg copyreg Queue queue SocketServer socketserver repr reprlib