The difference between Python2 and Python3

Source: Internet
Author: User

Here's a summary of the differences between Python2 and Python3:

A. input

Using the input_raw () function in Python2

Using the input () function in Python3

Two. Output

Python2: Printstatement , the statement means that you can directly follow what you want to print, if you follow a tuple object, print directly

Python3: Theprint function , which functions as if it had to be bracketed to call--------print ()

Three. Encoding

Python2: Default encoding ASCII

Python3: Default encoding utf-8

Four. String type

Version Python2 Python3
byte string Str bytes
Text string Unicode Str

Five. Division

The result in Python2 is shaping, and floating-point types are in Python3.

Six. Import

Package Import order in Python2: standard library-relative pour (i.e. current directory)-Absolute import (Sys.path)

Package Import order in Python3: standard library-Absolute import (if you want to import relative, use from. Moudel)

Seven. Class

The default class in Python2 is a legacy class, and you need to explicitly inherit the modern Class (object) to create a modern class.

The old class is completely removed in Python3, all classes are modern, but you can still explicitly inherit the object class.

Eight. Renaming the module

Python3 Python2
Configparser Configparser
Filter Itertools.ifilter
Input Raw_input
Map Itertools.imap
Range Xrange
Functools.reduce Reduce
Socketserver Socketserver
Zip Itertools.izip

Nine. Module merging

Stringio and Cstringio in Python2 are merged into IO in Python3

The pickle and Cpickle in Python2 are merged into Pickle in Python3.

Urllib, Urllib2, and Urlparse in Python2 are merged Python3 in Urllib

10. Dictionaries

Keys, values, and items in the Dict class in Python2 return a list object, Iterkeys, Itervalues, and Iteritems return the builder object.

Python3 removes list, returns only one generator object, preserves only the view (builder), but the method is named: Keys, values, and items.

The 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.