在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Python/ Python3引入什么新的東西?
Python3文件操作
Python3日期和時間
Python3基礎(chǔ)語法
Python3字典
Python3元組
Python3文件方法
Python3字符串
Python3引入什么新的東西?
Python3異常處理
Python3模塊
Python3數(shù)字
Python3變量類型
Python3函數(shù)
Python3循環(huán)
Python3 os文件目錄的方法
Python3 while循環(huán)語句
Python3斷言
Python3基本運算符
Python3環(huán)境安裝設(shè)置
Python3標(biāo)準(zhǔn)異常
Python3嵌套循環(huán)
Python3教程
Python3決策
Python3 for循環(huán)語句
Python3列表

Python3引入什么新的東西?

__future__ 模塊

Python 3.x引入一些Python2不兼容的關(guān)鍵字和函數(shù),可以通過在 Python2 內(nèi)置的模塊 __future__ 導(dǎo)入。建議如果你想在代碼中支持 Python3.x,使用__future__導(dǎo)入它。

例如,如果想在 Python2 中擁有 Python 3.x 整數(shù)的除法行為,添加下面的 import 語句
from __future__ import division

print函數(shù)

在 Python3 最值得注意和最廣為人知的變化是print函數(shù)的使用。print 函數(shù)使用的括號()在Python3中是強(qiáng)制性的。它在 Python2 中是可選的。

print "Hello World" #is acceptable in Python 2
print ("Hello World") # in Python 3, print must be followed by () 

print()函數(shù)默認(rèn)情況下在結(jié)束時會插入一個換行。在 Python2,它可以通過 ',' 在末行抑制輸出換行。 在 Python3 則使用"end=' '" 附加空格,而不是換行

print x,           # Trailing comma suppresses newline in Python 2
print(x, end=" ")  # Appends a space instead of a newline in Python 3

從鍵盤讀取輸入

Python2 中有輸入函數(shù)兩個版本。 input() 和 raw_input()。如果它被包含在引號 '' 或 "",input() 對待接收到的數(shù)據(jù)作為字符串,否則數(shù)據(jù)將被視為數(shù)字類型。

在 Python3 中 raw_input()函數(shù)已被棄用。此外,接收到的輸入數(shù)據(jù)總是作為字符串處理。
In Python 2 >>> x=input('something:') 
something:10 #entered data is treated as number
>>> x
10
>>> x=input('something:')
something:'10' #eentered data is treated as string
>>> x
'10'
>>> x=raw_input("something:")
something:10 #entered data is treated as string even without ''
>>> x
'10'
>>> x=raw_input("something:")
something:'10' #entered data treated as string including ''
>>> x
"'10'" In Python 3 >>> x=input("something:")
something:10
>>> x
'10'
>>> x=input("something:")
something:'10' #entered data treated as string with or without ''
>>> x
"'10'"
>>> x=raw_input("something:") # will result NameError
Traceback (most recent call last):
  File "", line 1, in  x=raw_input("something:")
NameError: name 'raw_input' is not defined 

整數(shù)除法

在Python2,兩個整數(shù)的除法的結(jié)果會四舍五入到最接近的整數(shù)。如:3/2 其結(jié)果將顯示 1。 為了獲得一個浮點除法,分子或分母必須明確為浮點數(shù)。因此無論是 3.0/2 或 3/2.0 或 3.0/2.0 將產(chǎn)生1.5 。

Python3 計算 3/2 默認(rèn)結(jié)果值為 1.5,這對新手程序員更加直觀。

Unicode表示

Python2 里如果你想將它保存為 Unicode,需要標(biāo)記為 U 的字符串。

Python3 中的字符串默認(rèn)存儲為 Unicode。在Python3,我們有個Unicode(UTF-8)字符串和 2 字節(jié)類:字節(jié)和字節(jié)數(shù)組。

xrange()函數(shù)已被刪除

在 Python2 的 range() 函數(shù)返回一個列表,還有 xrange()返回一個對象只會在需要時在范圍內(nèi)產(chǎn)生所需項目以節(jié)省內(nèi)存。

在Python3,range()函數(shù)去除了,而 xrange()已更名為 range()。 另外在 Python3.2 以及更高的版本中, range()對象支持切片。

引發(fā)異常

Python2 中同時接受符號的'大膽'和'新'的語法;如果我們不在括號中括入異常參數(shù),Python3 中會引發(fā)一個 SyntaxError:

raise IOError, "file error" #This is accepted in Python 2
raise IOError("file error") #This is also accepted in Python 2
raise IOError, "file error" #syntax error is raised in Python 3
raise IOError("file error") #this is the recommended syntax in Python 3

異常的參數(shù)

在 Python3,異常參數(shù)應(yīng)以 'as' 關(guān)鍵字來聲明。
except Myerror, err: # In Python2
except Myerror as err: #In Python 3

next() 函數(shù)和.next()方法

在Python 2,next() 作為生成器對象的一個方法是允許的。在 Python2,next()函數(shù)過度產(chǎn)生器對象遍歷也是可以接受的。在Python3,但是,next()函數(shù)作為生成器方法來中止并引發(fā)AttributeError。

gen = (letter for letter in 'Hello World') # creates generator object

next(my_generator) #allowed in Python 2 and Python 3
my_generator.next() #allowed in Python 2. raises AttributeError in Python 3

2to3實用工具

隨著 Python3 解釋器,2t03.py 腳本將被通常安裝在  tools/scripts 文件夾。 它讀取 Python2.x 源代碼,并應(yīng)用了一系列的修復(fù)將它轉(zhuǎn)變成有效的 Python3.x 代碼。

Here is a sample Python 2 code (area.py):

def area(x,y=3.14): 
    a=y*x*x
    print a
    return a

a=area(10)
print "area",a

To convert into Python 3 version:

$2to3 -w area.py

Converted code :

def area(x,y=3.14): # formal parameters
    a=y*x*x
    print (a)
    return a

a=area(10)
print("area",a)