Python 2.x and python3.x main difference table

Source: Internet
Author: User
Tags new set

python2.4+ and python3.0+ major changes or additions

Python2 Python3
Print is a built-in command print becomes a function
Print >> f,x,y print (x,y,file=f)
Print x, print (x,end= ")
Reload (m) imp.reload (m)
Apply (f, PS, KS) F (*ps, **ks)
x <> y x! = y
Long int
1234L 1234
D.has_key (k) k in D or D.get (k)! = None (Has_key dead, in immortality!)
Raw_input () input ()
Input () eval (input ())
Xrange (A, b) range (A, B)
File () Open ()
X.next () x.__next__ () and called by the next () method
X.__GETSLICE__ () x.__getitem__ ()
X.__SETSILCE__ () x.__setitem__ ()
__cmp__ () removed __cmp__ (), instead of __lt__ (), __gt__ (), __eq__ (), etc.
Reduce () functools.reduce ()
Exefile (filename) EXEC (open (filename). Read ())
0567 0o567 (octal)
New nonlocal keyword
STR for Unicode text, bytes for binary text
New Iterator method Range,map,zip, etc.
New set parsing and dictionary parsing
U ' unicodestr ' unicodestr '
Raise E,v Raise E (V)
Except E, x:except e as x:
File.xreadlines for line in file: (or X = iter (file))
D.keys (), D.items (), etc list (D.keys ()), List (D.items ()),

List (etc)
Map (), zip (), etc list (map ()), List (Zip ()), list (etc)
X=d.keys (); X.sort () sorted (d)
X.__nonzero__ () x.__bool__ ()
x.__hex__,x.__bin__ x.__index__
Types. ListType List
__metaclass__ = M class C (Metaclass = m):
__builtin__ Builtins
Sys.exc_type,etc sys.exc_info () [0],sys.exc_info () [1],...
Function.func_code function.__code__
Add Keyword-one parameter
Add Ellipse Object
simplifies the super () method syntax
Use-t,-tt to control indent and mix spaces and tabs as errors
From M Import * can only appear at the top level of the file
appear in any location.
Class Myexception:class MyException (Exception):
Thread,queue Module Renaming _thread,queue
Cpickle,socketserverModuleRenamed _pickle,socketserver
Configsparser Module Renaming Configsparser
Tkinter module renaming Tkinter
Other modules are integrated into such as HTTP module, Urllib, URLLIB2 module, etc.
Os.popen subprocess. Popen
Class-based exceptions for string-based exceptions
Property mechanism of new Class (class attribute)
Unbound methods are functions
Mixed type comparable sort non-numeric mixed type comparison error occurred
/Is traditional division de-Traditional division,/change to True division
No function annotations have function Annotations def f (a:100, b:str)->int used through f.__annotation__
New environment Manager With/as
Python3.1 supports multiple environment manager items with a () as a, B () as B
Extended sequence unpacking A, *b = seq
Unify all classes for modern classes
Enhanced __slot__ class Properties
If X: Priority x.__len__ () Priority x.__bool__ ()
Type (I) district classification and type no longer differentiate (no longer distinguish between modern and classic classes, but also extended meta classes)
Static methods require the self parameter static method to be used directly according to the declaration
No abnormal chain with abnormal chain raise exception from Other_exception

Python 2.x and python3.x main difference table

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.