(personal record) Python2 and Python3 version differences

Source: Internet
Author: User

There are still some open source modules that have not been updated to Python3, and do not understand the version differences and cannot make changes to inappropriate places.

The special usage of Python2 is not explored because of the pursuit of Python3 approach.

This article does not complement all versions of the differences, only for archival records I encountered version differences.

Print function:

In Python 2, print is treated as a statement and not as a function

Change:

Print statement in Python 2 ==>python the print () function in 3

Input ():

Python2 's Raw_input () updated to input () in Python 3

Integer Division:

Update:

Divide by py2 Decimal Division = = "Py3 in the calculation of the more accurate division

If you want to keep decimals in py3, use the//operator

But pay attention to the use of the decimal//operator,//is to return a smaller number than the actual quotient.

Unicode encoding:
    • The string in 1.Python 2 is based on ASCII
    • 2.Python 3 By default, the string type in Unicode,python3 is of the Unicode type, and two additional byte classes are added: bytes and bytearrays
    • Use of Byte:
      • The Str object can be converted to a bytes object using encode ()
      • Bytes object converted to str object using decode ()
      • The b single or double quotation marks in Python that prefix the data of the bytes type are:

    • Because the coding change also causes the network programming function in Python to use the change, the network transmission is the byte stream, the Python2 network transmission default is the bytes type, but the Python3 string is Unicode, therefore Python3 wants to encode the data to transmit.
Inequality operator

Python 2.x does not equal two ways!! = and <>

Python 3.x is stripped of <> only! = a notation

There are still some tutorials to keep <> usage!

Handling Exceptions:

The syntax for catching exceptions is changed from except exception, variable to except exception as variable

Python3 some of the exception refinement, a lot of new exceptions

Throw exception:

Update:

Py3 using constructors to throw exceptions

Raise exception name, "Custom exception information"
Raise exception Name ("Custom exception Information")

Iteration of the generator:

The generator is supported in Python2. Next ()

Updated to

Next (generator) in Python3, Generator. __next__ ()

(personal record) Python2 and Python3 version differences

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.