# Python2: import sys >>>sys.getdefaultencoding() 'ascii' # Python3: import sys >>>sys.getdefaultencoding() 'utf-8'
# Python2: >>> True = False >>> True False >>> True = 1 >>> True 1 >>> False = 'x' >>> False 'x' # Python3: >>> True = False File "<stdin>", line 1 SyntaxError: can't assign to keyword >>> True = 1 File "<stdin>", line 1 SyntaxError: can't assign to keyword >>> import keyword >>> keyword.iskeyword('True') True >>> keyword.kwlist ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
a = 3 def func1(): a = 1 def foo(): a = 2 foo() print(a) # 1 func1() def func2(): a = 1 def foo(): nonlocal a a = 2 foo() print(a) # 2 func2()
#Python2 >>> 6 / 2 3 >>> 6 //2 3 >>> 1 <> 2 True >>> 1 != 2 True >>> 'a' < 1 False >>> 'a' < 1 False >>> 'a' > 1 True #Python3 >>> 6 / 2 3.0 >>> 6 //2 3 >>> 1 <> 2 File "<stdin>", line 1 1 <> 2 ^ SyntaxError: invalid syntax >>> 1 != 2 True >>> 'a' < 1 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '<' not supported between instances of 'str' and 'int'
#Python2 >>> file <type 'file'> # Python3 >>> file Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'file' is not defined
摘自:html
Endpython