Comparison between Python 2 and Python 3 and python 3

Source: Internet
Author: User

Comparison between Python 2 and Python 3 and python 3

I. version Comparison

The Python version is mainly divided into two categories:

Python 2.7.3 is the most widely used Python 2.7.3.

Python 3. x, known as Python3: is the latest version, such as Python 3.1. In the long run, it is also a future trend.

[Differences between Python2 and Python3]

1. From Python2 to Python3, many basic function interfaces have changed. Even some libraries or functions have been removed and renamed.

Python2 and Python3 have changed interfaces in many basic and most commonly used functions. The most typical function is to count the most commonly used print functions.

2. Third-party database support, which is currently the best supported by Python2, is not supported by Python3

One of the reasons why Python is powerful is that there are many third-party libraries and powerful functions.

Currently, many third-party Python libraries only support Python 2.

Or, even if Python 3 is provided, it is not necessarily mature.

Ii. Encoding comparison

In Python, there are only two types of characters, whether Python2 or Python3:

  1. General Unicode characters;
  2. (After unicode is encoded) a character of a certain encoding type, such as UTF-8, GBK and so on.

Character Type in Python2:

  • Str: encoded byte sequence
  • Unicode: text character before Encoding

Character Type in Python3:

  • Str: encoded unicode text character
  • Bytes: the byte sequence before Encoding

We can think of a string in two states: Text state and byte (Binary) state. The two character types in Python2 and Python3 correspond to the Two States respectively, and then perform codec conversion between them.Encoding refers to converting a string into a bytecode that involves the internal representation of a string. decoding refers to converting a bytecode into a string and displaying bits as characters.

In Python2, Both str and unicode have the encode and decode methods. HoweverWe do not recommend using encode for str and decode for unicode.This is a design defect in Python2. Python3 is optimized. str only has one encode method to convert the string into a bytecode, and bytes only has one decode method to convert the bytecode into a text string.

Both str and unicode of Python2 are subclasses of basestring, so they can be directly spliced. In Python3, bytes and str are two independent types, which cannot be spliced.

In Python2, the common character enclosed by quotation marks is str. The encoding type of the string corresponds to the encoding type stored in your Python file, in the most common Windows platform, GBK is used by default. In Python3, the string enclosed by single quotes or double quotes is a Unicode str.

There are some prerequisites for str encoding:

  1. The Python file starts to declare the corresponding encoding.
  2. The Python file itself is indeed saved using this encoding.
  3. The two encoding types should be the same (for example, both UTF-8 or GBK)

In this way, the Python parser can correctly parse the text into the corresponding encoded str.

In general, the character encoding problem in Python3 has been greatly optimized, and it is no longer as troublesome as Python2. In Python3, the text is always Unicode, represented by the str type. binary data is represented by bytes, which does not secretly mix str and bytes, making the difference between the two more obvious.

Summary

The above is all about this article. I hope this article will help you learn or use python. If you have any questions, please leave a message, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.