1.python2åpython3çåºå«
2.为ä»ä¹è¿ä¸¤å¹´pythonç¼ç¨è¯è¨ç¹å«ç«ï¼
python2åpython3çåºå«
ä½ åºè¯¥æ¯åæ¥è§¦Pythonå§,saas系统源码建议ç´æ¥å¦Python3.xä¸é¢æ¯Python2.xä¸3.xçæ¬åºå«:
Pythonç3.0çæ¬ï¼å¸¸è¢«ç§°ä¸ºPython ï¼æç®ç§°Py3kãç¸å¯¹äºPythonçæ©æçæ¬ï¼è¿æ¯ä¸ä¸ªè¾å¤§çå级ã
为äºä¸å¸¦å ¥è¿å¤çç´¯èµï¼Python 3.0å¨è®¾è®¡çæ¶å没æèèåä¸ç¸å®¹ã
许å¤é对æ©æPythonçæ¬è®¾è®¡çç¨å¼é½æ æ³å¨Python 3.0ä¸æ£å¸¸æ§è¡ã
为äºç §é¡¾ç°æç¨å¼ï¼Python 2.6ä½ä¸ºä¸ä¸ªè¿æ¸¡çæ¬ï¼åºæ¬ä½¿ç¨äºPython 2.xçè¯æ³ååºï¼åæ¶èèäºåPython 3.0çè¿ç§»ï¼å 许使ç¨é¨åPython 3.0çè¯æ³ä¸å½æ°ã
æ°çPythonç¨å¼å»ºè®®ä½¿ç¨Python 3.0çæ¬çè¯æ³ã
é¤éæ§è¡ç¯å¢æ æ³å®è£ Python 3.0æè ç¨å¼æ¬èº«ä½¿ç¨äºä¸æ¯æ´Python 3.0ç第ä¸æ¹åºãç®åä¸æ¯æ´Python 3.0ç第ä¸æ¹åºæTwisted, py2exe, PILçã
大å¤æ°ç¬¬ä¸æ¹åºé½æ£å¨åªåå°ç¸å®¹Python 3.0çæ¬ãå³ä½¿æ æ³ç«å³ä½¿ç¨Python 3.0ï¼ä¹å»ºè®®ç¼åç¸å®¹Python 3.0çæ¬çç¨å¼ï¼ç¶å使ç¨Python 2.6, Python 2.7æ¥æ§è¡ã
Python 3.0çåå主è¦å¨ä»¥ä¸å 个æ¹é¢:
1.print å½æ°
printè¯å¥æ²¡æäºï¼åè代ä¹çæ¯print()å½æ°ã Python 2.6ä¸Python 2.7é¨åå°æ¯æè¿ç§å½¢å¼çprintè¯æ³ãå¨Python 2.6ä¸Python 2.7éé¢ï¼ä»¥ä¸ä¸ç§å½¢å¼æ¯çä»·çï¼
print "fish"print ("fish") #注æprintåé¢æä¸ªç©ºæ ¼
print("fish") #print()ä¸è½å¸¦æä»»ä½å ¶å®åæ°
ç¶èï¼Python 2.6å®é å·²ç»æ¯ææ°çprint()è¯æ³ï¼
from __future__ import print_functionprint("fish", "panda", sep=', ')
2.Unicode(è¿æ¯ä¸ä¸ªæå ¶å¥½çæ¹è¿)
Python 2 æ ASCII str() ç±»åï¼unicode() æ¯åç¬çï¼ä¸æ¯ byte ç±»åã
ç°å¨ï¼ å¨ Python 3ï¼æ们æç»æäº Unicode (utf-8) å符串ï¼ä»¥åä¸ä¸ªåèç±»ï¼byte å bytearraysã
ç±äº Python3.X æºç æ件é»è®¤ä½¿ç¨utf-8ç¼ç ï¼è¿å°±ä½¿å¾ä»¥ä¸ä»£ç æ¯åæ³çï¼
>>> ä¸å½ = 'china'>>>print(ä¸å½)
china
Python 2.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"æç±å京天å®é¨"
>>> str
u'\u\u\u\u4eac\u\u5b\ue8'
Python 3.x
>>> str = "æç±å京">>> str
'æç±å京'
3.é¤æ³è¿ç®
Pythonä¸çé¤æ³è¾å ¶å®è¯è¨æ¾å¾é常é«ç«¯ï¼æå¥å¾å¤æçè§åãPythonä¸çé¤æ³æ两个è¿ç®ç¬¦ï¼/å//
é¦å æ¥è¯´/é¤æ³:
å¨python 2.xä¸/é¤æ³å°±è·æ们çæç大å¤æ°è¯è¨ï¼æ¯å¦JavaåCåå·®ä¸å¤ï¼æ´æ°ç¸é¤çç»ææ¯ä¸ä¸ªæ´æ°ï¼æå°æ°é¨åå®å ¨å¿½ç¥æï¼æµ®ç¹æ°é¤æ³ä¼ä¿çå°æ°ç¹çé¨åå¾å°ä¸ä¸ªæµ®ç¹æ°çç»æã
å¨python 3.xä¸/é¤æ³ä¸åè¿ä¹åäºï¼å¯¹äºæ´æ°ä¹é´çç¸é¤ï¼ç»æä¹ä¼æ¯æµ®ç¹æ°ã
Python 2.x:
>>> 1 / 20
>>> 1.0 / 2.0
0.5
Python 3.x:
>>> 1/20.5
注æçæ¯å¹¶ä¸æ¯èå¼å°æ°é¨åï¼èæ¯æ§è¡flooræä½ï¼å¦æè¦æªåå°æ°é¨åï¼é£ä¹éè¦ä½¿ç¨math模åçtruncå½æ°
python 3.x:
>>> import math>>> math.trunc(1 / 2)
0
>>> math.trunc(-1 / 2)
0
4.å¼å¸¸
å¨ Python 3 ä¸å¤çå¼å¸¸ä¹è½»å¾®çæ¹åäºï¼å¨ Python 3 ä¸æ们ç°å¨ä½¿ç¨ as ä½ä¸ºå ³é®è¯ã
æè·å¼å¸¸çè¯æ³ç± except exc, var æ¹ä¸º except exc as varã
使ç¨è¯æ³except (exc1, exc2) as varå¯ä»¥åæ¶æè·å¤ç§ç±»å«çå¼å¸¸ã Python 2.6å·²ç»æ¯æè¿ä¸¤ç§è¯æ³ã
1. å¨2.xæ¶ä»£ï¼ææç±»åç对象é½æ¯å¯ä»¥è¢«ç´æ¥æåºçï¼å¨3.xæ¶ä»£ï¼åªæ继æ¿èªBaseExceptionç对象æå¯ä»¥è¢«æåºã
2. 2.x raiseè¯å¥ä½¿ç¨éå·å°æåºå¯¹è±¡ç±»åååæ°åå¼ï¼3.xåæ¶äºè¿ç§å¥è©çåæ³ï¼ç´æ¥è°ç¨æé å½æ°æåºå¯¹è±¡å³å¯ã
å¨2.xæ¶ä»£ï¼å¼å¸¸å¨ä»£ç ä¸é¤äºè¡¨ç¤ºç¨åºé误ï¼è¿ç»å¸¸åä¸äºæ®éæ§å¶ç»æåºè¯¥åçäºæ ï¼å¨3.xä¸å¯ä»¥çåºï¼è®¾è®¡è 让å¼å¸¸åçæ´å ä¸ä¸ï¼åªæå¨é误åççæ åµæè½å»ç¨å¼å¸¸æè·è¯å¥æ¥å¤çã
5.xrange
å¨ Python 2 ä¸ xrange() å建è¿ä»£å¯¹è±¡çç¨æ³æ¯é常æµè¡çãæ¯å¦ï¼ for 循ç¯æè æ¯å表/éå/åå ¸æ¨å¯¼å¼ã
è¿ä¸ªè¡¨ç°åååçæå¨ï¼æ¯å¦ã"æ°æ§æ±å¼"ï¼ãä½æ¯è¿ä¸ª xrange-iterable æ¯æ ç©·çï¼æå³çä½ å¯ä»¥æ ééåã
ç±äºå®çæ°æ§æ±å¼ï¼å¦æä½ ä¸å¾ä» ä» ä¸éåå®ä¸æ¬¡ï¼xrange() å½æ° æ¯ range() æ´å¿«ï¼æ¯å¦ for 循ç¯ï¼ã尽管å¦æ¤ï¼å¯¹æ¯è¿ä»£ä¸æ¬¡ï¼ä¸å»ºè®®ä½ éå¤è¿ä»£å¤æ¬¡ï¼å 为çæå¨æ¯æ¬¡é½ä»å¤´å¼å§ã
å¨ Python 3 ä¸ï¼range() æ¯å xrange() é£æ ·å®ç°ä»¥è³äºä¸ä¸ªä¸é¨ç xrange() å½æ°é½ä¸ååå¨ï¼å¨ Python 3 ä¸ xrange() ä¼æåºå½åå¼å¸¸ï¼ã
import timeitn =
def test_range(n):
return for i in range(n):
pass
def test_xrange(n):
for i in xrange(n):
pass
Python 2
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
Python 3
print('Python', python_version())print('\ntiming range()')
%timeit test_range(n)
Python 3.4.1
timing range()
loops, best of 3: µs per loopprint(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()å½æ°ç¨äºå°ä¸ä¸ªæ´æ°è½¬æ¢æäºè¿å¶å串ã Python 2.6å·²ç»æ¯æè¿ä¸¤ç§è¯æ³ã
å¨Python 3.xä¸ï¼è¡¨ç¤ºå «è¿å¶åé¢éçæ¹å¼åªæä¸ç§ï¼å°±æ¯0oã
python 2.x
>>> 0o
>>>
python 3.x>>>File "<stdin>", line 1
^
SyntaxError: invalid token
>>> 0o
7.ä¸çè¿ç®ç¬¦Python 2.xä¸ä¸çäºæ两ç§åæ³ != å <>
Python 3.xä¸å»æäº<>, åªæ!=ä¸ç§åæ³ï¼è¿å¥½ï¼æä»æ¥æ²¡æ使ç¨<>çä¹ æ¯
8.å»æäºrepr表达å¼``
Python 2.x ä¸åå¼å·``ç¸å½äºreprå½æ°çä½ç¨
Python 3.x ä¸å»æäº``è¿ç§åæ³ï¼åªå 许使ç¨reprå½æ°ï¼è¿æ ·åçç®çæ¯ä¸ºäºä½¿ä»£ç çä¸å»æ´æ¸ æ°ä¹ï¼ä¸è¿ææè§ç¨reprçæºä¼å¾å°ï¼ä¸è¬åªå¨debugçæ¶åæç¨ï¼å¤æ°æ¶åè¿æ¯ç¨strå½æ°æ¥ç¨å符串æ述对象ã
def sendMail(from_: str, to: str, title: str, body: str) -> bool:pass
9.å¤ä¸ªæ¨¡å被æ¹åï¼æ ¹æ®PEP8ï¼
StringIO模åç°å¨è¢«å并å°æ°çio模ç»å ã new, md5, gopherlibç模å被å é¤ã Python 2.6å·²ç»æ¯æ´æ°çio模ç»ã
httplib, BaseHTTPServer, CGIHTTPServer, SimpleHTTPServer, Cookie, cookielib被å并å°httpå å ã
åæ¶äºexecè¯å¥ï¼åªå©ä¸exec()å½æ°ã Python 2.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æ¿ä»£å®å§ ã
为ä»ä¹è¿ä¸¤å¹´pythonç¼ç¨è¯è¨ç¹å«ç«ï¼
å¨ç¼ç¨è¯è¨ä¸ï¼ Python é¿æç¨³å± åäºï¼ä¸ä» å·²ç»æ为æ°æ®åæã人工æºè½é¢åå¿ ä¸å¯å°çå·¥å ·ï¼è¿è¢«è¶æ¥è¶å¤å°å ¬å¸ç¨äºç½ç«æ建ãPython æ¹åå²ä½çèªæ°´å¨æ°´æ¶¨è¹é«ï¼æ为ç®åæææ½åçç¼ç¨è¯è¨ä¹ä¸ã
Python æ¯ä¸é¨æ°æå好ãåè½å¼ºå¤§ãé«æçµæ´»çç¼ç¨è¯è¨ï¼å¦ä¼ä¹åæ 论æ¯æ³è¿å ¥æ°æ®åæã人工æºè½ãç½ç«å¼åè¿äºé¢åï¼è¿æ¯å¸æææ¡ç¬¬ä¸é¨ç¼ç¨è¯è¨ï¼é½å¯ä»¥ç¨ Python æ¥å¼å¯æ éæªæ¥çæ éå¯è½ï¼
pythonè½å¹²ä»ä¹ï¼
个人å¼åï¼
1.Pythonæé常å®åçåºç¡ä»£ç åºï¼æ ååºï¼å¼æºåºï¼èªå®ä¹æ¨¡åï¼
æ¯å¦è¯´ï¼ä½ è¦å¼åä¸ä¸ªç½ç»ç¬è«ï¼ä½ å¯è½åªéè¦ä½¿ç¨å 个代ç åºå°±è¡äº,re,requests,selenium,beautifulsoup
2.å¾å¤äººç¨djangoæ¥å¼åèªå·±ä¸ªäººå客ï¼Djangoæ¯ä¸ä¸ªå¼æ¾æºä»£ç çWebåºç¨æ¡æ¶ï¼ç±Pythonåæã
3.ä¹æ人使ç¨Twistedæ¥å¼åç½ç»æå¡ç¨åºï¼Twistedæ¯ç¨Pythonå®ç°çåºäºäºä»¶é©±å¨çç½ç»å¼ææ¡æ¶ï¼Twistedæ¯æ许å¤å¸¸è§çä¼ è¾ååºç¨å±åè®®ï¼å æ¬TCPãUDPãSSL/TLSãHTTPãIMAPãSSHãIRC以åFTPã
4.æ人ç¨pythonæ¥å¼å游æãΣ( ° Ⳡ°|||)︴é£æºå¤§æï¼ï¼
6.æè¿ççé¢æ¸¸æççé¢ä½å¼å¨å¾å¤é½æ¯ç¨pythonå¼åçã
7.æ»èè¨ä¹ï¼å¯¹äºä¸ªäººå¼åæ¥è¯´ï¼pythonå¯ä»¥èçä½ çæ¶é´åç®åä½ ç代ç éã