ipython学习笔记

2019-12-20  本文已影响0人  CodeCompiler

ipython常用的命令

len?  ##问号进行帮助的查询
def square(a):
    """return the square of a."""
    return a ** 2
    
## 以上定义一个函数

## ?? 两个问号可以进行查询源代码
square??

## <TAB> 可以进行一个补全

# 通配符号 可以通过 * 来表示
*find*

colab的操作按键可以通过剪藏来进行保存

ipython的快捷键操作

可以Google一下 ipython cheat sheet

ipython的魔法操作

粘贴格子 相关快捷键

%paste %cpaste

运行外部文件 (python)

%run

分析代码的运行时间

%timeit 后面跟随命令 统计时间

帮助的命令

? , %magic, %lsmagic


ipython IN 和 OUT 标号的意义

ipython与命令行之间的关系

osx:projects $ pwd
/home/jake/projects
 
osx:projects $ ls
datasci_book   mpld3   myproject.txt
 
osx:projects $ mkdir myproject          # mkdir = make new directory
 
osx:projects $ cd myproject/
 
osx:myproject $ mv ../myproject.txt ./  # mv = move file. Here we're moving the
                                        # file myproject.txt from one directory
                                        # up (../) to the current directory (./)
osx:myproject $ ls
myproject.txt

以上是shell 下面是ipython的实例 通过加上 !(感叹号)* 可以进行命令行命令的执行

In [1]: !ls
myproject.txt
 
In [2]: !pwd
/home/jake/projects/myproject
 
In [3]: !echo "printing from the shell"
printing from the shell
In [4]: contents = !ls
 
In [5]: print(contents)
['myproject.txt']
 
In [6]: directory = !pwd
 
In [7]: print(directory)
['/Users/jakevdp/notebooks/tmp/myproject']

In [8]: type(directory)
IPython.utils.text.SList
In [11]: !pwd
/home/jake/projects/myproject
 
In [12]: !cd ..
 
In [13]: !pwd
/home/jake/projects/myproject

In [14]: %cd ..
/home/jake/projects

In [15]: cd myproject
/home/jake/projects/myproject
In [9]: message = "hello from Python"
 
In [10]: !echo {message}
hello from Python

ipython的automagic函数


ipython 进行除错

Partial list of debugging commands

There are many more available commands for interactive debugging than we've listed here; the following table contains a description of some of the more common and useful ones:

Command Description
list Show the current location in the file
h(elp) Show a list of commands, or find help on a specific command
q(uit) Quit the debugger and the program
c(ontinue) Quit the debugger, continue in the program
n(ext) Go to the next step of the program
<enter> Repeat the previous command
p(rint) Print variables
s(tep) Step into a subroutine
r(eturn) Return out of a subroutine

计算时间

pip install line_profiler

%load_ext line_profiler
%lprun -f ....

ipython的深入学习

Web Resources

Books

Finally, a reminder that you can find help on your own: IPython's ?-based help functionality (discussed in Help and Documentation in IPython) can be very useful if you use it well and use it often.
As you go through the examples here and elsewhere, this can be used to familiarize yourself with all the tools that IPython has to offer.

上一篇 下一篇

猜你喜欢

热点阅读