Python基础学习之Ipython解释器
进入ipython通常我们并不使用Python自带的解释器而是使用另一个比较方便的解释器——ipython解释器命令行下输入ipython即可进入ipython解释器。所有在python解释器下可以运行的代码都可以在ipython解释器下运行printhello, worldhello, world可以进行简单赋值操作a1直接在解释器中输入变量名会显示变量的值不需要加printa1b[1,2,3]ipython magic命令ipython解释器提供了很多以百分号%开头的magic命令这些命令很像linux系统下的命令行命令事实上有些是一样的。查看所有的magic命令%lsmagicAvailable 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以一个百分号开头作用与一行cell magic以两个百分号开头作用于整个cell。最后一行Automagic is ON, % prefix IS NOT needed for line magics.说明在此时即使不加上%也可以使用这些命令。使用whos查看当前的变量空间%whosVariable Type Data/Info ---------------------------- a int 1 b list n3使用reset重置当前变量空间%reset-f再查看当前变量空间%whosInteractive namespace is empty.使用pwd查看当前工作文件夹%pwduC:\\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.pyprinthello worldWriting 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.pyhello world删除这个文件importos 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查看历史命令%histprint 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 %histipython 使用使用?查看函数的帮助sum?使用??查看函数帮助和函数源代码如果是用python实现的# 导入numpy和matplotlib两个包%pylab# 查看其中sort函数的帮助sort??Using matplotlib backend: Qt4Agg Populating the interactive namespace from numpy and matplotlibipython支持使用tab键自动补全命令。使用_使用上个cell的输出结果a12a12_1325可以使用!来执行一些系统命令。!ping baidu.com正在 Ping baidu.com [180.149.132.47] 具有 32 字节的数据: 来自 180.149.132.47 的回复: 字节32 时间69ms TTL49 来自 180.149.132.47 的回复: 字节32 时间64ms TTL49 来自 180.149.132.47 的回复: 字节32 时间61ms TTL49 来自 180.149.132.47 的回复: 字节32 时间63ms TTL49 180.149.132.47 的 Ping 统计信息: 数据包: 已发送 4已接收 4丢失 0 (0% 丢失) 往返行程的估计时间(以毫秒为单位): 最短 61ms最长 69ms平均 64ms当输入出现错误时ipython会指出出错的位置和原因1hello--------------------------------------------------------------------------- TypeError Traceback (most recent call last) ipython-input-25-d37bedb9732a in module() ---- 1 1 hello TypeError: unsupported operand type(s) for : int and str