Python 02-python2.x differs from version 3.x

Source: Internet
Author: User
Tags floor division

  • In order not to take in too much of a burden, Python 3.0 did not consider downward compatibility when designing. As a result, many programs designed for early Python versions cannot perform 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.
Print function
  • The print statement is gone and replaced by the print () function.
print "fish"print ("fish") #注意print后面有个空格print("fish")  #print()不能带有任何其它参数
Unicode
  • Python 2 has an ASCII str () type, Unicode () is separate, not a byte type.
  • Python 3 has a Unicode (Utf-8) string, and a byte class: Byte and Bytearrays.
Python 2.x
  >>> str = "I love Beijing Tian ' an gate" >>> str ' \xe6\x88\x91\xe7\x88\xb1\xe5\x8c\x97\ Xe4\xba\xac\xe5\xa4\xa9\xe5\xae\x89\xe9\x97\xa8 ' >>> str = u "I love Beijing Tian ' an gate" >>> Stru ' \u6211\u7231\u5317\ U4eac\u5929\u5b89\u95e8 '  
Python 3.x
  >>> str = "I love Beijing Cheonan" >>> str ' I love Beijing Tian ' an gate ' 
Division
  • 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//
/Division:
  • in Python 2.x/division is familiar with most of the languages, such as Java AH C ah almost
    • integer phase In addition to the result is an integer, the fractional part is completely ignored the
    • floating-point number division will retain the part of the decimal point to obtain the result of a floating point.
  • in Python 3.x/division no longer does this
    • divides the integers, and the result is a floating-point number.
python 2.x:
  >>> 1/20>>> 1.0/2.00.5  
Python 3.x:

>>> 1/20.5 //Division:

  • This division is called Floor Division
  • will automatically perform a floor operation on the result of Division
  • in Pytho N 2.x and Python 3.x are consistent
Python 2.x:
  >>>-1//2-1  
Python 3.x:
  >&G  T;>-1//2-1  
  • Note that instead of discarding the fractional part, the floor operation is performed, and if you want to intercept the integer part, you need to use the Math module's trunc function
python 3.x:
  >>> import math>>> math.trunc (0>>> math.trunc ( -1/2) 0  
Abnormal
  • In Python 3 We now use as as the keyword.
  • 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.
    • 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.
    • The 2.x raise statement uses commas to separate the thrown object type from the parameter, and 3.x cancels the work of this kind of flower, directly calling the constructor to throw the object.
  • In the 2.x era, exceptions in the code in addition to the program errors, but also often do something common control structure should do
  • In 3.x, it can be seen that the designer makes the anomaly more exclusive, only in the case of error occurred in order to use the exception capture statement to deal with.
xrange
  • In Python 2, the use of xrange () to create iterative objects is very popular. For example, a for loop or a list/set/dictionary derivation.
  • 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).
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
>>> 0o1000512>>> 01000512
Python 3.x
>>> 01000  File "<stdin>", line 1    01000        ^SyntaxError: invalid token>>> 0o1000512
inequality operator
  • Python 2.x does not equal two ways!! = and <>
  • Python 3.x is stripped of <> only! = a notation
removed the repr expression '
  • The inverse quotation mark in Python 2.x is equivalent to the effect of the REPR function
  • Python 3.x is stripped of this notation, allowing only the REPR function to be used
multiple modules renamed (according to PEP8) TR class= "even" >
old name new name
_winreg winreg
CONFIGP Arser configparser
copy_reg copyreg
queue queue
socketserver Sock Etserver
repr reprlib
    The Li>stringio module is now incorporated into the new IO module. Modules such as
  • new, MD5, gopherlib, and so on are removed. The
  • Python 2.6 already supports the new IO module.
  • httplib, Basehttpserver, Cgihttpserver, Simplehttpserver, cookies, Cookielib are incorporated into the HTTP package.
  • canceled the EXEC statement, leaving only the Exec () function.
  • Python 2.6 already supports the EXEC () function.
Data type
  • py3.x removed the long type and now has only one integer--int, but it behaves like 2. X version of Long
  • Added the bytes type, which corresponds to 2. X version of the eight-bit string, define a bytes literal method as follows:
>>> b = b‘china‘ >>> type(b) <type ‘bytes‘>
  • str objects and Bytes objects can be converted using the. encode () (str-bytes) or. Decode () (bytes-str) methods.
>>> s = b.decode() >>> s ‘china‘ >>> b1 = s.encode() >>> b1 b‘china‘
  • Dict. Keys (),. Items, and. Values () methods return iterators, and functions such as the previous iterkeys () are discarded.
  • Also remove the Dict.has_key (), with in instead of it.
Interactive mode
  • In python2.x raw_input () and input (), two functions are present, the difference being:
    • Raw_input (): treats all inputs as strings, returning string types
    • Input (): Only the input of "number" can be received, it has its own attribute when dealing with a pure digital input, it returns the type of the number entered (int, float)
  • Raw_input () and input () are consolidated in python3.x, removing the raw_input (), leaving only the input () function, which receives any wayward input, defaults all input to string processing, and returns the string type.

Python 02-python2.x differs from version 3.x

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.