一般咱們並不使用Python自帶的解釋器,而是使用另外一個比較方便的解釋器——ipython解釋器,命令行下輸入:javascript
ipython
便可進入ipython解釋器。html
全部在python解釋器下能夠運行的代碼均可以在ipython解釋器下運行:java
print "hello, world"
hello, world
能夠進行簡單賦值操做:python
a = 1
直接在解釋器中輸入變量名,會顯示變量的值(不須要加print
):linux
a
1
b = [1, 2, 3]
ipython解釋器提供了不少以百分號%
開頭的magic
命令,這些命令很像linux系統下的命令行命令(事實上有些是同樣的)。ruby
查看全部的magic
命令:bash
%lsmagic
Available line magics: %alias %alias_magic %autocall %automagic %autosave %bookmark %cd %clear %cls %colors %config %connect_info %copy %ddir %debug %dhist %dirs %doctest_mode %echo %ed %edit %env %gui %hist %history %install_default_config %install_ext %install_profiles %killbgscripts %ldir %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %macro %magic %matplotlib %mkdir %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %ren %rep %rerun %reset %reset_selective %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode Available cell magics: %%! %%HTML %%SVG %%bash %%capture %%cmd %%debug %%file %%html %%javascript %%latex %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile Automagic is ON, % prefix IS NOT needed for line magics.
line magic
以一個百分號開頭,做用與一行;less
cell magic
以兩個百分號開頭,做用於整個cell。svg
最後一行Automagic is ON, % prefix IS NOT needed for line magics.
說明在此時即便不加上%
也能夠使用這些命令。函數
使用 whos
查看當前的變量空間:
%whos
Variable Type Data/Info ---------------------------- a int 1 b list n=3
使用 reset
重置當前變量空間:
%reset -f
再查看當前變量空間:
%whos
Interactive namespace is empty.
使用 pwd
查看當前工做文件夾:
%pwd
u'C:\\Users\\lijin\\Documents\\Git\\python-tutorial\\01. python tools'
使用 mkdir
產生新文件夾:
%mkdir demo_test
使用 cd
改變工做文件夾:
%cd demo_test/
C:\Users\lijin\Documents\Git\python-tutorial\01. python tools\demo_test
使用 writefile
將cell中的內容寫入文件:
%%writefile hello_world.py print "hello world"
Writing hello_world.py
使用 ls
查看當前工做文件夾的文件:
%ls
驅動器 C 中的卷是 System 卷的序列號是 DC4B-D785 C:\Users\lijin\Documents\Git\python-tutorial\01. python tools\demo_test 的目錄 2015/09/18 11:32 <DIR> . 2015/09/18 11:32 <DIR> .. 2015/09/18 11:32 19 hello_world.py 1 個文件 19 字節 2 個目錄 121,763,831,808 可用字節
使用 run
命令來運行這個代碼:
%run hello_world.py
hello world
刪除這個文件:
import os os.remove('hello_world.py')
查看當前文件夾,hello_world.py
已被刪除:
%ls
驅動器 C 中的卷是 System 卷的序列號是 DC4B-D785 C:\Users\lijin\Documents\Git\python-tutorial\01. python tools\demo_test 的目錄 2015/09/18 11:32 <DIR> . 2015/09/18 11:32 <DIR> .. 0 個文件 0 字節 2 個目錄 121,763,831,808 可用字節
返回上一層文件夾:
%cd ..
C:\Users\lijin\Documents\Git\python-tutorial\01. python tools
使用 rmdir
刪除文件夾:
%rmdir demo_test
使用 hist
查看歷史命令:
%hist
print "hello, world" a = 1 a b = [1, 2, 3] %lsmagic %whos %reset -f %whos %pwd %mkdir demo_test %cd demo_test/ %%writefile hello_world.py print "hello world" %ls %run hello_world.py import os os.remove('hello_world.py') %ls %cd .. %rmdir demo_test %hist
使用 ?
查看函數的幫助:
sum?
使用 ??
查看函數幫助和函數源代碼(若是是用python實現的):
# 導入numpy和matplotlib兩個包 %pylab # 查看其中sort函數的幫助 sort??
Using matplotlib backend: Qt4Agg Populating the interactive namespace from numpy and matplotlib
ipython 支持使用 <tab>
鍵自動補全命令。
使用 _
使用上個cell的輸出結果:
a = 12 a
12
_ + 13
25
能夠使用 !
來執行一些系統命令。
!ping baidu.com
正在 Ping baidu.com [180.149.132.47] 具備 32 字節的數據: 來自 180.149.132.47 的回覆: 字節=32 時間=69ms TTL=49 來自 180.149.132.47 的回覆: 字節=32 時間=64ms TTL=49 來自 180.149.132.47 的回覆: 字節=32 時間=61ms TTL=49 來自 180.149.132.47 的回覆: 字節=32 時間=63ms TTL=49 180.149.132.47 的 Ping 統計信息: 數據包: 已發送 = 4,已接收 = 4,丟失 = 0 (0% 丟失), 往返行程的估計時間(以毫秒爲單位): 最短 = 61ms,最長 = 69ms,平均 = 64ms
當輸入出現錯誤時,ipython會指出出錯的位置和緣由:
1 + "hello"
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-25-d37bedb9732a> in <module>() ----> 1 1 + "hello" TypeError: unsupported operand type(s) for +: 'int' and 'str'