Python

1- IPython简介

IPython(interactive Python) provides a rich architecture for interactive computing with:
  •     A powerful interactive shell.
  •     A kernel for Jupyter.
  •     Support for interactive data visualization and use of GUI toolkits.
  •     Flexible, embeddable interpreters to load into your own projects.
  •     Easy to use, high performance tools for parallel computing.
 

2- 安装IPython

  • 利用pip安装:“pip install ipython”;
  • 在命令行下执行“ipython”命令启动交互式shell;
  • 使用“In[1]、out[1]” 作为输入输出行的序号,按下回车立刻显示相应的结果;
注意:在同时安装了Python2和Python3的环境中,可以尝试“ipython”或“ipython3”来分别启动不同Python版本下的IPython;
 
>ipython
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (In
tel)]
Type "copyright", "credits" or "license" for more information.

IPython 5.5.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: str = "hello world"

In [2]: str
Out[2]: 'hello world'

In [3]: exit

3- 使用IPython

  • 帮助信息:“ipython -h”或“ipython --help”
  • 异常追踪:执行出现异常时,IPython 默认会打印整个调用栈跟踪,并显示发生异常的附近的几行代码作为上下文参考;
  • 命令补全:Tab键,显示匹配的变量(对象、函数等);
  • 对象内省:
    • 在变量前面或后面加上一个问号(?),显示有关该对象的一些通用信息;
    • 如果对象是一个函数或实例方法,则会显示其docstring;使用双问号(??)显示该函数的源代码;
    • 可以使用通配符(*)显示匹配的字符串;
In [18]: str = "this is a test"

In [19]: str
Out[19]: 'this is a test'

In [20]: str?
Type:        str
String form: this is a test
Length:      14
Docstring:
str(object='') -> string

Return a nice string representation of the object.
If the argument is a string, the return value is the same object.

In [21]: 

4- 魔术命令

以百分号(%)为前缀的特殊命令,可以提供很多实用便利的功能(若命令不与变量名冲突,百分号可省略);
# 常用魔术命令
%quickref    显示 IPython 的快速参考
%magic    显示所有魔术命令详细文档
%debug    从最新的异常跟踪的底部进入交互式解释器
%hist    打印命令的输入历史
%pdb    在异常发生后自动进入调试器
%paste    执行剪贴板中的 Python 代码
%cpaste    打开一个特殊提示符,便于手动粘贴待执行的 Python代码
%reset    删除命令空间中的全部变量
%run script.py    执行一个 Python 脚本
%time statement    报告 statement 的执行时间
%timeit statement    多次执行 statement 以计算平均执行时间
%who、%who_ls、%whos    显示命令空间中定义的变量,三种不同信息级别
%xdel variable    删除 variable,并尝试清除其在 IPython 中的对象上的一切引用

# 显示帮助信息
%magic    浏览全部魔术命令的详细文档
%<command>?    查看指定魔术命令的详细用法

# 代码测试
%time    检测任意 Python 语句的执行时间
%run    可以在一个空的命令空间(无法访问IPython 中导入的包)中运行脚本,Ctrl+C停止脚本

# 复制粘贴
%paste    将剪切板中的文本粘贴交互命令行整体执行
%cpaste    复制粘贴或输入代码到交互命令行整体执行,输入结束符 ‘--’ 或Ctrl+D结束粘贴,建议使用,因为出错的可能性更小

示例:检测任意 Python 语句的执行时间

In [1]: %time print "hello world"
hello world
Wall time: 0 ns

In [2]: %time print 'hello world'
hello world
Wall time: 1e+03 µs

In [3]:

示例:复制粘贴

In [1]: %paste
def test(a, b):
        return a+b
a = 1
b = 2
ans = test(a, b)

## -- End pasted text --

In [2]: test
Out[2]: <function __main__.test>

In [3]: test(222,333)
Out[3]: 555

In [4]:

In [4]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:def hi():
:       print 'hi'
:ha = 'haha'
:--

In [5]: hi
Out[5]: <function __main__.hi>

In [6]: hi()
hi

In [7]: ha
Out[7]: 'haha'

In [8]:

5- 常用快捷键

Ctrl-P 或上箭头键 后向搜索命令历史中以当前输入的文本开头的命令
Ctrl-N 或下箭头键 前向搜索命令历史中以当前输入的文本开头的命令
Ctrl-R 按行读取的反向历史搜索(部分匹配)

Ctrl-Shift-v 从剪贴板粘贴文本
Ctrl-C 中止当前正在执行的代码
Ctrl-D 退出IPython会话

Ctrl-A 将光标移动到行首
Ctrl-E 将光标移动到行尾
Ctrl-K 删除从光标开始至行尾的文本
Ctrl-U 清除当前行的所有文本译注

Ctrl-L 清屏

  

 
原文地址:https://www.cnblogs.com/anliven/p/10004781.html