Home  >  Article  >  Backend Development  >  This article tells you the difference between choosing Python2 and Python3

This article tells you the difference between choosing Python2 and Python3

Tomorin
TomorinOriginal
2018-08-23 17:47:442661browse

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 6d267e5fab17ea8bc578f9e7e5e1570b python2 also has few people Use 72b62fc178de328155d2ba50cbd700b9 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn