±à¼ÍƼö: |
±¾ÎÄÀ´×ÔÓÚcnblogs,ÎÄÕÂÖ÷Òª½éÉÜÁËIpython»ù´¡ÖªÊ¶¡¢%runÃüÁîÒÔ¼°shellÃüÁîºÍ±ðÃûµÈÏà¹ØÖªÊ¶¡£ |
|

°²×°ipythonÓÃpip¼´¿É¡£ps.²©Ö÷ÓõÄÊÇwin7ϵͳ£¬ËùÒÔ½ÓÏÂÀ´µÄ¶¼ÊÇÔÚwindowsϵͳϲÙ×÷µÄ¡£
Ò».Ipython»ù´¡
Æô¶¯£º¿ªÊ¼²Ëµ¥-ÊäÈëcmd-»Ø³µ-ÊäÈëipython


³õ³¢ÊÔ

¶þ.Tab¼ü×Ô¶¯Íê³É
ÔÚshellÖÐÊäÈë±í´ïʽʱ£¬Ö»Òª°´ÏÂTab¼ü£¬µ±Ç°ÃüÁî¿Ø¼þÖÐÈκÎÓëÊäÈëµÄ×Ö·û´®ÏàÆ¥ÅäµÄ±äÁ¿£¨¶ÔÏ󡢺¯ÊýµÈ£©¾Í»á±»ÕÒ³öÀ´¡£
ps.֮ǰÎÒ·¢ÏÖÎÒµÄipythonûÓÐTab¼ü×Ô¶¯²¹È«¹¦ÄÜ£¬tab¼üµÄ¹¦ÄÜÊÇËõ½ø¡£×îºó pip install
pyreadline¼´¿É½â¾ö¡£
ÏÂÃæÕâ¸öÀý×Ó£¬ÊäÈëb.ºó°´ÏÂ<tab>¼ü¼´¿ÉµÃµ½ÏÂͼµÄ½á¹û¡£

Èý.ÄÚÊ¡
ÔÚ±äÁ¿µÄÇ°Ãæ»òºóÃæ¼ÓÉÏÒ»¸öÎʺÅ(?)¾Í¿ÉÒÔ½«ÓйظöÔÏóµÄһЩͨÓÃÐÅÏ¢ÏÔʾ³öÀ´¡£Õâ¾Í½Ð×ö¶ÔÏóµÄÄÚÊ¡¡£

Èç¹û¶ÔÏóÊÇÒ»¸öº¯Êý»òʵÀý·½·¨£¬ÔòÆädocstringÒ²»á±»ÏÖʵ³öÀ´¡£
ʹÓÃ??»¹½«ÏÔʾ³ö¸Ãº¯ÊýµÄÔ´´úÂë¡£

һЩ×Ö·û´®ÅäÒÔͨÅä·û(*)¼´¿ÉÏÔʾ³öËùÓÐÓë¸ÃͨÅä·û±í´ïʽÏàÆ¥ÅäµÄÃû³Æ¡£
ÀýÈ磬ÎÒÃÇ¿ÉÒÔÁгöNumpy¶¥¼¶ÃüÃû¿Õ¼äÖк¬ÓС°load¡±µÄËùÓк¯Êý

