连仕彤博客[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命令。
- log4j:ERROR Failed to load driver
- 前端MVC Vue2学习总结(七)——ES6与Module模块化、Vue-cli脚手架搭建、开发、发布项目与综合示例
- 前端MVC Vue2学习总结(六)——axios与跨域HTTP请求、Lodash工具库
- spring之config.xml完整版示例
- 前端MVC Vue2学习总结(五)——表单输入绑定、组件
- 【HCTF】2017部分Web出题思路详解
- 前端MVC Vue2学习总结(四)——条件渲染、列表渲染、事件处理器
- 如何移除Android应用广告
- 前端MVC Vue2学习总结(三)——模板语法、过滤器、计算属性、观察者、Class 与 Style 绑定
- 前端MVC Vue2学习总结(二)——Vue的实例、生命周期与Vue脚手架(vue-cli)
- hive具体操作
- hive中配置hwi
- 从零开始内网安全渗透学习
- hive启动后相关操作
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 诸葛亮vs司马懿,排序算法大战谁能笑到最后?
- Centos密钥登陆,解决云服务器被尝试登陆问题
- Fair Scheduler到Capacity Scheduler 转换工具
- 这可能是把SpringBoot配置文件讲解的最详细的文章了
- 抛出这8个问题,检验一下你到底会不会ThreadLocal,来摸个底~
- 最近合肥降雨不断,给自己做一个天气情况微信定时推送吧
- 【适合收藏】为了多点时间陪女朋友,我向BAT大佬跪求了这15条JS技巧
- iOS文本尺寸自适应异步计算实现
- CentOS7下系统分析与排障利器sysdig简单介绍
- Faker 一个生成虚拟数据的Python模块
- ICLR 2020 Mogrifier LSTM 解析
- 优秀的程序员是如何利用工具来提升工作效率的?
- webpack实战——一切皆模块
- IntelljJ最新激活教程,有效期到 2089 年!
- 【从0到1学算法】二分查找法