python2.x and 3??. X version Difference

Source: Internet
Author: User

Chinese Original: http://www.runoob.com/python/python-2x-3x.html

Unicode

Python 2 has an ASCII str () type, Unicode () is separate, not a byte type.

Now, in Python 3, we end up with a Unicode (Utf-8) string, and a byte class: Byte and Bytearrays.

Since the python3.x source file uses Utf-8 encoding by default, this makes the following code legal:

Division operation

The division in Python is more sophisticated than other languages and has a very complex set of rules. The division in Python has two operators,/and//

First/Division:

In Python 2.x/division is similar to most of the languages we are familiar with, such as Java AH C Ah, the result of dividing an integer is an integer, the fractional part is completely ignored, and the floating-point division retains the part of the decimal point to get the result of a floating-point number.

In Python 3.x/division no longer does this, and the result is a floating-point number for dividing between integers.

Abnormal

Handling Exceptions in Python 3 has also changed slightly, and in Python 3 we now use as as keywords.

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 already supports both of these syntaxes.

    • 1. In the 2.x era, all types of objects can be thrown directly, in the 3.x era, only objects that inherit from Baseexception can be thrown.
    • 2.2.x raise statement using commas to throw object types and parameters separated, 3.x canceled the work of this kind of flower, directly call the constructor to throw objects.

In the 2.x era, exception in the code in addition to the program error, but also often do something common control structure should do, in 3.x can be seen, the designer let the exception becomes more exclusive, only in the case of errors can be used to deal with the exception capture statement.

Xrange

The use of xrange () to create iterative objects in Python 2 is very popular. For example, a for loop or a list/set/dictionary derivation.

This behaves much like a generator (e.g.. "Lazy evaluation"). But this xrange-iterable is infinite, meaning you can traverse infinitely.

Because of its lazy evaluation, if you must not simply not traverse it once, the xrange () function is faster than range (for example for loops). However, it is not recommended that you repeat iterations more than once, because the generator starts from scratch each time.

In Python 3, range () is implemented like xrange () so that a specialized xrange () function no longer exists (xrange () throws a named exception in Python 3).

Inequality operator

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

Python 3.x removed the <> only! = a kind of notation, fortunately, I never used <> habits

Removed the repr expression '

The inverse quotation mark in Python 2.x is equivalent to the effect of the REPR function

Python 3.x has removed the notation, allowing only the REPR function to be used to make the code look clearer? However, I feel that the opportunity to use repr is very rare, usually only used when debugging, most of the time, or using the STR function to describe the object with a string

Multiple modules are renamed (according to PEP8)

Print function

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,

octal literal representation

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 already supports both of these syntaxes.

In Python 3.x, there is only one way to represent octal literals, which is 0o1000.

Python 2.x

Data type

1) py3.x removed the long type and now has only one integer--int, but it behaves like 2. X version of Long

2) added the bytes type, corresponding to 2. X version of the eight-bit string, define a bytes literal method as follows

python2.x and 3??. X version Difference

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.