前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >连仕彤博客[Python笔记] IPython使用技巧

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

作者头像
行 者
发布2018-04-17 15:06:23
5050
发布2018-04-17 15:06:23
举报
文章被收录于专栏:运维技术迷运维技术迷

帮助

?:IPython的概述和简介

代码语言:javascript
复制
 
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):查询指定名称的帮助

代码语言:javascript
复制
 
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 '\r\n')
      - os.defpath is the default search path for executables
      - os.devnull is the file path of the null device ('/dev/null', etc.)

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

代码语言:javascript
复制
 
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对象的更多详细信息

代码语言:javascript
复制
 
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

特殊变量

_:表示倒数第一次输出

代码语言:javascript
复制
 
In [15]: b = 1000
 
In [16]: type(b)
Out[16]: int
 
In [17]: _
Out[17]: int

__:表示倒数第二次输出

代码语言:javascript
复制
 
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

___:表示倒数第三次输出

代码语言:javascript
复制
 
 
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:目录历史

代码语言:javascript
复制
 
In [24]: _dh
Out[24]: ['/home/python/jjedu/projects/cmdb']

_oh:输出历史

代码语言:javascript
复制
 
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命令。

代码语言:javascript
复制
 
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定义一个系统命令的别名

代码语言:javascript
复制
 
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中查看。

代码语言:javascript
复制
 
In [32]: %cd /data
/data
 
In [33]: _dh
Out[33]: ['/home/python/jijiedu/projects/cmdb', '/data']

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

代码语言:javascript
复制
 
In [34]: %pwd
Out[34]: '/data'

7.%ls返回文件列表

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

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

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 帮助
  • 特殊变量
  • Shell命令
  • 魔术变量
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档