¼òֱ̫°ôÁËÓÐľÓУ¡£¡£¡£¡£¡£¡£¡
ËÄ.%runÃüÁî
ÔÚipython»á»°»·¾³ÖУ¬ËùÓÐÎļþ¶¼¿ÉÒÔͨ¹ý%runÃüÁîµ±×öPython³ÌÐòÀ´ÔËÐС£
ÊäÈë %run ·¾¶+pythonÎļþÃû³Æ¼´¿É¡£
ps.¡¶¼¯ÌåÖǻ۱à³Ì¡·ÀïÃæ´úÂëµÄÖ´ÐпÉÒÔÓÃÕâ¸ö
Îå.ÖжÏÕýÔÚÖ´ÐеĴúÂ루¼±ÐÔ×ӱر¸23333£©
°´ÏÂCtrl-C¾Í»áÒý·¢Ò»¸öKeyboardInterrupt¡£³ýÁËһЩ·Ç³£ÌØÊâµÄÇé¿öÏ£¬¾ø´ó²¿·Öpython³ÌÐò¶¼»áÁ¢¼´Í£Ö¹Ö´ÐÐ
Áù.Ö´ÐмôÌù°åÖеĴúÂë
ÊéÉÏ˵ʹÓÃctrl+shift+v½«¼ôÌù°åµÄ´úÂëÆ¬¶ÎÕ³Ìù³öÀ´£¬È»¶øwindowsÏµÍ³ËÆºõ²»¿ÉÐУ¬ËùÒÔÊó±êÓÒ¼üÕ³ÌùºÃÁË¡£
%paste¿ÉÒÔ³ÐÔØ¼ôÌù°åÖеÄÒ»ÇÐÎı¾£¬²¢ÔÚshellÖÐÒÔÕûÌåÐÎʽִÐС£
%cpaste¸ú%paste²î²»¶à£¬Ö»²»¹ýËü¶à³öÁËÒ»¸öÓÃÓÚÕ³Ìù´úÂëµÄÌØÊâÌáʾ·û¶øÒÑ¡£Èç¹û·¢ÏÖÕ³ÌùµÄ´úÂëÓÐ´í£¬Ö»ÐèÒª°´ÏÂctrl+c¼´¿ÉÖÕÖ¹%cpasteÌáʾÈç¡£
Æß.¼üÅÌ¿ì½Ý¼ü
Æäʵ²»µ½ÕæÕýʹÓÃÎÒÒ²¼Ç²»×¡23333£¬ÔÚ´ËÕ³Ìù¸ø´ó¼Ò×÷Ϊ±¸Íü¡£
Ctrl-P »òÉϼýÍ·¼ü ºóÏòËÑË÷ÃüÁîÀúÊ·ÖÐÒÔµ±Ç°ÊäÈëµÄÎı¾¿ªÍ·µÄÃüÁî
Ctrl-N »òϼýÍ·¼ü ǰÏòËÑË÷ÃüÁîÀúÊ·ÖÐÒÔµ±Ç°ÊäÈëµÄÎı¾¿ªÍ·µÄÃüÁî
Ctrl-R °´ÐжÁÈ¡µÄ·´ÏòÀúÊ·ËÑË÷£¨²¿·ÖÆ¥Å䣩
Ctrl-Shift-v ´Ó¼ôÌù°åÕ³ÌùÎı¾
Ctrl-C ÖÐÖ¹µ±Ç°ÕýÔÚÖ´ÐеĴúÂë
Ctrl-A ½«¹â±êÒÆ¶¯µ½ÐÐÊ×
Ctrl-E ½«¹â±êÒÆ¶¯µ½ÐÐβ
Ctrl-K ɾ³ý´Ó¹â±ê¿ªÊ¼ÖÁÐÐβµÄÎı¾
Ctrl-U Çå³ýµ±Ç°ÐеÄËùÓÐÎı¾Òë×¢12
Ctrl-F ½«¹â±êÏòÇ°ÒÆ¶¯Ò»¸ö×Ö·û
Ctrl-b ½«¹â±êÏòºóÒÆ¶¯Ò»¸ö×Ö·û
Ctrl-L ÇåÆÁ
°Ë.Òì³£ºÍ¸ú×Ù
¾Å.ħÊõÃüÁî
Õ³ÌùÔÚ´ËÒÔ×÷±¸Íü
ÃüÁî ˵Ã÷
%quickref ÏÔʾIPythonµÄ¿ìËٲο¼
%magic ÏÔʾËùÓÐħÊõÃüÁîµÄÏêϸÎĵµ
%debug ´Ó×îеÄÒì³£¸ú×ٵĵײ¿½øÈë½»»¥Ê½µ÷ÊÔÆ÷
%hist ´òÓ¡ÃüÁîµÄÊäÈ루¿ÉÑ¡Êä³ö£©ÀúÊ·
%pdb ÔÚÒì³£·¢Éúºó×Ô¶¯½øÈëµ÷ÊÔÆ÷
%paste Ö´ÐмôÌù°åÖеÄPython´úÂë
%cpaste ´ò¿ªÒ»¸öÌØÊâÌáʾ·ûÒÔ±ãÊÖ¹¤Õ³Ìù´ýÖ´ÐеÄPython´úÂë
%reset ɾ³ýinteractiveÃüÃû¿Õ¼äÖеÄÈ«²¿±äÁ¿/Ãû³Æ
%page OBJECT ͨ¹ý·ÖÒ³Æ÷´òÓ¡Êä³öOBJECT
%run script.py ÔÚIPythonÖÐÖ´ÐÐÒ»¸öPython½Å±¾Îļþ
%prun statement ͨ¹ýcProfileÖ´ÐÐstatement£¬²¢´òÓ¡·ÖÎöÆ÷µÄÊä³ö½á¹û
%time statement ±¨¸æstatementµÄÖ´ÐÐʱ¼ä
%timeit statement ¶à´ÎÖ´ÐÐstatementÒÔ¼ÆËãϵ×ÛÆ½¾ùÖ´ÐÐʱ¼ä¡£¶ÔÄÇЩִÐÐʱ
¼ä·Ç³£Ð¡µÄ´úÂëºÜÓÐÓÃ
%who¡¢%who_ls¡¢%whos ÏÔʾinteractiveÃüÃû¿Õ¼äÖж¨ÒåµÄ±äÁ¿£¬ÐÅÏ¢¼¶±ð/ÈßÓà¶È¿É±ä
%xdel variable ɾ³ývariable£¬²¢³¢ÊÔÇå³ýÆäÔÚIPythonÖеĶÔÏóÉϵÄÒ»ÇÐÒýÓÃ
Ê®.»ùÓÚQtµÄ¸»GUI¿ØÖÆÌ¨
IpythonÍŶӿª·¢ÁËÒ»¸ö»ùÓÚQt¿ò¼Ü£¨ÆäÄ¿µÄÊÇΪÖÕ¶ËÓ¦ÓóÌÐòÌṩÖîÈçÄÚǶͼƬ¡¢¶àÐб༡¢Óï·¨¸ßÁÁÖ®ÀàµÄ¸»Îı¾¼ô¼¹¦ÄÜ£©µÄGUI¿ØÖÆÌ¨¡£
ʹÓÃ
ipython qtconsole --pylab=inline |
À´Æô¶¯µÄ»°¿ÉΪÆäÌí¼Ó»æÍ¼¹¦ÄÜ¡£
ps.ÕâÒ»²½¸Õ¿ªÊ¼ÎÞ·¨Ö´ÐУ¬ÎҵĽâ¾ö·½·¨ÊÇ£º
pip install qtconsole£¬¾Í¿ÉÒÔÔËÐÐÁË- -..
ʮһ.matplotlib¼¯³ÉÓëpylabģʽ
ͨ³£ÎÒÃÇͨ¹ýÔÚÆô¶¯Ipythonʱ¼ÓÉÏ--pylab±ê¼ÇÀ´¼¯³Ématplotlib
×¢Òâ¿Õ¸ñ°¡~ÊÇ
ipython --pylab
Ê®¶þ.ʹÓÃÀúÊ·ÃüÁî
Ê®Èý.ËÑË÷²¢ÖØÓÃÀúÊ·ÃüÁî
ÀúÊ·ÃüÁîÓÃÉÏϼýÍ·¾ÍºÃÀ²£¬ctrl+pºÍctrl+n Ì«Âé·³À²¡£
ctrl+rÓÃÓÚʵÏÖ²¿·ÖÔöÁ¿ËÑË÷£¬°´ÏÂctrl+r²¢ÊäÈëÄãÏëËÑË÷µÄÐÐÖеö×Ö·û¡£°´ÏÂctrl+r½«»áÑ»·ËÑË÷ÀúÊ·ÃüÁîÖÐÿһÌõÓëÊäÈëÏà·ûµÄÐС£
Ê®ËÄ.ÊäÈëºÍÊä³ö±äÁ¿
ipython½«×î½üµÄÁ½¸öÊä³ö½á¹û±£´æÔÚ_(Ò»¸öÏ»®Ïß)ºÍ__(Á½¸öÏ»®Ïß)±äÁ¿ÖÐ


