For beginners, they only know that python2 and Python3 are two generations of versions, but they don’t know the differences between the two generations. This article will introduce the difference between Python2 and Python3 .
print is no longer a statement, but a function. For example, it used to be print 'abc' but now it is print('abc')
But python2.6 can use from __future__ import print_function to achieve the same function:
#py2 print("hello")#等价print(“hello”) #py3 print("hello")
In Python 3, there are no old-style classes, only new-style classes, which means there is no need to use class Foobar(object) like this: pass explicitly subclasses object
but it is better to add it. The main difference is that old-style is a classtype type and new-style is a type type
Original 1/2 (two Integer division) the result is 0, now it is 0.5
python 2.2 The above can be used from __future__ import division to implement the modified features, also note that // replaces the previous / operation
New The string formatting method format replaces the % error. This method has been available in str and unicode since python2.6. At the same time, python3 still supports the % operator
xrange is renamed to range
and the changes are also There are a series of built-in functions and methods that all return iterator objects instead of lists or tuples, such as filter, map, dict.items, etc.
!= replaces < > python2 also has few people Use <
except Exception, e becomes except (Exception) as e
exec becomes a function
* Mainly due to changes in the class library, the organizational structure has changed a bit. But the function has not changed. urlparse - > Such changes in urllib.parse
* The most core change is not mentioned, support for bytes and native UNICODE strings, the unicode object is deleted, str is a native unicode string, and bytes replaces the previous str. This is The core.
The above is the detailed content of This article tells you the difference between choosing Python2 and Python3. For more information, please follow other related articles on the PHP Chinese website!