當前位置:首頁 » 編程語言 » python3x2x

python3x2x

發布時間: 2023-08-24 21:59:25

A. python2.x和3.xinput功能的區別

在python2版本中,輸入功能是通過input()和raw_input()實現的。在python3版本中,是通過input()函數實現的。

Python2和Python3中input()函數區別如下:

1.輸入同為數值時。(整型和浮點型)

Python3中input()輸出的都是字元串;

Python2中input()輸出的是它們原來的類型。

B. Python的這幾個版本區別

系統區別:

1、Download Windows X86 可供電腦系統32位使用。

2、Download Windows X86-64 可供電腦系統64位使用。

版本區別:

1、Download Windows X86 web-based installer 是通過需要聯網完成安裝的文件。

2、Download Windows X86 executable installer 是可執行的安裝文件,下載後,雙擊安裝即可。

3、Download Windows X86 embeddable zip file 是可嵌入式的壓縮包文件,可以集成到其他應用中。

(2)python3x2x擴展閱讀:

Python2與Python3的選擇:

因為早期的 Python版本在基礎方面設計存在著一些不足之處。2008 年的時候 Guido van Rossum 又重新開發 Python 3.0(被稱為Python 3000,或簡稱Py3k),Python3在設計的時候很好地解決了這些遺留問題, 並且在性能上也有了一定的提升,。

然而 Python3 帶來的最大的問題就是不完全向後兼容,當時向後兼容的版本是Python2.6。Python開發團隊無法一下子就讓所有項目和類庫都轉到 Python3.0 上面。

所以,兩個版本就進入了長期並行開發和維護的狀態。但是就更新速度來說 ,Python3更新速度遠快於Python2的速度,因為Python2目前主要以維護為主。Python3是未來的趨勢。

C. python2和3的區別大嗎

1、Python2之前是非常受歡迎的,現在很多軟體公司依然使用的是Python2,不過由於越來越多的公司開始從Python2遷移到3,因此初學者可以避免花費更多的時間學習過去的版本了。
2、Python2和Python3之間有一些是無法互相兼容的,現在很多開發人員都在使用Python3開發庫,許多老的基於Python2的開發包依然是無法兼容的,想要將Python2庫移植到Python3上,對於初學者來說還是比較困難的事情。
3、Python3有更好的Unicode編碼支持在Python3的語法之中,默認文化字元串就是Unicode,而Python2默認的存儲為ASCII,而且Unicode比ASCII更加通用,可以提供更多的選擇。
4、Python3版本改進了整數除法的問題,在Python2中如果你在小數點後面寫任何一個數字,計算會自動四捨五入的。而使用Python3的情況下是不需要擔心這種情況發生的。
5、兩個版本之間有不同的列印語句聲明語法,是語法差異,不影響Python的功能。

D. python3.4學習筆記 3.x和2.x的區別,持續更新

python3.4學習筆記(四) 3.x和2.x的區別

在2.x中:print html,3.x中必須改成:print(html)

import urllib2
ImportError: No mole named 'urllib2'
在python3.x裡面,用urllib.request代替urllib2

import thread
ImportError: No mole named 'thread'
在python3.x裡面,用_thread(在前面加一個下劃線)代替thread

在2.x中except Exception,e : 3.x中改為except (Exception):

=================================
print函數
雖然print語法是Python 3中一個很小的改動,且應該已經廣為人知,但依然值得提一下:Python 2中的print語句被Python 3中的print()函數取代,這意味著在Python 3中必須用括弧將需要輸出的對象括起來。
在Python 2中使用額外的括弧也是可以的。但反過來在Python 3中想以Python2的形式不帶括弧調用print函數時,會觸發SyntaxError。
Python 2.7.6

print 'Python', python_version()
print 'Hello, World!'
print('Hello, World!')
print "text", ; print 'print more text on the same line'
輸出:
Hello, World!
Hello, World!
text print more text on the same line
---------------------------

Python 3.4.1
print('Python', python_version())
print('Hello, World!')

print("some text,", end="")
print(' print more text on the same line')
輸出:
Hello, World!
some text, print more text on the same line
print 'Hello, World!'
File "<ipython-input-3-139a7c5835bd>", line 1
print 'Hello, World!'
^
SyntaxError: invalid syntax

