Novice Python Learn to learn Python2 or Python3? __python

Source: Internet
Author: User
Tags integer division

People who want to learn Python have a puzzle, that is, Python currently has two versions of Python2 and Python3,python2 and Python3, what is the difference between two versions should learn which.

Python3 and Python2 are incompatible, and the differences are relatively large, python3 are not backward compatible, but most of the components and extensions are based on Python2. At present, most of the practical applications are not considered Python3, and sometimes pay attention to write compatible with 2/3 of the code. When you write new code with Python2, consider the possibility of migrating to Python3 later. According to statistics, currently 10% uses Python 3;20% to use Python 2 as well as Python 3,python2, and 70% uses Python 2.

In fact, Python is one of the most commonly used software on Linux, but the current version of Linux is mostly used Python2, and, on Linux rely on Python2 program more, so Python3 to replace python2 become mainstream still need a few years time. If you learn Python 2 to find a job or learn Python 2, it's not hard to go from Python 2 to Python3.

Said for a long time, Python2 and Python3 What is the difference?

1. Performance

The Py3.0 runs Pystone benchmark 30% slower than Py2.5. Guido that Py3.0 has great optimization space, and can achieve good results in string and plastic operation.

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

2. Code

py3.x source file defaults to use UTF-8 encoding

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

7 changes the behavior of the order operator, such as X<y, when the x and Y types do not match, throw the TypeError instead of returning the bool value immediately

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

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.

11 added 2 literal and bin () functions

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 object and SEQ is iterative.

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

14 The new Metaclass syntax:

15) Support class decorator.

4. Strings and byte strings

1 Now the string has only one type of STR, but it is almost the same as the 2.x version of Unicode.

2 for the byte string, see the 2nd Item of "Data type"

5. Data type

1 py3.x removes the long type and now has only one integral type--int, but behaves like 2. X version of Long

2 new types of bytes

6. Object-oriented

1) Introduce abstract base class (Abstraact base Classes,abcs).

2 the container class and iterator class are ABCs, so the type of cellections module is much more than Py2.5.

3 The next () method of the iterator is renamed __NEXT__ () and adds the built-in function next () to invoke the iterator's

4 added @abstractmethod and @abstractproperty two decorator, it is more convenient to write abstract methods (properties).

7. Abnormal

1 so the exception is inherited from Baseexception and deleted by Stardarderror

2 Removes the sequence behavior of the exception class and the. Message property

3) Replace raise Exception,args syntax with raise Exception (args)

4 catch Exception syntax change, introduce the AS keyword to identify the exception instance

5) exception chain, because __CONTEXT__ is not implemented in 3.0A1 version

8. Module changes
1 Remove the Cpickle module, you can use the Pickle module instead. In the end we will have a transparent and efficient module.

2 Remove Imageop Module

3) removed Audiodev, Bastion, bsddb185, exceptions, Linuxaudiodev, Md5,imewriter, Mimify, Popen2, rexec, sets, Sha, Stringold, Strop, sunaudiodev,timing and Xmllib modules

4 Remove the BSDDB module (published separately, can be obtained from Python "bindings" for Oracle Berkeley db)

5) Remove new module

6) Os.tmpnam () and Os.tmpfile () functions are moved to the Tmpfile module

7 Tokenize module now works with bytes. The main entry point is no longer generate_tokens, but Tokenize.tokenize ()

9. Other
1) xrange () renamed to Range (), to use range () to obtain a list, you must explicitly call the

2) Bytes objects cannot be hashed, and B.lower (), B.strip () and B.split () methods are not supported

3) Zip (), map (), and filter () return the iterator.

4) String.letters and related. Lowercase and uppercase are removed, please use string.ascii_letters, etc.

5 if x < y cannot be compared, throw TypeError exception

6 __GETSLICE__ series members are discarded.

7) The file class is discarded

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.