The difference between Python 2 and python 3

Time:2021-7-19

Statement: the following is my personal understanding. If you find any mistakes or questions, you can contact me for discussion

brief introduction

At present, there are two Python versions of projects developed with Python, namely, python 2. X and python 3. X. because Python 3 has a large upgrade compared with Python 2, python 3 will be the mainstream version now and in the future.

Python 3 timeline

  • Python 3.0 was released on December 3, 2008
  • Python 3.1 was released on June 27, 2009
  • Python 3.2 was released on February 20, 2011
  • Python 3.3 was released on September 29, 2012
  • Python 3.4 was released on March 16, 2014
  • Python 3.5 was released on September 13, 2015
  • Python 3.6 was released on December 23, 2016
  • Python 3.7 released on June 27, 2018
  • Python 3.8 released on October 14, 2019
  • Python 3.9 released on October 5, 2020

In order not to bring in too much burden, downward compatibility was not considered in the design of Python 3.0, so the early projects could not run normally on Python 3.0. In order to gradually migrate the early projects to Python 3.0, python officially provides an over version of Python 2.6 (based on the syntax and Library of Python 2, allowing the use of partial syntax and functions of Python 3). In mid-2010, python 2.7 will be released as the last version of Python 2. X (the official maintenance of Python 2.7 will be stopped after January 1, 2020). It’s very important for us to understand the changes of python3 version for our daily development and project maintenance

Changes after Python 3.0

Interpreter name

The interpreter of Python 2. X is python, and the interpreter of Python 3. X is Python 3

Interpreters implemented in other languages:

PyPy Python interpreter implemented in Python, supports JIT immediate compilation
cpython Python interpreter based on official version of C language
Jython Python interpreter running on Java
IronPython Python interpreter running on. Net and mono platform

character string

The string in Python 2 is a str string based on ASCII encoding. When the string variable is called, its bytes value (string memory address in hexadecimal) will be printed. Unicode is a separate type.

In Python 3, strings support Unicode natively, and byte has become a separate byte class, and python 3 source files use UTF-8 encoding by default, so Chinese can also be used as variable names.

import

In Python 2, the import package adopts relative path, which makes it difficult to import standard library.

In python3, absolute path is adopted for import package

print

Print in Python 2 is a special statement. In Python 3, print becomes a print function

Range function and xrange function

Xrange function is abandoned in Python 3, and the mechanism of range function is modified to achieve the effect of returning iteratable objects

Old style and new style

There are old-fashioned classes and new-fashioned classes in python2. In python3, new-fashioned classes are adopted uniformly (new-fashioned class declaration requires inheriting object), and new-fashioned classes must be used to apply multiple inheritance

Division and remainder

  • In Python 2, if two integers are operated, the result is integer, but if one of them has a floating-point type, the result is floating-point. Python 3 is true division and the result is float.

  • In Python 2, the remainder returns the largest integer less than the result of division operation, which is consistent with the return type logic of the ‘/’ operator in terms of type. The action of python3 is the same as that of python2.

raw_ Input function and input function

raw_ Input function is abandoned in Python 3, and input function is used uniformly

indent

Python 3 adopts a more strict indenting method, and tab and space cannot coexist

long

Long integer is abandoned by Python 3, and int is used uniformly. Int has the characteristics of long

file

The file function is abandoned and the open function is used

not equal operator

Python 3 abolished < >, only=

Method return value

In Python 3, these methods no longer return list objects: keys (), values (), items (), zip (), map (), filter (), which are associated with dictionary, but they can be forcibly converted through list

Next() function

The next () function of iterator iterator is discarded by Python 3, and next (iterator) is used uniformly

exec

The exec statement is abandoned by Python 3, and the exec function is used uniformly

has_ Key function

has_ The key function is abandoned by Python and uses the in keyword

Round function

In Python 2, the round function returns a value of type float. In Python 3, the round function returns an int value.

Comparison operator

Any two objects in Python 2 can be compared. In Python 3, only objects with the same type of data can be compared.

This work adoptsCC agreementReprint must indicate the author and the link of this article

Recommended Today

ES6 Usage Summary

Destructuring assignment Realize variable exchange let a =1; let b=2 [a,b]=[b,a] console.log(a,b)//2,1 Default value assignment [a,b,c=3]=[1,2] console.log(a,b,c)//1,2,3 Get function return value directly function fc(){ return [1,2] } let a,b; [a,b]=fc() console.log(a,b)//1,2 Nested deconstruction assignment of objects let metadata={ title:’currentTitle’, test:[{ title:”deepTitle”, desc:’description’ }] } let {title:titleone,test:[{title:titlescond}]}=metadata; console.log(titleone,titlescond)//currentTitle,deepTitle String extension If the Unicode code is greater […]