python3變化

一、print,在python3中已經是函數

>>> import sys
>>> print("fatal error",file=sys.stderr)
fatal error
>>> fp=open(r"d:\\a.txt",'w')
>>> print("python3",file=fp)
>>> fp.close()
>>> print("There are <", 2**32, "> possibilities!", sep=" ") 默認空格符爲分隔符
There are < 4294967296 > possibilities!
>>> print("There are <", 2**32, "> possibilities!", sep="") 自定義,不用空格分開
There are <4294967296> possibilities!
>>> 

二、python3中是unicode碼,而python2中是ascii碼,可以避免中文的編碼困擾

>>> import sys
>>> print(sys.getdefaultencoding())
utf-8
>>> "中文"
'中文'

在python2.7中

>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> "中文"
'\xd6\xd0\xc\xc4'


三、定義變量更靈活

>>> a,*b,c= range(5)
>>> a,b,c
(0, [1, 2, 3], 4)

四、某些庫改名了

Old Name --> New Name
_winreg ---> winreg
ConfigParser ---> configparser
copy_reg ---> copyreg
Queue ---> queue
SocketServer ---> socketserver
markupbase ---> _markupbase
repr ---> reprlib
test.test_support ---> test.support

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章