The difference between Python3 and Python2

Source: Internet
Author: User

Enter a different language version of the Python interactive environment

Py-2

Py-3

Execution of the PIP command:

Py-2-m pip install xxxx

Py-3-m pip install xxxx

PIP3 install nose This can also be

Execute Python file

Py-2 a.py

Py-3 a.py

You can also go to the python3.6 installation directory

Change the Python.exe to Python3.exe

Add this Python3.6 path to the environment variable

Python3 changed points: Print major changes, post print parentheses

D:\test\pytyon3>py-3

Python 3.6.5 (V3.6.5:F59C0932B4, Mar 2018, 17:00:18) [MSC v.1900-bit (AMD64)] on Win32

Type "Help", "copyright", "credits" or "license" for more information.

>>> print ("Hello")

Hello

>>> print ("Hello", end= "")

Hello>>>

Print ("Hello", end= ""), no end by default to enter the end, end indicates what end, it is not to end with a carriage return, python3 there is no xrange, only range, will generate an iterator, equivalent to Python2 xrange

>>> Range (1,10)

Range (1, 10)

>>> list (range (1,10))

[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>>

>>> for I in Range (1,10,2):

... print (i)

...

1

3

5

7

9

A=iter (range (10)) generates an iterator with next (a) access

>>> A=iter (Range (10))

>>> Next (a)

0

>>> Next (a)

1

>>> Next (a)

2

STR Type-Major changes

>>> s= "S"

>>> type (s)

<class ' str ' >

>>>

PYTHON3:STR type---"is equivalent to Py2 's Unicode type str type encode after it becomes bytes type---" equivalent to Python2 str type

>>> s= "S"

>>> type (s)

<class ' str ' >

>>> type (s.encode (' Utf-8 '))

<class ' bytes ' >

>>> s= "US"

>>> print (s)

We

>>> Print (S.encode ("GBK"))

B ' \xce\xd2\xc3\xc7 '

>>> Print (S.encode ("GBK"). Decode ("GBK"))

We

Python3 even if encode into gbk-and cmd default encoding format, also cannot display, because encode is the bytes type, to be displayed in Unicode format

Python3 default is Unicode type

Read file: Can perform encoding format fp=open ("D:\\a.txt", "R", encoding= "Utf-8")

>>> Fp=open ("D:\\a.txt", "R", encoding= "Utf-8")

>>> Fp.read ()

' \ufeff Glorious way to learn, \ Xia Xiaoxu \ Hee hee '

With open

>>> with open ("D:\\a.txt", "R", encoding= "Utf-8") as FP:

... print (Fp.read ())

...

The road to Glory is learning,

Xia Xiaoxu

Hee Hee

>>>

Python3 MySQL Installation

PIP3 Install Pymysql

Import Pymysql

# Open Database connection

db = Pymysql.connect ("localhost", "testuser", "test123", "TESTDB")

# Create a Cursor object using the cursor () method cursor

cursor = Db.cursor ()

# Execute SQL Query using the Execute () method

Cursor.execute ("Select VERSION ()")

# Use the Fetchone () method to get a single piece of data.

data = Cursor.fetchone ()

Print ("Database version:%s"% data)

# Close the database connection

Db.close ()

Serialization of Pickle

Pickle can put the variable in the text, the next time you open it can be taken from the text

Import Pickle

21:40 Listen deserialization

DATA2 = [1,2,3,4]

Det_str = Pickle.dumps (data2)

Print (DET_STR)

Pickle Reference Blog

Https://www.cnblogs.com/zhangxinqi/p/8034380.html

Thread changed to threading.

The thread module has been deprecated. Users can use the threading module instead. Therefore, the "thread" module can no longer be used in Python3. For compatibility, Python3 renames the thread to "_thread".

Import queue, first letter cannot be capitalized

>>> Import Queue

>>> Import Queue

Traceback (most recent):

File "<stdin>", line 1, in <module>

Modulenotfounderror:no module named ' Queue '

>>> queue. Queue (10)

<queue. Queue Object at 0x000001c4882ae860>

>>> Q=queue. Queue (10)

>>> q.put (0)

>>> Q.get ()

0

If you want to add Chinese to the time string, you need to introduce a locale package

>>> Import Time

>>> import Locale

>>> Locale.setlocale (locale. Lc_ctype, ' Chinese ')

' chinese_china.936 '

>>> Print (Time.strftime ('%y year%m Month%d '))

August 16, 2018

Note When writing a file: You can't use encode or anything.

Returns the number of bytes written after a file has been written

>>> with open ("E:\\x.txt", "W", encoding= "Utf-8") as FP:

... fp.write ("we \ \ \ \")

...

6

>>>

>>> with open ("D:\\a.txt", "W", encoding= "Utf-8") as FP:

... fp.write ("we")

...

2

There's no CMP function in Python3.

The difference between Python3 and Python2

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.