连仕彤博客[Python笔记] IPython使用技巧

时间:2022-05-11
本文章向大家介绍连仕彤博客[Python笔记] IPython使用技巧,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

帮助

?:IPython的概述和简介

 
In [1]: ? 
 
IPython -- An enhanced Interactive Python
=========================================
 
IPython offers a fully compatible replacement for the standard Python
interpreter, with convenient shell features, special commands, command
history mechanism and output results caching.
 
At your system command line, type 'ipython -h' to see the command line
options available. This document only describes interactive features.
 
GETTING HELP
------------
 
Within IPython you have various way to access help:
 
  ?         -> Introduction and overview of IPython's features (this screen).
  object?   -> Details about 'object'.
  object??  -> More detailed, verbose information about 'object'.
  %quickref -> Quick reference of all IPython specific syntax and magics.
  help      -> Access Python's own help system.
 
If you are in terminal IPython you can quit this screen by pressing `q`.
 
 
MAIN FEATURES
-------------
 
* Access to the standard Python help with object docstrings and the Python
  manuals. Simply type 'help' (no quotes) to invoke it.
 
* Magic commands: type %magic for information on the magic subsystem.
 
* System command aliases, via the %alias command or the configuration file(s).
 
* Dynamic object information:
 
  Typing ?word or word? prints detailed information about an object. Certain

helo(name):查询指定名称的帮助

 
In [3]: import os
 
In [4]: help(os)
Help on module os:
 
NAME
    os - OS routines for NT or Posix depending on what system we're on.
 
MODULE REFERENCE
    https://docs.python.org/3.5/library/os
    
    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.
 
DESCRIPTION
    This exports:
      - all functions from posix, nt or ce, e.g. unlink, stat, etc.
      - os.path is either posixpath or ntpath
      - os.name is either 'posix', 'nt' or 'ce'.
      - os.curdir is a string representing the current directory ('.' or ':')
      - os.pardir is a string representing the parent directory ('..' or '::')
      - os.sep is the (or a most common) pathname separator ('/' or ':' or '\')
      - os.extsep is the extension separator (always '.')
      - os.altsep is the alternate pathname separator (None or '/')
      - os.pathsep is the component separator used in $PATH etc
      - os.linesep is the line separator in text files ('r' or 'n' or 'rn')
      - os.defpath is the default search path for executables
      - os.devnull is the file path of the null device ('/dev/null', etc.)

obj?:列出obj对象的详细信息

 
In [7]: a?
Type:        int
String form: 100
Docstring:  
int(x=0) -> integer
int(x, base=10) -> integer
 
Convert a number or string to an integer, or return 0 if no arguments
are given.  If x is a number, return x.__int__().  For floating point
numbers, this truncates towards zero.
 
If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base.  The literal can be preceded by '+' or '-' and be surrounded
by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4

obj??:列出obj对象的更多详细信息

 
In [8]: a??
Type:        int
String form: 100
Docstring:  
int(x=0) -> integer
int(x, base=10) -> integer
 
Convert a number or string to an integer, or return 0 if no arguments
are given.  If x is a number, return x.__int__().  For floating point
numbers, this truncates towards zero.
 
If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base.  The literal can be preceded by '+' or '-' and be surrounded
by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4

特殊变量

_:表示倒数第一次输出

 
In [15]: b = 1000
 
In [16]: type(b)
Out[16]: int
 
In [17]: _
Out[17]: int

__:表示倒数第二次输出

 
In [15]: b = 1000
 
In [16]: type(b)
Out[16]: int
 
In [17]: _
Out[17]: int
 
In [18]: c = "i love python"
 
In [19]: print(c)
i love python
 
In [20]: __
Out[20]: int

___:表示倒数第三次输出

 
 
In [15]: b = 1000
 
In [16]: type(b)
Out[16]: int
 
In [17]: _
Out[17]: int
 
In [18]: c = "i love python"
 
In [19]: print(c)
i love python
 
In [20]: __
Out[20]: int
 
In [21]: d = "hello ipython"
 
In [22]: print(d)
hello ipython
 
In [23]: ___
Out[23]: int

_dh:目录历史

 
In [24]: _dh
Out[24]: ['/home/python/jjedu/projects/cmdb']

_oh:输出历史

 
In [25]: _oh
Out[25]: 
{5: module,
 10: module,
 11: module,
 14: module,
 16: int,
 17: int,
 20: int,
 23: int,
 24: ['/home/python/jjedu/projects/cmdb']}

Shell命令

在ipython中,使用!可以来执行shell命令。

 
In [27]: !pwd
/home/python/jijiedu/projects/cmdb
 
In [28]: !free -m
              total        used        free      shared  buff/cache   available
Mem:            992         654          64           0         273         182
Swap:             0           0           0

魔术变量

□ 使用%百分号开头的,IPython内置的特殊方法 1.%magic,%是line magic,%%是cell magic,notebook的cell。 2.%alias定义一个系统命令的别名

 
In [30]: alias ll ls -l
 
In [31]: ll
total 38216
-rw------- 1 python python   242133 Apr 16 15:33 nohup.out
-rw-rw-r-- 1 python python     2967 Mar 31 13:38 九九乘法表.ipynb
-rw-rw-r-- 1 python python     2281 Apr  9 13:24 冒泡法.ipynb
-rw-rw-r-- 1 python python     4395 Apr  3 16:40 列表.ipynb
-rw-rw-r-- 1 python python     3474 Apr 10 10:19 字符串.ipynb
-rw-rw-r-- 1 python python     4568 Mar 31 13:46 打印闪电_菱形_乘法表.ipynb
-rw-rw-r-- 1 python python    29439 Apr 12 17:15 批量替换字符串中的字符.ipynb

3.%timeit statement -n:一个循环loop执行语句多少次 -r:循环执行多少次loop,取最好的结果。

4. %%timeit setup_code code…

5. %cd改变当前工作目录,cd可以认为是%cd的连接,路径历史在_dh中查看。

 
In [32]: %cd /data
/data
 
In [33]: _dh
Out[33]: ['/home/python/jijiedu/projects/cmdb', '/data']

6.%pwd显示当前工作目录

 
In [34]: %pwd
Out[34]: '/data'

7.%ls返回文件列表

 
In [37]: %ls
nohup.out         列表.ipynb                  批量替换字符串中的字符.ipynb  猴子吃桃问题讲解_100以内斐波那契数列.ipynb

8.%%js、%%javascript在cell中运行js脚本

注:%pwd这种事魔术方法,是IPython的内部实现,和操作系统无关。而!pwd就要依赖当前操作系统的shell提供的命令执行,默认windows不支持pwd命令。