Python2 or Python3?__python?

Source: Internet
Author: User
Tags floor division

Not long ago learned Python2.7.5, recently read the CS50 course introduced Python3

found that the python2.x version and the python3.x version is still a big difference. Went to the official website, the 2.x and 3.0 of the main difference for a moment, trying to do the translation, there is no understanding of the place, there are wrong places, and then continue to learn the way to correct and add it.

1. Where is the difference? Simply put: Python 2.x is a legacy, Python 3.x is now and will be the future language. Python3.0 was released in 2008, Python2.7 's final version of 2.x was released in the middle of 2010, and also released this version of the "Last Words" "End-of-life release" 2.x version will not have a new major release. Since the 3.x version is being developed in great detail and has released many stable versions within five years, including the 3.3;2014 3.4;3.5 in 2015 of 2012, and 3.6 in 2016. This means that all of the current standard library upgrades are only performed on python3.x.

Guido van Rossum (the original creator of the Python language) decided to clean up Python 2.x and less to consider backwards compatibility. The biggest improvement is better support for Unicode (by default, all text strings are Unicode) and saner byte/Unicode seperation.
In addition, there are a number of changes to the core language, such as print and exec. , integers using floor division) is easier to adjust and more suitable for new scholars. is also more connected with other parts of the language. The ragged, obscure parts have been removed (for example, class is new) and the range () function returns an efficient memory iterator (a memory efficient iterable) rather than a 2.x list.

2.Python 3.0 What's new. https://docs.python.org/3/whatsnew/3.0.html#what-s-new-in-python-3-0 a.print function

Old:print ' The answer is ', 2*2
new:print ("The answer is", 2*2)

old:print x,           # trailing comma suppresses New Line before the comma ends, meaning do not create new lines.
new:print (x, end= "")  # Appends a space instead of a newline use end= "" Tell Python not to change lines

old:print              # Prints A newline  
new:print ()            # You must called the function! call function to create a new line

old:print >>sys.stderr, "Fatal error" C11/>new:print ("Fatal error", File=sys.stderr)

old:print (x, y)       # prints Repr ((x, y)) #repr ()
New:print ((x, y))      # not the same as print (x, y)!

Repr () is. Python has a way of converting any value into a string: passing it into the repr () or str () function. The function str () is used to convert the value into a human-readable form, the repr () translates into a form for the interpreter to read (if there is no equivalent syntax, a SYNTAXERROR exception occurs) if an object does not have a suitable explanatory form for human reading, STR () returns the value equivalent to the REPR (). Many types of structures, such as numbers or lists, dictionaries, have a unified interpretation of each function. strings and floating-point numbers, have a unique way of interpreting.
Can be customized between the separator output

Print ("There are <", 2**32, "> possibilities!", sep= "")
Output: There are <4294967296> possibilities!
The Note:1.python3 print () function does not support the space attribute in the original print (). For example, in Python 2.x, print "a\n", "B" will output "a\nb\n"; "There is a space between the middle escape character and B, print handled it by itself, eliminating the space" but in Python 3.0, print ("a\n", "B") outputs "a\n b\n". "In the P3, this feature is gone, it turns out to have spaces, then there are spaces" 2. Use print () instead of the print statement.
B.views and iterators instead of listsSome common APIs no longer return lists: the Dict.keys () in the Dict method, Dict.items (), Dict.values () return to "views"?? Instead of returning to lists. For example: This is not workable: K = D.keys (); K.sort (). Use k = sorted (d) instead (Python 2.5 is feasible). Similarly, no longer supports methods such as Dict.iterkeys (), Dict.iteritems (), Dict.itervalues () map () filter () returns the iterator Iterators range () to replace the original xrange () Zip () now returns an iterator c. operator operatorAn operator similar to 1 < ', 0 > None or len <= len is no longer valid. If none < none appear TypeError instead of returning false. As a result, arranging a heterogeneous list becomes no longer feasible-because all elements need to be comparable to other elements. "That is, the comparison requires that the same type of CMP () function no longer support"--cmp ()-also "__lt () __; __eq () is used to take __hash () __ d. Integers
The Pep237:long type is renamed int This means that there is only one built-in integer type, called int, but it behaves more like a PEP238 than the original long type: Returns float data in the form of 1/2, and returns int data in a representation such as 1//2.

The repr () function of a long integer no longer contains an L-end. The octal number is no longer 0720 "in the form of a 0" and replaced by a 0o720 form
E.text vs. Data Instead of Unicode vs. 8-bitPython3.0 uses text and binary data to replace the original Unicode string and the 8-bit string. All text is Unicode; however, compilation to Unicode behaves as binary data. You can no longer use U "..." to represent Unicode text, you must use B "..." to represent binary data because str and bytes types cannot be mixed again, so you have to explicitly switch between them. Use Str.encode () to change from str to bytes. Use Bytesdecode () to change from bytes to Str. Also can be bytes (s,encoding=) or str (b,encoding) Like str,bytes type is also immutable. There is a variable type called ByteArray, and almost all APIs accept bytes and ByteArray. The variable API is based on collections.mutablesequence. All raw strings will be parsed individually. This means that the original ' \u ' can jump out of the raw strings is no longer accepted. For example, R ' \u20ac ' is a six-character string in Python 3.0, and in 2.6, your ' \u20ac ' is a "euro" character. PEP 3120: The default soure encoding is UTF-8 's original Stringio and Cstringio module is gone, instead, import IO module and use IO separately. Stringio for text or Io.bytesio for data
d. New SyntaxPep3104:nonlocal's statement. With nonlocal x, you can directly assign an external variable (not Non-global) PEP 3132: An expanded, iterative unpacking. A, b, *rest = Some_sequence. is OK. And even, *rest, a = Stuff.rest is usually to (empty) the list
(A, *rest, b) = range (5)
Result: A to 0, B to 4, and rest to [1, 2, 3]. The new octal system, such as 0o720 (already in 2.6). The original octal system (0720) has been canceled. The new binary, such as 0b1010 (already in 2.6), now has a new built-in function bin () bytes also has a new built-in function bytes () metaclass syntax original Metaclass
Class C:
    __metaclass__ = M
    ...
Now is:
Class C (metaclass=m):
    ...
The original __metaclass__ no longer supports the removal of tuple parameter unpack. Cannot def (A, (b, c)):p This defines the function by removing the quote instead of repr () and using the <> instead. = removed keyword: exec (), it is no longer a keyword, but a function the integer data does not support the L or L end string data no longer supports the U or u opening from module import * The syntax of the From module imports * is allowed to be imported at the modular level, and is no longer supported within functions





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.