专栏首页运维技术迷连仕彤博客[Python笔记] IPython使用技巧

连仕彤博客[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 '\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对象的详细信息

 
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命令。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 连仕彤博客pyenv环境部署

    pyenv是一款Python多版本管理工具,它可以很好的管理Python解释器、Python版本和Python虚拟环境,对于日常开发中,是一款很有必要安装的利器...

    行 者
  • [Python笔记] 文件IO常用操作

    文件操作中,最常用的操作就是读和写; 文件访问的模式有昂中:文本模式和二进制模式。不同模式下,操作函数不尽相同,表现的结果也不一样。

    行 者
  • PXE无人值守安装Linux

    一、网络(PXE)装机概述 1.传统装机的局限性 1.1需要用到固定的光驱、U盘等物理介质 1.2如果批量安装多台计算机,都用到物理介质,显然不太现实。 2...

    行 者
  • 10分钟上手,OpenCV自然场景文本检测(Python代码+实现)

    EAST文本检测器需要OpenCV3.4.2或更高版本,有需要的读者可以先安装OpenCV。

    磐创AI
  • 实验2 C++数组与指针

    步行者08
  • 10分钟上手,OpenCV自然场景文本检测(Python代码+实现)

    EAST文本检测器需要OpenCV3.4.2或更高版本,有需要的读者可以先安装OpenCV。

    新智元
  • 转载:Package by feature, not layer

    The first question in building an application is "How do I divide it up into pac...

    用户1172465
  • CF-1197C-C. Array Splitting

    某些人
  • US oil prices turn negative as demand dries up

    The price of US oil has turned negative for the first time in history.

    仇诺伊
  • POJ 2374 Fence Obstacle Course(线段树+动态规划)

    Fence Obstacle Course Time Limit: 3000MS Memory Limit: 65536K Total Sub...

    ShenduCC

扫码关注云+社区

领取腾讯云代金券