Home  >  Article  >  Backend Development  >  What version of python is there?

What version of python is there?

silencement
silencementOriginal
2019-05-21 17:51:1720145browse

What version of python is there?

The current mainstream versions of python are python2 and python3. Python2 is expected to stop maintenance in 2020. Beginners are recommended to learn python3 directly.

Next let’s talk about the differences between Python2 and Python3.

Basic syntax differences

##Core class differences

1. Python3’s native handling of Unicode characters Support

The use of ASCII code as the default encoding method in Python2 results in two types of strings, str and unicode. Python3 only supports unicode strings. The corresponding relationship between bytes and characters in python2 and python3 is:

2. Python3 uses absolute paths for import.

The relative path import in Python2 will make it difficult to import the standard library (imagine that there is file.py in the same directory, how to import this file and the standard library file at the same time). This point will be modified in Python3. If you 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 Python 2. Python 3 adopts new-style classes uniformly. New-style class declarations require inheritance from object, and multiple inheritance must be applied using new-style classes.

4. Python3 uses stricter indentation. In the indentation mechanism of Python 2, 1 tab and 8 spaces are equivalent, so tabs and spaces can be allowed to coexist in the code at the same time. This equivalence mechanism can cause problems in the use of some IDEs. In Python3, a tab can only be replaced by another tab, so the coexistence of tabs and spaces will result in an error: TabError: inconsistent use of tabs and spaces in indentation.

obsolete class difference

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

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

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

##4. The inequality operator "<>" is abandoned by Python3, and the unified use is "!="

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

is used uniformly.

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

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

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 uniformly used.

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

11. The file function is abandoned by Python3. Open is used to process files. You can check the file type through io.IOBase.

12. The apply function is abandoned by Python3.

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

Related learning recommendations: python tutorial

The above is the detailed content of What version of python is there?. 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