What's the difference between Python 2 and Python 3?

Source: Internet
Author: User
Tags integer integer division

To this topic you may have guessed what I will say next, hehe, right, that is to list these two different versions of the But not!

You'll find that Python has two major versions, Python2 and Python3, but Python is different from other languages, backward-compatible, and Python3 is backward-compatible, but most of the components and extensions are based on Python2, Here's a summary of the difference between Python2 and Python3.

1. Performance

The Py3.0 runs Pystone benchmark 30% slower than Py2.5. Guido that Py3.0 has a great space for optimization, in string and plastic operations can be

To achieve good optimization results.

Py3.1 performance is 15% slower than Py2.5, and there is a lot of room for improvement.

2. Code

The py3.x source file uses Utf-8 encoding by default, which makes the following code legal:

>>> China = ' Chinese '

>>>print (China)

The

3. Grammar

1) Remove the <>, and switch to!=.

2) Remove ", all switch to REPR ()

3 keywords add as and with, and True,false,none

4) Integer division returns the floating-point number, to get the integer result, use//

5) Add the nonlocal statement. External (Non-global) variables can be assigned directly using noclocal x

6 Remove the Print statement and add the print () function to achieve the same function. There is also the EXEC statement, which has been changed to exec () function

For example:

2.x:print "The answer is", 2*2

3.x:print ("The answer is", 2*2)

2.x:print X, # Stop wrapping with comma end

3.x:print (X, end= "") # Use a space instead of a newline

2.x:print # Output New Line

3.x:print () # Output New Line

2.x:print >>sys.stderr, "Fatal error"

3.x:print ("Fatal error", File=sys.stderr)

2.x:print (x, y) # output repr ((x, y))

3.x:print ((x, y)) # is different from print (x, y)!

7 Change the behavior of the order operator, such as X

8 input function changed, delete raw_input, replace with input:

2.x:guess = Int (raw_input (' Enter an Integer: ') # Read keyboard Input Method

3.x:guess = Int (input (' Enter an Integer: '))

9) to remove the tuple parameter solution package. Cannot def (A, (b, c)):p This defines a function

10 The new 8-character variable, the OCT () function is modified accordingly.

The 2.X approach is as follows:

>>> 0666

438

>>> Oct (438)

' 0666 '

3.X this way:

>>> 0666

Syntaxerror:invalid Token (

>>> 0o666

438

>>> Oct (438)

' 0o666 '

11 added 2 literal and bin () functions

>>> Bin (438)

' 0b110110110 '

>>> _438 = ' 0b110110110 '

>>> _438

' 0b110110110 '

12) Scalable iterative solution. In py3.x, A, b, *rest = seq and *rest, a = seq are all legitimate, requiring only two points: rest is the list

Objects and SEQ can be iterated.

13 The new super (), can no longer give super () pass parameters,

>>> class C (object):

def __init__ (Self, a):

Print (' C ', a)

>>> class D (C):

def __init (Self, a):

Super (). __init__ (a) # call super () without parameters

>>> D (8)

C 8

<__main__. D Object at 0x00d7ed90>

Related Article

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.