The main difference between Python2 and Python3

Source: Internet
Author: User

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

    • Print

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
    • Input function

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

    • Results of 1/2

Py2: Return 0

Py3: Returns 0.5, without the difference between int and long

    • Coding

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

    • String

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

    • True and False

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

    • Iterators

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

    • Nonlocal

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

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.