Differences between Python2 and Python3

1. Differences in basic grammar

Core class differences

1. Python3's native support for Unicode characters

The use of ASCII code as the default encoding in Python2 leads to two types of string, str and unicode, and Python3 only supports unicode strings. The correspondence between python2 and python3 bytes and characters is:

img

2. Python3 uses an absolute path for import.

The relative path import in Python2 will make it difficult to import the standard library. This point will be modified in Python3. If you still need to import files in the same directory, you must use absolute paths, otherwise you can only use related import methods to import.

3. There is a difference between old-style classes and new-style classes in Python2, and Python3 adopts new-style classes uniformly. The new-style class declaration requires object inheritance, and multiple inheritance must be applied with the new-style class.

4. Python3 uses stricter indentation.
In the indentation mechanism of Python2, 1 tab and 8 spaces are equivalent, so tab and space can be allowed to coexist in the code at the same time during indentation. This equivalence mechanism can cause problems in the use of some IDEs. Python3 tab in a tab can find another alternative, tab, and thus cause an error space coexist:
TabError: inconsistent use of tabs and spaces in indentation.

Obsolete differences

1. The print statement was abandoned by python3, and the print function was used uniformly

2. The exec statement is abandoned by python3, and the exec function is used uniformly

3. The execfile statement is abandoned by Python3. It is recommended to use exec(open("./filename").read())

4. The inequality operator "<>" was abandoned by Python3 and used uniformly"!="

5. The long integer type is abandoned by Python3, and int is used uniformly

6. The xrange function is abandoned by Python3, and range is used uniformly. The range mechanism in Python3 has also been modified and the efficiency of generating large data sets has been improved.

7. In Python3, these methods no longer return list objects: the keys(), values(), items(), zip(), map(), filter() associated with the dictionary, but they can be forcibly converted through the list:

mydict={"a":1,"b":2,"c":3}
mydict.keys()  #<built-in method keys of dict object at 0x000000000040B4C8>
list(mydict.keys()) #['a', 'c', 'b']

8. The next() function of iterator is abandoned by Python3, and next(iterator) is used uniformly

9. The raw_input function is abandoned by Python3, and the input function is used uniformly

10. The has_key function of dictionary variables is abandoned by Python, and the in keyword is used uniformly

11. The file function is abandoned by Python3, and open is used to process files uniformly, and the file type can be checked through io.IOBase

12. The apply function is abandoned by Python3

13. The exception StandardError is abandoned by Python3, and Exception is used uniformly

Modify the class difference

1. The difference between the floating-point number division operator / and //

  • Python2: / is integer division, // is decimal division
  • Python3: / is decimal division, // is integer division.

2. The difference between exception throwing and catching mechanism

  • Python2
raise IOError, "file error" #抛出异常
except NameError, err:  #捕捉异常
  • Python3
raise IOError("file error") #抛出异常
except NameError as err: #捕捉异常

3. The difference between variable values ​​in the for loop

  • Python2, the for loop will modify the value of the external variable with the same name
i = 1
print ('comprehension: ', [i for i in range(5)])
print ('after: i =', i  ) #i=4
  • Python3, the for loop will not modify the value of the external variable with the same name
i = 1
print ('comprehension: ', [i for i in range(5)])
print ('after: i =', i  ) #i=1

4. The difference between the return value of the round function

  • Python2, round function returns float type value
isinstance(round(15.5),int) #True
  • Python3, round function returns int type value
isinstance(round(15.5),float) #True

5. The difference between comparison operators

  • Any two objects can be compared in Python2
11 < 'test' #True
  • Only objects of the same data type can be compared in Python3
11 < 'test' # TypeError: unorderable types: int() < str()

Second, the third-party toolkit

We searched the number of third-party toolkits for Python2.7 and Python3.5 in the official pip download source pypi, and found that the number of third-party tool categories corresponding to the Python2.7 version is 28,523, and the number of Python3.5 versions is 12457. There is a considerable gap in the number of versions supported by third-party toolkits.

We have listed common and useful third-party toolkits from the perspective of data analysis applications (the following table), and analyzed the support of these toolkits in Python2.7 and Python3.5:

classificationTool nameuse
data collectionscrapyWeb page collection, crawler
data collectionscrapy-redisDistributed crawler
data collectionseleniumweb test, simulated browser
data processingbeautifulsoupWeb page interpretation library, providing lxml support
data processinglxmlxml interpretation library
data processingxlrdexcel file reading
data processingxlwtexcel file writing
data processingxlutilsSimple format modification of excel file
data processingpywin32Reading and writing of excel files and customization of complex formats
data processingPython-docxReading and writing of Word files
data analysisnumpyMatrix-based mathematical calculation library
data analysispandasTable-based statistical analysis library
data analysisscipyScientific computing library, supporting high-level abstract and complex models
data analysisstatsmodelsStatistical Modeling and Econometrics Toolkit
data analysisscikit-learnMachine learning tool library
data analysisgensimNatural language processing tool library
data analysisjiebaChinese word segmentation tool library
data storageMySQL-pythonmysql read and write interface library
data storagemysqlclientmysql read and write interface library
data storageSQLAlchemyORM encapsulation of the database
data storagepymssqlsql server read and write interface library
data storageredisRedis read and write interface
data storagePyMongoMongodb read and write interface
Data presentationmatplotlibPopular data visualization library
Data presentationseabornBeautiful data is the lake library, based on matplotlib
Tool assistancejupyterWeb-based python IDE, often used for data analysis
Tool assistancechardetCharacter check tool
Tool assistanceConfigParserConfiguration file read and write support
Tool assistancerequestsHTTP library for network access