1,市場差異
python2:官方通知python2 2020開始不再維護,但企業很多**都是python2,python2有很大的使用者基群故會出現歷史遺留問題,
需要很長時間的遷移過度到python3
python3:最新版本,但目前市場使用量不大
2,系統差異
python2:是centos 6-7系統預設支援的python版本
python3:是centos8系統預設支援的python版本
3,輸出差異(**舉例)
python2:
>>
> print "abc"
abc>>
> print(
"abc"
)abc
python3:
>>
> print "abc"
會報錯>>
> print(
"abc"
)abc
4,輸入差異
python 2: input 預設接收整形資料,str型別要用引號包起來,或用raw_input函式可以接收字串
python 3: input 函式接收,獲得輸入型別為字元型
**舉例
python2:
>>
> input(
"請輸入任意字元:"
)請輸入任意字元:as
報錯》> input(
"請輸入任意字元"
)請輸入任意字元:「as」
報錯》> input(
"請輸入任意字元"
)請輸入任意字元:'as'
'as'
>>
> raw_input(
"請輸入任意字元"
)請輸入任意字元:as
'as'
python3:
>>
> input(
"請輸入任意字元:"
)請輸入任意字元:ad
'ad'
>>
> input(
"請輸入任意字元:"
)請輸入任意字元:『ad』
'『ad』'
>>
> raw_input(
"請輸入任意字元:"
)報錯
#即raw_input 在python3中已廢除
5,資料型別與運算子差異
整除python3: /表示真除
python2:整數相除只能獲取商值,浮點型資料相除才能獲得真實資料
整形資料
python3:只有int
python2:區分int和long
**舉例
pyhton2:
>>
> a=999999999999
>>
> print(type(a))
>
>>
> c=99999999999999999999999999999
>>
> print(type(c))
>
pyhton3:
python3:
>>
> a=999999
>>
> print(type(a))
>
>>
> c=99999999999999999999999999999999999999999999999
>>
> print(type(c))
>
6,range和xrange的區別
**舉例
pyhton2:
>>
> range(0,4)
[0, 1, 2, 3]
>>
> list(xrange(1,4))
[1, 2, 3]
>>
> xrange(0,4)
xrange(4)
pyhotn3:
>>
> range(0,4)
range(0, 4)
>>
> list(range(0,4))
[0, 1, 2, 3]
python3已廢除了xrange
7,異常機制的區別
**舉例
python2
>>
> try:
... a = 1/0
... except exception,e:
... print e
python3
>>
> try:
... a = 1/0
... except exception as e:
... print(e)
8,字元編碼的區別
pyhton2:預設編碼是ascii碼
只支援英文本母,數字,特殊字元,不支援中文含有中文需要做編碼宣告,用utf-8:
#--coding: utf-8-- #encoding=utf-8
pyhton3:預設編碼是utf-8
9,布林型別的區別
python2 true、false 是兩個變數 可以更改
python3 true、false變成兩個關鍵字 不能進行修改
**舉例
python2:
>>
> true = 3
>>
> print true
3>>
> false = 66
>>
> print false
66
python3:
>>
> true = 2
報錯》> false = 4
報錯
10,模組匯入的區別
python2 預設是相對匯入,自己建立模組時,必須要有__init__.py的檔案
python3 無要求
11,字串的區別
python2 unicode型別表示字串序列,str型別表示位元組序列
python3 str型別表示字串序列,byte型別表示位元組序列
python3和python2的區別
1.效能 py3.0執行 pystone benchmark的速度比py2.5慢30 guido認為py3.0有極大的優化空間,在字串和整形操作上可 以取得很好的優化結果。py3.1效能比py2.5慢15 還有很大的提公升空間。2.編碼 py3.x原始碼檔案預設使用utf 8編碼,這就使得以下 是合...
Python2和Python3的比較
python2 python3 print fish print fish unicode 是單獨的 unicode utf 8 字串 b b china byte 和 bytearrays type b type bytes s b.decode 轉化成 string 型別b1 s encode ...
python3和python2的區別
這個星期開始學習python了,因為看的書都是基於python2.x,而且我安裝的是python3.1,所以書上寫的地方好多都不適用於python3.1,特意在google上search了一下3.x和2.x的區別。特此在自己的空間中記錄一下,以備以後查詢方便,也可以分享給想學習python的frie...