Differences between Python 2.x and 3.x
Version 3.0 of Python, often referred to as Python 3000, or Py3k for short. This is a big upgrade from earlier versions of Python.
In order not to bring too much burden, Python 3.0 was not designed with backward compatibility in mind.
Many programs designed for earlier Python versions will not run properly on Python 3.0.
In order to take care of existing programs, Python 2.6, as a transitional version, basically uses the syntax and libraries of Python 2.x, while considering the migration to Python 3.0, allowing the use of some Python 3.0 syntax and functions.
New Python programs are recommended to use the Python 3.0 syntax.
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 do not currently support Python 3.0 include Twisted, py2exe, PIL, etc.
Most third-party libraries are working hard to be compatible with Python 3.0. Even if Python 3.0 cannot be used immediately, it is recommended to write programs that are compatible with Python 3.0, and then use Python 2.6, Python 2.7 to execute.
The changes in Python 3.0 are mainly in the following areas:
print function
The print statement is gone, replaced by the print() function. This form of print syntax is partially supported in Python 2.6 and Python 2.7. In Python 2.6 and Python 2.7, the following three forms are equivalent:
print "fish" print ("fish") #Note that there is a space after print print("fish") #print() cannot take any other arguments
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 class of bytes: byte and bytearrays.
Since Python3.X source files use utf-8 encoding by default, this makes the following code legal:
>>> China = 'china' >>>print(China) china
Python 2.x
>>> str = "I love Beijing Tiananmen" >>> str '\xe6\x88\x91\xe7\x88\xb1\xe5\x8c\x97\xe4\xba\xac\xe5\xa4\xa9\xe5\xae\x89\xe9\x97\xa8' >>> str = u"I love Beijing Tiananmen" >>> str u'\u6211\u7231\u5317\u4eac\u5929\u5b89\u95e8'
Python 3.x
>>> str = "I love Beijing Tiananmen" >>> str 'I love Beijing Tiananmen'
division operation
The division in Python is very high-end compared to other languages, and has a very complex set of rules. Division in Python has two operators, / and //
First talk/division:
In python 2.x / division is similar to most languages we are familiar with, such as Java and C. The result of integer division is an integer, and the decimal part is completely ignored. Floating-point division will retain the decimal point and get an integer. The result of a floating point number.
In python 3.x / division doesn't do that anymore, for division between integers, the result will also be a float.
Python 2.x:
>>> 1 / 2 0 >>> 1.0 / 2.0 0.5
Python 3.x:
>>> 1/2 0.5
For // division, this division is called floor division, which automatically performs a floor operation on the result of the division, which is consistent in python 2.x and python 3.x.
python 2.x:
>>> -1 // 2 -1
python 3.x:
>>> -1 // 2 -1
Note that it is not to discard the decimal part, but to perform the floor operation. If you want to intercept the decimal part, 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
abnormal
Handling exceptions has also changed slightly in Python 3, where we now use as as the keyword.
The syntax for catching exceptions is changed from except exc, var to except exc as var .
Multiple classes of exceptions can be caught simultaneously using the syntax except (exc1, exc2) as var. Both syntaxes are already supported in Python 2.6.
- 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. The 2.x raise statement uses a comma to separate the type of the thrown object from the parameters. 3.x cancels this strange writing method and directly calls the constructor to throw the object.
In the 2.x era, in addition to indicating program errors, exceptions in the code often do things that ordinary control structures should do. In 3.x, it can be seen that the designer makes exceptions more specific, and only when errors occur The situation can only be handled with an exception catch statement.
xrange
The use of xrange() to create iterable objects is very popular in Python 2. For example: for loops or list/set/dictionary comprehensions.
This behaves very much like a generator (eg. "lazy evaluation"). But this xrange-iterable is infinite, meaning you can traverse infinitely.
Because of its lazy evaluation, the xrange() function is faster than range() if you have to iterate over it just once (like a for loop). Still, instead of 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 (xrange() throws named exceptions in Python 3).
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
octal literal representation
Octal numbers must be written as 0o777, the original form 0777 cannot be used; binary numbers must be written as 0b111.
A new bin() function has been added to convert an integer to a binary string. Both syntaxes are already supported in Python 2.6.
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 "<stdin>", line 1 01000 ^ SyntaxError: invalid token >>> 0o1000 512
inequality operator
In Python 2.x, there are two ways of writing != and <>
In Python 3.x, the <> is removed, and there is only one way of writing !=. Fortunately, I have never used the habit of <>
Removed repr expression ``
Backticks `` in Python 2.x are equivalent to the role of the repr function
In Python 3.x, the `` is removed, and only the repr function is allowed. Is the purpose of this to make the code look clearer? However, I feel that there are very few opportunities to use repr. Generally, it is only used when debugging. Most of the time, the str function is 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)
old name | new name |
---|---|
_winreg | winreg |
ConfigParser | configparser |
copy_reg | copyreg |
Queue | queue |
SocketServer | socketserver |
repr | reprlib |
The StringIO module is now merged into the new io module. Modules like new, md5, gopherlib etc. are removed. Python 2.6 already supports the new io module.
httplib, BaseHTTPServer, CGIHTTPServer, SimpleHTTPServer, Cookie, cookielib are merged into http package.
The exec statement is cancelled, leaving only the exec() function. Python 2.6 already supports the exec() function.
5. Data Types
1) Py3.X removed the long type, and now there is only one integer type - int, but it behaves like the 2.X version of long
2) The bytes type has been added, which corresponds to the 2.X version of the octet string. The method to define a bytes literal is as follows:
>>> b = b'china' >>> type(b) <type 'bytes'>
str objects and bytes objects can be converted to each other using the .encode() (str -> bytes) or .decode() (bytes -> str) methods.
>>> s = b.decode() >>> s 'china' >>> b1 = s.encode() >>> b1 b'china'
3) The .keys(), .items and .values() methods of dict return iterators, and the previous functions such as iterkeys() are discarded. Also removed is dict.has_key(), replace it with in.