"Python tutorial" python2.x with 3. x version Difference

Source: Internet
Author: User

python2.x and 3.x version differences

The 3.0 version of Python, often referred to as Python 3000, or simply py3k. This is a large upgrade relative to earlier versions of Python.

In order not to take in too much of a burden, Python 3.0 did not consider downward compatibility when designing.

Many of the programs designed for early Python versions are not performing properly on Python 3.0.

To take care of the existing program, Python 2.6, as a transitional version, basically uses the syntax and library of Python 2.x, taking into account the migration to Python 3.0, allowing the use of some of the syntax and functions of Python 3.0.


The new Python program recommends using the Python version 3.0 syntax.


Unless the execution environment cannot install Python 3.0 or the program itself uses a third-party library that does not support Python 3.0. Currently does not support Python 3.0 third-party library has twisted, py2exe, PiL and so on.

Most third-party libraries are striving to be compatible with Python version 3.0. Even if Python 3.0 is not immediately available, it is recommended that you write a program that is compatible with Python version 3.0 and then execute it using Python 2.6, Python 2.7来.

Major changes

The changes in Python 3.0 are mainly in the following areas:

The print statement is gone and replaced by the print () function. Python 2.6 and Python 2.7 support this form of print syntax in parts. In Python 2.6 and Python 2.7, the following three forms are equivalent:

Print "Fish" print ("Fish") #注意print后面有个空格print ("Fish") #print () cannot have any other parameters

However, Python 2.6 has actually supported the new print () syntax:

From __future__ import print_functionprint ("Fish", "Panda", sep= ', ')

The new str category represents a Unicode string, which is equivalent to the Python 2.x version of the Unicode category. The bit sequence is represented by a syntax similar to B "ABC", denoted by the bytes class, which is equivalent to the str category of Python 2.x.

Now the two categories can no longer be implicitly auto-converted, so in Python 3.x "fish" +b "Panda" is an error. The correct notation is "fish" +b "Panda". Decode ("Utf-8"). Python 2.6 can automatically recognize a byte sequence as a Unicode string by:

From __future__ import unicode_literalsprint (repr ("Fish"))

The division operator "/" always returns a floating-point number within Python 3.x. In Python 2.6, it is determined whether the dividend and divisor are integers. If an integer returns an integer value, which is equal to divisible, the floating-point number returns a float value.

In order for Python 2.6 to return a floating-point value uniformly, you can:

From __future__ import Divisionprint (3/2)

The syntax for catching exceptions is changed by except Exc, Var to except Exc as Var. Use the syntax except (EXC1, Exc2) as Var to capture multiple categories of exceptions at the same time. Python 2.6 supports both of these syntaxes.

New notation for Set (set): {1,2,3,4}. Note {} still represents an empty dictionary (dict).

Dictionary derivation (Dictionary comprehensions) {expr1:expr2 for K, V in D}, this syntax is equivalent to

Result={}for K, V in D.items ():    result[expr1]=expr2return Result

Collection derivation (set comprehensions) {expr1 for x in Stuff}. This syntax is equivalent to:

result = set () for x in stuff:    result.add (EXPR1) return result

Octal number must be written 0o777, the original form 0777 can not be used, the binary must be written in 0b111. A new Bin () function is added to convert an integer into a binary string. Python 2.6 supports both of these syntaxes.

Dict.keys (), Dict.values (), Dict.items (), map (), filter (), range (), zip () no longer returns a list, but an iterator.

If there is no definite meaningful order between the two objects defined. Use <, <=, and >= compare them to throw exceptions. For example, 1 < "" Returns true in Python 2.6 and throws an exception in Python 3.0. Now the CMP (), instance.__cmp__ () function has been removed.

You can annotate the parameters of a function with the return value. This feature makes it easier for the IDE to analyze the original code in more depth. For example, add a category message to the parameter:

def sendMail (from_: str, TO:STR, TITLE:STR, BODY:STR), BOOL:    Pass

Merges an int with a long type.

Multiple modules are renamed (according to PEP8):

The old name

New name.

_winreg winreg

Configparser Configparser

Copy_reg Copyreg

Queue queue

Socketserver Socketserver

Repr Reprlib

The Stringio module is now incorporated into the new IO module. Modules such as new, MD5, gopherlib, etc. are removed. Python 2.6 has support for new IO modules.

Httplib, Basehttpserver, Cgihttpserver, Simplehttpserver, cookies, Cookielib are incorporated into the HTTP package.

The EXEC statement is canceled and only the EXEC () function is left. Python 2.6 already supports the EXEC () function.

The above is the "Python tutorial" python2.x and 3.x version of the difference between the content, more related to Topic.alibabacloud.com (www.php.cn)!

  • 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.