What is the difference between python 2 and python 3?

Source: Internet
Author: User

What is the difference between python 2 and python 3?

You may have guessed what I want to talk about next in this topic. Haha, right, that is, to list the two different versions, but not to mention them!

You will find that python has two major versions: python2 and python3, but python is backward compatible with other languages. python3 is not backward compatible, however, most components and extensions are based on python2. The differences between python2 and python3 are summarized below.

1. Performance

Py3.0 runs pystone benchmark 30% slower than Py2.5. Guido believes that Py3.0 has a huge space for optimization, which can be used for string and integer operations.

To achieve good optimization results.

Py3.1 performance is 15% slower than Py2.5, and there is still much room for improvement.

2. Encoding

The Py3.X source code file uses UTF-8 encoding by default, which makes the following code Legal:

>>> China = 'China'

>>> Print (China)

China

3. Syntax

1) Remove <>, switch to all! =

2) Remove ''and use repr () instead ()

3) Add the as and with keywords, and add True, False, None

4) The integer division function returns a floating point number. To obtain the integer result, use //

5) Add the nonlocal statement. You can use noclocal x to directly assign peripheral (non-Global) variables.

6) Remove the print statement and add the print () function to implement the same function. The exec statement has been changed to the exec () function.

For example:

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

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

2. X: print x, # use a comma to end the line break.

3. X: print (x, end = "") # Use spaces to replace line breaks

2. X: print # output New Line

3. X: print () # output a 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) # unlike print (x, y )!

7) changes the behavior of sequence operators, such as x

8) the input function is changed. raw_input is deleted and replaced by input:

2. X: guess = int (raw_input ('enter an integer: ') # How to read keyboard input

3. X: guess = int (input ('enter an integer :'))

9) Remove the productkey parameter from the package. You cannot define a function like def (a, (B, c): pass.

10) the new octal variable modifies the oct () function accordingly.

The method of 2.x is as follows:

>>> 0666

438

>>> Oct (438)

'123'

3. X:

>>> 0666

SyntaxError: invalid token (

>>> 0o666

438

>>> Oct (438)

'0o666'

11) added the binary literal and bin () functions.

>>> Bin( 438)

'0b110110110'

>>> _ 438 = '0b110110110'

>>> _ 438

'0b110110110'

12) scalable and iterative unpacking. In Py3.X, a, B, * rest = seq and * rest, a = seq are both valid and only two points are required: rest is list

Objects and seq can be iterated.

13) the new super () can no longer pass parameters to super,

>>> Class C (object ):

Def _ init _ (self, ):

Print ('C',)

>>> Class D (C ):

Def _ init (self, ):

Super (). _ init _ (a) # super () called without Parameters ()

>>> D (8)

C 8

<__ Main _. D object at 0x00D7ED90>

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.