【云计算】Ipython解释器到底是什么
小标 2019-02-25 来源 : 阅读 1865 评论 0

摘要:本文主要向大家介绍了【云计算】Ipython解释器到底是什么,通过具体的内容向大家展现,希望对大家学习云计算有所帮助。

本文主要向大家介绍了【云计算】Ipython解释器到底是什么,通过具体的内容向大家展现,希望对大家学习云计算有所帮助。

【云计算】Ipython解释器到底是什么

进入ipython


通常我们并不使用Python自带的解释器,而是使用另一个比较方便的解释器——ipython解释器,命令行下输入:


ipython


即可进入ipython解释器。


所有在python解释器下可以运行的代码都可以在ipython解释器下运行:


In[1]:


print "hello, world"


hello, world


可以进行简单赋值操作:


In[2]:


a = 1


直接在解释器中输入变量名,会显示变量的值(不需要加print):


In[3]:


a


Out[3]:


1


In[4]:


b = [1, 2, 3]


ipython magic命令


ipython解释器提供了很多以百分号%开头的magic命令,这些命令很像linux系统下的命令行命令(事实上有些是一样的)。


查看所有的magic命令:


In[5]:


%lsmagic


Out[5]:


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 以一个百分号开头,作用与一行;


cell magic 以两个百分号开头,作用于整个cell。


最后一行Automagic is ON, % prefix IS NOT needed for line magics.说明在此时即使不加上%也可以使用这些命令。


使用 whos 查看当前的变量空间:


In[6]:


%whos


Variable   Type    Data/Info
----------------------------
a          int     1
b          list    n=3


使用 reset 重置当前变量空间:


In[7]:


%reset -f


再查看当前变量空间:


In[8]:


%whos


Interactive namespace is empty.


使用 pwd 查看当前工作文件夹:


In[9]:


%pwd


Out[9]:


u'C:\\Users\\lijin\\Documents\\Git\\python-tutorial\\01. python tools'


使用 mkdir 产生新文件夹:


In[10]:


%mkdir demo_test


使用 cd 改变工作文件夹:


In[11]:


%cd demo_test/


C:\Users\lijin\Documents\Git\python-tutorial\01. python tools\demo_test


使用 writefile 将cell中的内容写入文件:


In[12]:


%%writefile hello_world.py
print "hello world"


Writing hello_world.py


使用 ls 查看当前工作文件夹的文件:


In[13]:


%ls


 驱动器 C 中的卷是 System
 卷的序列号是 DC4B-D785

 C:\Users\lijin\Documents\Git\python-tutorial\01. python tools\demo_test 的目录

2015/09/18  11:32



 . 2015/09/18 11:32



 .. 2015/09/18 11:32 19 hello_world.py 1 个文件 19 字节 2 个目录 121,763,831,808 可用字节


使用 run 命令来运行这个代码:


In[14]:


%run hello_world.py


hello world


删除这个文件:


In[15]:


import os
os.remove('hello_world.py')


查看当前文件夹,hello_world.py 已被删除:


In[16]:


%ls


 驱动器 C 中的卷是 System
 卷的序列号是 DC4B-D785

 C:\Users\lijin\Documents\Git\python-tutorial\01. python tools\demo_test 的目录

2015/09/18  11:32



 . 2015/09/18 11:32



 .. 0 个文件 0 字节 2 个目录 121,763,831,808 可用字节


返回上一层文件夹:


In[17]:


%cd ..


C:\Users\lijin\Documents\Git\python-tutorial\01. python tools


使用 rmdir 删除文件夹:


In[18]:


%rmdir demo_test


使用 hist 查看历史命令:


In[19]:


%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


ipython 使用


使用 查看函数的帮助:


In[20]:


sum


使用 查看函数帮助和函数源代码(如果是用python实现的):


In[21]:


# 导入numpy和matplotlib两个包
%pylab
# 查看其中sort函数的帮助
sort


Using matplotlib backend: Qt4Agg
Populating the interactive namespace from numpy and matplotlib


ipython 支持使用  键自动补全命令。


使用 _ 使用上个cell的输出结果:


In[22]:


a = 12
a


Out[22]:


12


In[23]:


_ + 13


Out[23]:


25


可以使用 ! 来执行一些系统命令。


In[24]:


!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会指出出错的位置和原因:


In[25]:


1 + "hello"


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)


          

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标大数据云计算大数据安全频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved