Python基础学习之IPython工具的使用

105 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

Py基础学习-IPython工具

Ipython工具主要有三大重要组成:

1、增强的交互式Python外壳

2、数据科学社区的Jupyter内核,提供了web笔记本Jupyter Notebook

3、支持交互式数据可视化和GUI工具,拥有高性能交互式并行计算的架构

Ipython中有两种魔法命令,一种是line magics,另外一种是cell magics。%表示line magics,即只在本行有效;%%表示cell magics,表示在整个cell单元下有效

  • 进入ipython工具
ipython
  • 显示当前工作路径

使用pwd显示当前工作路径

%pwd
  • 新建文件夹

使用mkdir指令,新建一个名为test的文件夹

%mkdir test
  • 切换路径

使用cd指令,切换当前工作路径为./test

%cd test/
  • 新建文件并写入内容

print ('hello world')写入文件,文件名为hello_world.py

In [9]: %%writefile hello_world.py                                                              ...: print('hello world')                                                                    ...:                                                                                        ...:                                                                                     Writing hell_world.py 
  • 运行文件

使用run指令运行hello_world.py文件,运行输入"hello world"

%run hello_world.py
  • 查看当前路径下的所有文件信息

使用ls指令查看当前目录下的所有文件

%ls
  • 删除某个文件

使用os模块下的remove方法对文件进行删除操作

import os
os.remove('hello_world.py')
  • 删除某个空目录

删除当前目录下名为test的目录

%rmdir test
  • 查看历史用过的所有指令

使用hist或者history命令都可以查看历史输入

%hist
  • 查看某个函数的帮助信息和源代码

查看sum函数的帮助信息

sum?

查看sort函数的帮助信息和源代码

%pylab
sort??
  • 使用上一个cell的输出结果

上一个cell的输出即表示前面的输出

In[1]: a = 111
In[2]: print(a)
Out[1]: 111
In[3]: _ + 1
Out[2]: 112
  • 执行某个系统指令

执行ping命令

!ping baidu.com
  • 使用Jupyter notebook工具

退出Ipython编辑器

In[1]: exit

进入Jupyter notebook工具

ipython notebook

浏览器访问http://localhost:8888/tree

Jupyter界面截图 | 原创图源

支持jupyter的指令输入,可以在cell中使用pip下载库指令

pip list|findstr os

image.png

cmd截图 | 原创图源

支持linux等命令操作

%wget https://arxiv.org/abs/1907.01591