ÊäÈëµÄÎı¾±»±£´æÔÚÃûΪ_iXµÄ±äÁ¿ÖУ¬ÆäÖÐXÊÇÊäÈëÐеÄÐкš£±ÈÈç˵£¬ÔÚÊäÈëÍê27Ðк󣬾ͻá²úÉúÁ½¸öбäÁ¿_27(Êä³ö±äÁ¿)ºÍ_i27(ÊäÈë±äÁ¿)
ÎÒÕâ¶ù¾ÍÄõڰËÐоÙÀý×Ó°É2333

Ê®Îå.¼Ç¼ÊäÈëºÍÊä³ö
Ö´ÐÐ%logstart¼È¿É¿ªÊ¼¼Ç¼ÈÕÖ¾

Ê®Áù.Óë²Ù×÷ϵͳ½»»¥


Ê®Æß.shellÃüÁîºÍ±ðÃû£¨Õâ¶ùÎҺöà´úÂëÖ´Ðв»³öÀ´£¬´ý½â¾ö¡££©
ÔÚipythonÖУ¬ÒÔ¸Ð̾ºÅ(!)¿ªÍ·µÄÃüÁîÐбíʾÆäºóµÄËùÓÐÄÚÈÝÐèÒªÔÚϵͳshellÖÐÖ´ÐС£
##ÔÙÆô¶¯Ò»¸öpython½âÊÍÆ÷