注意:在Python中,帶不帶括弧輸出」Hello World」都很正常。
但如果在圓括弧中同時輸出多個對象時,就會創建一個元組,這是因為在Python 2中,print是一個語句,而不是函數調用。

print 'Python', python_version()
print('a', 'b')
print 'a', 'b'
Python 2.7.7
('a', 'b')
a b

---------------------------------
整數除法
由於人們常常會忽視Python 3在整數除法上的改動(寫錯了也不會觸發Syntax Error),所以在移植代碼或在Python 2中執行Python 3的代碼時,需要特別注意這個改動。

所以,我還是會在Python 3的腳本中嘗試用float(3)/2或 3/2.0代替3/2,以此來避免代碼在Python
2環境下可能導致的錯誤(或與之相反,在Python 2腳本中用from __future__ import division來使用Python
3的除法)。

Python 2.7.6
3 / 2 = 1
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0

Python 3.4.1
3 / 2 = 1.5
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0
---------------------------------
Unicode
Python 2有基於ASCII的str()類型,其可通過單獨的unicode()函數轉成unicode類型,但沒有byte類型。
而在Python 3中,終於有了Unicode(utf-8)字元串,以及兩個位元組類:bytes和bytearrays。

Python 2.7.6
print type(unicode('this is like a python3 str type'))
<type 'unicode'>
print type(b'byte type does not exist')
<type 'str'>
print 'they are really' + b' the same'
they are really the same
print type(bytearray(b'bytearray oddly does exist though'))
<type 'bytearray'>

Python 3.4.1 has <class 'bytes'>
print('and Python', python_version(), end="")
print(' also has', type(bytearray(b'bytearrays')))
and Python 3.4.1 also has <class 'bytearray'>
1
'note that we cannot add a string' + b'bytes for data'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-d3e8942ccf81> in <mole>()
----> 1 'note that we cannot add a string' + b'bytes for data'

TypeError: Can't convert 'bytes' object to str implicitly

=================================

python 2.4 與 python 3.0 的比較
一、 print 從語句變為函數
原: print 1,2+3
改為: print ( 1,2+3 )

二、range 與 xrange
原 : range( 0, 4 ) 結果 是 列表 [0,1,2,3 ]
改為:list( range(0,4) )
原 : xrange( 0, 4 ) 適用於 for 循環的變數控制
改為:range(0,4)

三、字元串
原: 字元串以 8-bit 字元串存儲
改為: 字元串以 16-bit Unicode 字元串存儲

四、try except 語句的變化
在2.x中except Exception,e : 3.x中改為except (Exception):

五、打開文件
原: file( ..... )
或 open(.....)
改為:
只能用 open(.....)
六、從鍵盤錄入一個字元串
原: raw_input( "提示信息" )
改為: input( "提示信息" )

七、bytes 數據類型
A bytes object is an immutable array. The items are 8-bit bytes, represented by integers in the range 0 <= x < 256.
bytes 可以看成是「位元組數組」對象,每個元素是 8-bit 的位元組,取值范圍 0~255。
由於在 python 3.0中字元串以 unicode 編碼存儲,當寫入二進制文件時,字元串無法直接寫入(或讀取),必須以某種方式的編碼為位元組序列後,方可寫入。

