The main difference between Python2 and Python3
(You may understand there is not in place, and then come back to change)
Reference: Liu Zhijun, what are the main differences between Python 2 and Python 3? "Answer
https://www.zhihu.com/question/19698598
Py2:print Statement , the statement means that you can directly follow what you want to print, and if it is followed by a tuple object, print directly
Py3:print function , the function thinks that this must be added in parentheses to call, if the tuple object, you can receive multiple positional parameters, and can print
If you want to use print as a function in Python2, you can import print_function in the future module
Here's an example:
# py2>>> print("hello", "world")(‘hello‘, ‘world‘)# py3>>> print("hello", "world")hello world
# py2>>> print("hello", "world")(‘hello‘, ‘world‘)>>> from __future__ import print_function>>> print("hello", "world")hello world
Py2:input_raw ()
Py3:input ()
- The difference between using super ()
Py2: The base class must be shown in the parameters
PY: directly without parameter calls
Py2: Return 0
Py3: Returns 0.5, without the difference between int and long
PY2: Default encoding ASCII
PY3: Default encoding Utf-8
And in order to use Chinese in PY2, introducing coding declaration in the head, it is not recommended to use
The Py2:unicode type represents a sequence of strings, and the str type represents a sequence of bytes
The py3::str type represents a sequence of strings, and a byte type represents a sequence of bytes
Py2:true and False are two global variables in Python2, can be assigned values or perform other operations, with initial values of 1 and 0, although modifications violate the principles of Python design, but you can actually change
PY3: Fixed this variable so that true or false is not mutable
Py2: Many of the methods that return a list, such as range, the Dict.keys () of the Dictionary object, the Dict.values () method, map, filter, zip, and the iterator must implement the next method
Py3: Changed the method of returning the list to return iterator object, built-in __next__, do not need to implement next
Py2: There is no way to declare a variable as a non-local variable in a nested function, only a global variable can be declared in a function
The Py3:nonlocal method is implemented with the following example:
def func(): c = 1 def foo(): c = 12 foo() print(c)func() #1
def func(): c = 1 def foo(): nonlocal c c = 12 foo() print(c)func() # 12
The main difference between Python2 and Python3