ÔÚʹÓã¡Ê±£¬ipython»¹ÔÊÐíʹÓõ±Ç°»·¾³Öж¨ÒåµÄpythonÖµ¡£Ö»ÐèÒªÔÚ±äÁ¿ÃûÇ°Ãæ¼ÓÉÏ$¼´¿É
ħÊõÃüÁî%alias¿ÉÒÔΪshellÃüÁî×Ô¶¨Òå¼ò³Æ¡£

ÕâÊÇÔõô»ØÊ¶ù~£¡
Ê®°Ë.Ŀ¼Êéǩϵͳ
ipythonÓÐÒ»¸ö¼òµ¥µÄĿ¼Êéǩϵͳ£¬ËüʹÄãÄܱ£´æ³£ÓÃĿ¼µÄ±ðÃûÒÔʵÏÖ¿ìËÙÌø×ª¡£
£¨Õâ¶ùÎÒµçÄÔͬÑùÖ´Ðв»³öÀ´£¬´ý½â¾ö¡££©
Ê®¾Å.Èí¼þ¿ª·¢¹¤¾ß
(Ò»)½»»¥Ê½µ÷ÊÔÆ÷
µ÷ÊÔ´úÂëµÄ×î¼ÑʱÆÚÖ®Ò»¾ÍÊÇ´íÎó¸Õ¸Õ·¢ÉúÄÇ»á¶ù¡£
%debugÃüÁÔÚ·¢ÉúÒì³£Ö®ºóÂíÉÏÊäÈ룩½«»áµ÷ÓÃÄĸö¡°Êºóµ÷ÊÔÆ÷¡±£¬²¢Ö±½ÓÌø×ªµ½Òý·¢Òì³£µÄÄǸöÕ»Ö¡£¨stack
frame£©
Õâ¶ùÐèÒª´ò¿ªipython_bug.pyÎļþ£¬ÐèÒªµÄͬѧ¿ÉÒÔÎÊÎÒÒª£¬Â·¾¶»¹ÐèÒª×Ô¼º¸ÄÒ»¸Ä¡£