(一)字元串編碼(encode) 為 bytes
例: s = "張三abc12"
b = s.encode( 編碼方式)
# b 就是 bytes 類型的數據
# 常用的編碼方式為 : "uft-16" , "utf-8", "gbk", "gb2312", "ascii" , "latin1" 等
# 注 : 當字元串不能編碼為指定的「編碼方式」時,會引發異常
(二) bytes 解碼(decode)為字元串
s = "張三abc12"
b = s.encode( "gbk") # 字元串 s 編碼為 gbk 格式的位元組序列
s1 = b.decode("gbk") # 將位元組序列 b以gbk格式 解碼為字元串
# 說明,當位元組序列不能以指定的編碼格式解碼時會引發異常
(三)使用方法舉例
#coding=gbk
f = open("c:\\1234.txt", "wb")
s = "張三李四abcd1234"
# -------------------------------
# 在 python2.4 中我們可以這樣寫:
# f.write( s )
# 但在 python 3.0中會引發異常
# -------------------------------
b = s.encode("gbk")
f.write( b )
f.close()
input("?")
讀取該文件的例子:
#coding=gbk
f = open("c:\\1234.txt", "rb")
f.seek(0,2) #定位至文件尾
n = f.tell() #讀取文件的位元組數
f.seek(0,0) #重新定位至文件開始處
b = f.read( n )
# ------------------------------
# 在 python 2.4 中 b 是字元串類型
# 要 python 3.0 中 b 是 bytes 類型
# 因此需要按指定的編碼方式確碼
# ------------------------------
s = b.decode("gbk")
print ( s )
# ------------------------------
# 在 python 2.4 中 可以寫作 print s 或 print ( s )
# 要 python 3.0 中 必須寫作 print ( s )
# ------------------------------
f.close()
input("?")
運行後應顯示:
張三李四abcd1234

(四) bytes序列,一但形成,其內容是不可變的,例:
s="ABCD"
b=s.encode("gbk")
print b[0] # 顯示 65
b[0] = 66
# 執行該句,出現異常: 'bytes' object does not support item assignment

八、 chr( K ) 與 ord( c )
python 2.4.2以前
chr( K ) 將編碼K 轉為字元,K的范圍是 0 ~ 255
ord( c ) 取單個字元的編碼, 返回值的范圍: 0 ~ 255
python 3.0
chr( K ) 將編碼K 轉為字元,K的范圍是 0 ~ 65535
ord( c ) 取單個字元的編碼, 返回值的范圍: 0 ~ 65535

九、 除法運算符
python 2.4.2以前
10/3 結果為 3
python 3.0
10 / 3 結果為 3.3333333333333335
10 // 3 結果為 3
十、位元組數組對象 --- 新增
(一) 初始化
a = bytearray( 10 )
# a 是一個由十個位元組組成的數組,其每個元素是一個位元組,類型借用 int
# 此時,每個元素初始值為 0
(二) 位元組數組 是可變的
a = bytearray( 10 )
a[0] = 25
# 可以用賦值語句更改其元素,但所賦的值必須在 0 ~ 255 之間
(三) 位元組數組的切片仍是位元組數組
(四) 字元串轉化為位元組數組
#coding=gbk
s ="你好"
b = s.encode( "gbk") # 先將字元串按某種「GBK」編碼方式轉化為 bytes
c = bytearray( b ) #再將 bytes 轉化為 位元組數組
也可以寫作
c = bytearray( "你好", "gbk")

(五) 位元組數組轉化為字元串
c = bytearray( 4 )
c[0] = 65 ; c[1]=66; c[2]= 67; c[3]= 68
s = c.decode( "gbk" )
print ( s )
# 應顯示: ABCD

(六) 位元組數組可用於寫入文本文件
#coding=gbk
f = open("c:\\1234.txt", "wb")
s = "張三李四abcd1234"
# -------------------------------
# 在 python2.4 中我們可以這樣寫:
# f.write( s )
# 但在 python 3.0中會引發異常
# -------------------------------
b = s.encode("gbk")
f.write( b )
c=bytearray( "王五","gbk")
f.write( c )
f.close()
input("?")

熱點內容
phpposthtml 發布:2025-02-04 21:37:46 瀏覽:87
最新asp源碼 發布:2025-02-04 21:17:33 瀏覽:570
讓linux死機 發布:2025-02-04 20:48:08 瀏覽:141
單方塊生存伺服器里如何獲取岩漿 發布:2025-02-04 20:48:07 瀏覽:785
快速指數演算法 發布:2025-02-04 20:20:40 瀏覽:299
python在類中定義函數調用函數 發布:2025-02-04 20:14:47 瀏覽:596
安卓手機的壁紙是哪個 發布:2025-02-04 20:14:44 瀏覽:202
java發展前景 發布:2025-02-04 20:10:19 瀏覽:77
mac登陸密碼哪裡設置 發布:2025-02-04 19:50:20 瀏覽:526
手游腳本封號 發布:2025-02-04 19:42:12 瀏覽:439