1.python2å¤å°ï¼
python2å¤å°ï¼
导读ï¼ä»å¤©é¦å¸CTOç¬è®°æ¥ç»åä½åäº«å ³äºpython2å¤å°çç¸å ³å 容ï¼å¦æè½ç¢°å·§è§£å³ä½ ç°å¨é¢ä¸´çé®é¢ï¼å«å¿äºå ³æ³¨æ¬ç«ï¼ç°å¨å¼å§å§ï¼Python2åPython3æåªäºä¸»è¦åºå«ä½ åºè¯¥æ¯åæ¥è§¦Pythonå§,建议ç´æ¥å¦Python3.x
ä¸é¢æ¯Python2.xä¸3.xçæ¬åºå«:
Pythonç3.0çæ¬ï¼å¸¸è¢«ç§°ä¸ºPythonï¼æç®ç§°Py3kãç¸å¯¹äºPythonçæ©æçæ¬ï¼è¿æ¯ä¸ä¸ªè¾å¤§çå级ã
为äºä¸å¸¦å ¥è¿å¤çç´¯èµï¼Python3.0å¨è®¾è®¡çæ¶å没æèèåä¸ç¸å®¹ã
许å¤é对æ©æPythonçæ¬è®¾è®¡çç¨å¼é½æ æ³å¨Python3.0ä¸æ£å¸¸æ§è¡ã
为äºç §é¡¾ç°æç¨å¼ï¼Python2.6ä½ä¸ºä¸ä¸ªè¿æ¸¡çæ¬ï¼åºæ¬ä½¿ç¨äºPython2.xçè¯æ³ååºï¼åæ¶èèäºåPython3.0çè¿ç§»ï¼å 许使ç¨é¨åPython3.0çè¯æ³ä¸å½æ°ã
æ°çPythonç¨å¼å»ºè®®ä½¿ç¨Python3.0çæ¬çè¯æ³ã
é¤éæ§è¡ç¯å¢æ æ³å®è£ Python3.0æè ç¨å¼æ¬èº«ä½¿ç¨äºä¸æ¯æ´Python3.0ç第ä¸æ¹åºãç®åä¸æ¯æ´Python3.0ç第ä¸æ¹åºæTwisted,py2exe,PILçã
大å¤æ°ç¬¬ä¸æ¹åºé½æ£å¨åªåå°ç¸å®¹Python3.0çæ¬ãå³ä½¿æ æ³ç«å³ä½¿ç¨Python3.0ï¼ä¹å»ºè®®ç¼åç¸å®¹Python3.0çæ¬çç¨å¼ï¼ç¶å使ç¨Python2.6,Python2.7æ¥æ§è¡ã
Python3.0çåå主è¦å¨ä»¥ä¸å 个æ¹é¢:
1.printå½æ°
printè¯å¥æ²¡æäºï¼åè代ä¹çæ¯print()å½æ°ãPython2.6ä¸Python2.7é¨åå°æ¯æè¿ç§å½¢å¼çprintè¯æ³ãå¨Python2.6ä¸Python2.7éé¢ï¼ä»¥ä¸ä¸ç§å½¢å¼æ¯çä»·çï¼
print?"fish"
print?("fish")?#注æprintåé¢æä¸ªç©ºæ ¼
print("fish")?#print()ä¸è½å¸¦æä»»ä½å ¶å®åæ°
ç¶èï¼Python2.6å®é å·²ç»æ¯ææ°çprint()è¯æ³ï¼
from?__future__?import?print_function
print("fish",?"panda",?sep=',?')
2.Unicode(è¿æ¯ä¸ä¸ªæå ¶å¥½çæ¹è¿)
Python2æASCIIstr()ç±»åï¼unicode()æ¯åç¬çï¼ä¸æ¯byteç±»åã
ç°å¨ï¼å¨Python3ï¼æ们æç»æäºUnicode(utf-8)å符串ï¼ä»¥åä¸ä¸ªåèç±»ï¼byteåbytearraysã
ç±äºPython3.Xæºç æ件é»è®¤ä½¿ç¨utf-8ç¼ç ï¼è¿å°±ä½¿å¾ä»¥ä¸ä»£ç æ¯åæ³çï¼
ä¸å½?=?'china'?print(ä¸å½)?
china
Python2.x
str?=?"æç±å京天å®é¨"str'\xe6\x\x\xe7\x\xb1\xe5\x8c\x\xe4\xba\xac\xe5\xa4\xa9\xe5\xae\x\xe9\x\xa8'
str?=?u"æç±å京天å®é¨"stru'\u\u\u\u4eac\u\u5b\ue8'
Python3.x
str?=?"æç±å京"str'æç±å京'
3.é¤æ³è¿ç®
Pythonä¸çé¤æ³è¾å ¶å®è¯è¨æ¾å¾é常é«ç«¯ï¼æå¥å¾å¤æçè§åãPythonä¸çé¤æ³æ两个è¿ç®ç¬¦ï¼/å//
é¦å æ¥è¯´/é¤æ³:
å¨python2.xä¸/é¤æ³å°±è·æ们çæç大å¤æ°è¯è¨ï¼æ¯å¦JavaåCåå·®ä¸å¤ï¼æ´æ°ç¸é¤çç»ææ¯ä¸ä¸ªæ´æ°ï¼æå°æ°é¨åå®å ¨å¿½ç¥æï¼æµ®ç¹æ°é¤æ³ä¼ä¿çå°æ°ç¹çé¨åå¾å°ä¸ä¸ªæµ®ç¹æ°çç»æã
å¨python3.xä¸/é¤æ³ä¸åè¿ä¹åäºï¼å¯¹äºæ´æ°ä¹é´çç¸é¤ï¼ç»æä¹ä¼æ¯æµ®ç¹æ°ã
Python2.x:
1?/?21.0?/?2.00.5
Python3.x:
1/20.5
注æçæ¯å¹¶ä¸æ¯èå¼å°æ°é¨åï¼èæ¯æ§è¡flooræä½ï¼å¦æè¦æªåå°æ°é¨åï¼é£ä¹éè¦ä½¿ç¨math模åçtruncå½æ°
python3.x:
import?mathmath.trunc(1?/?2)math.trunc(-1?/?2)4.å¼å¸¸
å¨Python3ä¸å¤çå¼å¸¸ä¹è½»å¾®çæ¹åäºï¼å¨Python3ä¸æ们ç°å¨ä½¿ç¨asä½ä¸ºå ³é®è¯ã
æè·å¼å¸¸çè¯æ³ç±?exceptexc,var?æ¹ä¸º?exceptexcasvarã
使ç¨è¯æ³except(exc1,exc2)asvarå¯ä»¥åæ¶æè·å¤ç§ç±»å«çå¼å¸¸ãPython2.6å·²ç»æ¯æè¿ä¸¤ç§è¯æ³ã
1.å¨2.xæ¶ä»£ï¼ææç±»åç对象é½æ¯å¯ä»¥è¢«ç´æ¥æåºçï¼å¨3.xæ¶ä»£ï¼åªæ继æ¿èªBaseExceptionç对象æå¯ä»¥è¢«æåºã
2.2.xraiseè¯å¥ä½¿ç¨éå·å°æåºå¯¹è±¡ç±»åååæ°åå¼ï¼3.xåæ¶äºè¿ç§å¥è©çåæ³ï¼ç´æ¥è°ç¨æé å½æ°æåºå¯¹è±¡å³å¯ã
å¨2.xæ¶ä»£ï¼å¼å¸¸å¨ä»£ç ä¸é¤äºè¡¨ç¤ºç¨åºé误ï¼è¿ç»å¸¸åä¸äºæ®éæ§å¶ç»æåºè¯¥åçäºæ ï¼å¨3.xä¸å¯ä»¥çåºï¼è®¾è®¡è 让å¼å¸¸åçæ´å ä¸ä¸ï¼åªæå¨é误åççæ åµæè½å»ç¨å¼å¸¸æè·è¯å¥æ¥å¤çã
5.xrange
å¨Python2ä¸xrange()å建è¿ä»£å¯¹è±¡çç¨æ³æ¯é常æµè¡çãæ¯å¦ï¼for循ç¯æè æ¯å表/éå/åå ¸æ¨å¯¼å¼ã
è¿ä¸ªè¡¨ç°åååçæå¨ï¼æ¯å¦ã"æ°æ§æ±å¼"ï¼ãä½æ¯è¿ä¸ªxrange-iterableæ¯æ ç©·çï¼æå³çä½ å¯ä»¥æ ééåã
ç±äºå®çæ°æ§æ±å¼ï¼å¦æä½ ä¸å¾ä» ä» ä¸éåå®ä¸æ¬¡ï¼xrange()å½æ°æ¯range()æ´å¿«ï¼æ¯å¦for循ç¯ï¼ã尽管å¦æ¤ï¼å¯¹æ¯è¿ä»£ä¸æ¬¡ï¼ä¸å»ºè®®ä½ éå¤è¿ä»£å¤æ¬¡ï¼å 为çæå¨æ¯æ¬¡é½ä»å¤´å¼å§ã
å¨Python3ä¸ï¼range()æ¯åxrange()é£æ ·å®ç°ä»¥è³äºä¸ä¸ªä¸é¨çxrange()å½æ°é½ä¸ååå¨ï¼å¨Python3ä¸xrange()ä¼æåºå½åå¼å¸¸ï¼ã
import?timeit
n?=?
def?test_range(n):
return?for?i?in?range(n):
pass
def?test_xrange(n):
for?i?in?xrange(n):
pass
Python2
print?'Python',?python_version()
print?'\ntiming?range()'?
%timeit?test_range(n)
print?'\n\ntiming?xrange()'?
%timeit?test_xrange(n)
Python?2.7.6
timing?range()
?loops,?best?of?3:?s?per?loop
timing?xrange()
?loops,?best?of?3:?s?per?loop
Python3
print('Python',?python_version())
print('\ntiming?range()')
%timeit?test_range(n)
Python?3.4.1
timing?range()
?loops,?best?of?3:?s?per?loop
print(xrange())
---------------------------------------------------------------------------
NameError?Traceback?(most?recent?call?last)
ipython-input-5-5d8f9bea?in?module()
----?1?print(xrange())
NameError:?name?'xrange'?is?not?defined
6.å «è¿å¶åé¢é表示
å «è¿å¶æ°å¿ é¡»åæ0oï¼åæ¥çå½¢å¼ä¸è½ç¨äºï¼äºè¿å¶å¿ é¡»åæ0bã
æ°å¢äºä¸ä¸ªbin()å½æ°ç¨äºå°ä¸ä¸ªæ´æ°è½¬æ¢æäºè¿å¶å串ãPython2.6å·²ç»æ¯æè¿ä¸¤ç§è¯æ³ã
å¨Python3.xä¸ï¼è¡¨ç¤ºå «è¿å¶åé¢éçæ¹å¼åªæä¸ç§ï¼å°±æ¯0oã
python2.x
0o
python3.x
File?"stdin",?line?1
^
SyntaxError:?invalid?token
0o
7.ä¸çè¿ç®ç¬¦
Python2.xä¸ä¸çäºæ两ç§åæ³!=å
Python3.xä¸å»æäº,åªæ!=ä¸ç§åæ³ï¼è¿å¥½ï¼æä»æ¥æ²¡æ使ç¨çä¹ æ¯
8.å»æäºrepr表达å¼``
Python2.xä¸åå¼å·``ç¸å½äºreprå½æ°çä½ç¨
Python3.xä¸å»æäº``è¿ç§åæ³ï¼åªå 许使ç¨reprå½æ°ï¼è¿æ ·åçç®çæ¯ä¸ºäºä½¿ä»£ç çä¸å»æ´æ¸ æ°ä¹ï¼ä¸è¿ææè§ç¨reprçæºä¼å¾å°ï¼ä¸è¬åªå¨debugçæ¶åæç¨ï¼å¤æ°æ¶åè¿æ¯ç¨strå½æ°æ¥ç¨å符串æ述对象ã
def?sendMail(from_:?str,?to:?str,?title:?str,?body:?str)?-?bool:
pass
9.å¤ä¸ªæ¨¡å被æ¹åï¼æ ¹æ®PEP8ï¼
StringIO模åç°å¨è¢«å并å°æ°çio模ç»å ãnew,md5,gopherlibç模å被å é¤ãPython2.6å·²ç»æ¯æ´æ°çio模ç»ã
httplib,BaseHTTPServer,CGIHTTPServer,SimpleHTTPServer,Cookie,cookielib被å并å°httpå å ã
åæ¶äºexecè¯å¥ï¼åªå©ä¸exec()å½æ°ãPython2.6å·²ç»æ¯æ´exec()å½æ°ã
.æ°æ®ç±»å
1ï¼Py3.Xå»é¤äºlongç±»åï¼ç°å¨åªæä¸ç§æ´åââintï¼ä½å®çè¡ä¸ºå°±å2.Xçæ¬çlong
2ï¼æ°å¢äºbytesç±»åï¼å¯¹åºäº2.Xçæ¬çå «ä½ä¸²ï¼å®ä¹ä¸ä¸ªbytesåé¢éçæ¹æ³å¦ä¸ï¼
b?=?b'china'?type(b)?type?'bytes'
str对象åbytes对象å¯ä»¥ä½¿ç¨.encode()(str-bytes)or.decode()(bytes-str)æ¹æ³ç¸äºè½¬åã
s?=?b.decode()?s?'china'?
b1?=?s.encode()?b1?b'china'
3ï¼dictç.keys()ã.itemså.values()æ¹æ³è¿åè¿ä»£å¨ï¼èä¹åçiterkeys()çå½æ°é½è¢«åºå¼ãåæ¶å»æçè¿ædict.has_key()ï¼ç¨inæ¿ä»£å®å§ã
python2.7æ¯pythonå ï¼
Python2.7æ¯Python2çæ¬ï¼Python3.xææ¯Python3çæ¬ã
Python2ä¸Python3ç主è¦åºå«ï¼
åºå«ä¸:printè¯æ³ä½¿ç¨
Python2.7printè¯æ³ä½¿ç¨print"HelloPython"?
Python3.7printè¯æ³ä½¿ç¨print("HelloPython")
åºå«äº:raw_input()åinput()
Python2.7?raw_input()?input()é½åå¨å¯ä½¿ç¨raw_input()æ¥æ¶å符串string?input()æ¥æ¶æ°åint/flot.
Python3.7?raw_input()ä¸åå¨?ä» åå¨input()两è å并?æ¥æ¶ä»»ææ ¼å¼è¿åstring
åºå«ä¸:å½æ°cmp()python2.7cmp(x,y)å½æ°ç¨äºæ¯è¾2个对象ï¼å¦æxyè¿å-1,å¦æx==yè¿å0,å¦æxyè¿å1
python3.7cmp()å·²ç»ä¸åå¨äº,å¦æä½ éè¦å®ç°æ¯è¾åè½ï¼éè¦å¼å ¥operator模åï¼éåä»»ä½å¯¹è±¡
为ä»ä¹è¯´pythonæ¯2æ¯å·¦ç§»ï¼å®åé¢è·çæ°åï¼å°±æ¯ç§»å¨çä½æ°
ï¼ä¹å°±æ¯æ2å¾å·¦ç§»å¨2ä½
æ2转æ¢æ2è¿å¶ï¼å°±æ¯
左移两ä½å°±åæï¼ä¹å°±æ¯è¿å¶ç8
类似å°ï¼å¯ä»¥è¿ä¹ç®ï¼
---ï¼ä¹å°±æ¯åè¿å¶ç
ç»è¯ï¼ä»¥ä¸å°±æ¯é¦å¸CTOç¬è®°ä¸ºå¤§å®¶æ´ççå ³äºpython2å¤å°çç¸å ³å 容解çæ±æ»äºï¼å¸æ对æ¨ææ帮å©ï¼å¦æ解å³äºæ¨çé®é¢æ¬¢è¿å享ç»æ´å¤å ³æ³¨æ¤é®é¢çæååï½