ÔÚÕâ¸öµ÷ÊÔÆ÷ÖУ¬¿ÉÒÔÖ´ÐÐÈÎÒâpython´úÂë²¢²é¿´¸÷¸öÕ»Ö¡ÖеÄÒ»ÇжÔÏóºÍÊý¾Ý£¨Ò²¾ÍÊǽâÊÍ»¹¡°ÁôÁËÌõÉú·¡±µÄÄÇЩ£©¡£
ĬÈÏ´Ó×îµÍ¼¶¿ªÊ¼µÄ£¨¼´´íÎó·¢ÉúµÄµØ·½£©¡£
ÊäÈëu£¨up£©ºÍd£¨down£©¼´¿ÉÔÚÕ»¸ú×ٵĸö¼¶±ðÖ®¼äµÄÇл»

Ö´ÐÐ%PdbÃüÁî¿ÉÒÔÈÃipythonÔÚ³öÏÖÒì³£Ö®ºó×Ô¶¯µ÷Óõ÷ÊÔÆ÷¡£
´ËÍ⣬µ÷ÊÔÆ÷»¹¿ÉÒÔΪ´úÂ뿪·¢¹¤×÷Ìṩ°ïÖú£¬ÓÈÆäÊǵ±ÄãÏëÒªÉèÖöϵã»ò¶Ôº¯Êý¡¢½Å±¾½øÐе¥²½µ÷ÊÔÒԲ鿴¸÷ÌõÓï¾äµÄÖ´ÐÐÇé¿öʱ¡£
ʵÏÖÕâ¸öÄ¿µÄµÄ·½Ê½Óм¸¸ö£º
*********************´ýÍêÉÆ
(¶þ)µ÷ÊÔÆ÷µÄÆäËüʹÓó¡¾°
*********************´ýÍêÉÆ
(Èý)²âÊÔ´úÂëµÄÖ´ÐÐʱ¼ä£º%timeºÍ%timeit
ÓÐʱºòÄã¿ÉÄÜ»áÏ£ÍûÁ˽âij¸ö¸´ÔÓ¼ÆËã¹ý³ÌÖе½µ×ÊÇÄÄЩº¯ÊýÕ¼ÓõÄʱ¼ä×î¶à¡£
ipythonרÃÅÌṩÁËÁ½¸öħÊõº¯Êý£¨%timeºÍ%timeit£©ÒÔ±ã×Ô¶¯Íê³É¸Ã¹ý³Ì¡£
%timeÒ»´ÎÖ´ÐÐÒ»ÌõÓï¾ä£¬È»ºó±¨¸æ×ÜÌåÖ´ÐÐʱ¼ä¡£
Àý×Ó£ºÏÂÃæÊÇÒ»¸öÓµÓÐ60Íò×Ö·û´®µÄÊý×飬ÒÔ¼°Á½¸ö²»Í¬µÄ¡°Äܹ»Ñ¡³öÆäÖÐÒÔfoo¿ªÍ·µÄ×Ö·û´®¡±µÄ·½·¨£º
#Ò»¸ö·Ç³£´óµÄ×Ö·û´®Êý×é
strings = ['foo','foobar','baz','qux','python','Guido
Van Rossum']*100000
method1 = [x for x in strings if x.startswith('foo')]
mathod2 = [x for x in strings if x[:3] == 'foo'] |
ͨ¹ý%timeÀ´²âÊÔ£º

À²À²À² ²î¾à³öÀ´ÁË¡£
µ«ÊÇÕâ²¢²»ÊÇÒ»¸ö·Ç³£¾«È·µÄ½á¹û£¬Èç¹ûÄã¶ÔÏàͬµÄÓï¾ä¶à´ÎÖ´ÐÐ%timeµÄ»°£¬¾Í»á·¢ÏÖÆä½á¹ûÊÇ»á±äµÄ¡££¨ÇײâÕæµÄ»á±ä233¿´ÏÂͼ£©

ΪÁ˵õ½¸üΪ¾«È·µÄ½á¹û£¬ÐèҪʹÓÃħÊõº¯Êý%timeit£¬¶ÔÓÚÈÎÒâÓï¾ä£¬Ëü»á×Ô¶¯¶à´ÎÖ´ÐÐÒÔ²úÉúÒ»¸ö·Ç³£¾«È·µÄƽ¾ùÖ´ÐÐʱ¼ä¡£

(ËÄ)»ù±¾ÐÔÄÜ·ÖÎö£º%prunºÍ%run -p
(Îå)ÖðÐзÖÎöº¯ÊýÐÔÄÜ
¶þÊ®.Ipython HTML Notebook
IPython NotebookʹÓÃä¯ÀÀÆ÷×÷Ϊ½çÃæ£¬Ïòºǫ́µÄIPython·þÎñÆ÷·¢ËÍÇëÇ󣬲¢ÏÔʾ½á¹û¡£ÔÚä¯ÀÀÆ÷µÄ½çÃæÖÐʹÓõ¥Ôª(Cell)±£´æ¸÷ÖÖÐÅÏ¢¡£CellÓжàÖÖÀàÐÍ£¬¾³£Ê¹ÓõÄÓбíʾ¸ñʽ»¯Îı¾µÄMarkdownµ¥Ôª£¬ºÍ±íʾ´úÂëµÄCodeµ¥Ôª¡£
ÿ¸ö´úÂëµ¥Ôª¶¼ÓÐÒ»¸öÊä³öÇøÓò£¬ÔÚCodeµ¥ÔªÖÐÊäÈë´úÂ룬°´ Shift-Enter ½«ÔËÐд˴úÂ룬´úÂëÖÐ×îºóÒ»¸ö±í´ïʽµÄÖµ½«Êä³öÇøÓòÏÔʾ¡£Èç¹ûÏ£ÍûÆÁ±ÎÊä³ö£¬¿ÉÒÔÔÚ×îºóÒ»ÌõÓï¾äÖ®ºóÌí¼ÓÒ»¸ö·ÖºÅ£º¡±;¡±¡£´ËÍ⣬´úÂëÖл¹¿ÉÒÔʹÓÃ
printÓï¾äÔÚÊä³öÇøÓòÖÐÏÔʾÐÅÏ¢¡£
ÔÚMarkdownµ¥ÔªÖл¹¿ÉÒÔÖ±½ÓʹÓÃHtmlºÍJavascript¡£
***********ÎҵĵçÄÔ½ø²»È¥£¡£¡´ý½â¾ö
¶þʮһ.ÀûÓÃipythonÌá¸ß´úÂ뿪·¢Ð§ÂʵãÌáʾ
(Ò»)ÖØÐ¼ÓÔØÄ£¿éÒÀÀµÏî
(¶þ)´úÂëÉè¼ÆÌáʾ
1.±£ÁôÓÐÒâÒåµÄ¶ÔÏóºÍÊý¾Ý
2.±âƽ½á¹¹Òª±ÈǶÌ׽ṹºÃ
3.ÎÞ¾å´óÎļþ
¶þÊ®¶þ.¸ß¼¶ipython¹¦ÄÜ
(Ò»)ÈÃÄãµÄÀà¶Ôipython¸ü¼ÓÓѺÃ
(¶þ)¸öÐÔ»¯ºÍÅäÖà |