程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Differences between python3 and python2

編輯:Python
What Xiaoting is sharing with you today is Python3 And Python2 The difference of .

Python3 And Python2 The difference of

be based on python3 Talking about python3 And python2 The difference of . Because of the current mainstream Python3, But I used to use Python2 Project done , We have to maintain it , So as python Worker , You have to understand the differences , among ,Python2 Yes ASCII str() type ,unicode() It's alone , No byte type . and Python3.X The source file defaults to utf-8 code , And a byte class :byte and bytearrays. This makes the following code legal :

Coding differences :

 I = 'zhongguo'
print( I )
Python3 result :zhongguo

notes :python2 China is illegal , No matter code or comment, Chinese characters cannot appear , Unless stated :#!/usr/bin/python -*- coding: UTF-8 -*-

As a default specification or code literacy , You don't usually use Chinese characters , Try to make the code as good as possible python spot !

python3 And python2 The biggest difference is print Output , Please refer to print Use :https://blog.csdn.net/u010986753

Python 3 In the version print The sentence is gone , In its place print() function .

print differences :

Output correctly "life is short we neeed python!" The code is as follows :

print('life is short we neeed python!')

Python3 result :life is short we neeed python!

Python2 Print in :

print "life is short we neeed python!"/
print 'life is short we neeed python!'/
print ('life is short we neeed python!') 

The output is the same !

Divide the difference

 > print(7/3)> 

Python3 result :2.3333333333333335

> Python2 result :2

> Python3 It means division print(7//3)

The difference of inequality :

Python2 There are not two ways of writing != and <>

Python3 Removed from <>, Only != A way of writing

The difference between integers :

Python2 There is an integer in —int And long -long

Python3 There is only one type of integer in —int

The difference in cue information :

Python2 in raw_input( " Prompt information " ) and input( " Prompt information " )

Python3 There are only input( " Prompt information " )

Differences in opening files :

Python2 in file( ..... ) or open(.....)

Python3 There are only open(.....)

map、filter and reduce The difference of :

Python2 In interactive mode :

>>> map
<built-in function map>
>>> filter<built-in function filter>
>>> reduce<built-in function reduce>

The result types they output are all lists :

>>> map(lambda x:x +2, [4,1,3])
[6, 3, 5]
>>> filter(lambda x:x %2 ==0,range(9))
[0, 2, 4, 6, 8]

Python3 In interactive mode : They go from functions to classes , secondly , Their return result is also an iterative object from the original list

>>> map<class 'map'>
>>> map(print,[1,2,3])
<map object at 0x10d8bd400>
>>> filter<class 'filter'>
>>> filter(lambda x:x % 2 == 0, range(10))
<filter object at 0x10d8bd3c8>

Traversal tuples For the more high-end reduce function , It's in Python3 China no longer belongs to built-in 了 , Moved to functools Modules . If you need to write a list parsing that traverses tuples ,Python2 You don't need to put parentheses around the tuple value . stay python3 in , These brackets are required .

Python2 in [ i for i in 1, 2]

Python3 in [i for i in (1,2)]

Get a range of numbers

python2 in , There are two ways to get a range of numbers :range(), Return a list , also xrange(), Returns an iterator .

python3 in ,range() Return iterator ,xrange() No longer exist .

Python2 in [ i for i in 1, 2]

Python3 in [i for i in (1,2)]

Welcome to xiaotinger's blog https://blog.csdn.net/u010986753 Please leave a message , It will be revised as soon as possible !

DB Written interview history link

http://mp.weixin.qq.com/s/Vm5PqNcDcITkOr9cQg6T7w

About Me: Wheat seedling

● The author of this article : Xiaoting'er

● Author's blog address :https://blog.csdn.net/u010986753

● copyright , Welcome to share this article , Reprint please keep the source


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved