jupyter notebook中IPython常用命令及快捷键介绍

本文介绍了如何在IPython环境中利用`help()`和`??`查看内置函数的帮助文档,使用`tab`键进行自动补全,以及使用魔法命令如`%run`、`%time`和`%timeit`来运行代码并测量其性能。此外,还提及了查看当前会话变量、导入包、执行系统指令和笔记本快捷键的操作。
摘要由CSDN通过智能技术生成

IPython查看帮助文档

1. 使用help()

help(len)
Help on built-in function len in module builtins:

len(obj, /)
    Return the number of items in a container.

2. 使用? | ??

? 返回帮助文档
?? 把函数的源代码显示出来

len?

Signature: len(obj, /)
Docstring: Return the number of items in a container.
Type: builtin_function_or_method

# 定义add函数
def add(a, b):
    return a + b
# 查看函数add源码
add??

Signature: add(a, b)
Docstring: <no docstring>
Source:
def add(a, b):
return a + b
File: c:\users\lwyco\appdata\local\temp\ipykernel_20040\2264944979.py
Type: function

3. tab自动补全

敲击tab键能自动补全

L.

也可以在import的时候自动补全

import nu

import numpy

IPython魔法命令

%run *.py运行外部Python文件

运行外部python文件(默认是当前目录,最好加上绝对路径)

例如在当前目录下有一个myscript.py文件:

def square(x):
    """square a number"""
    return x ** 2

for N in range(1, 4):
    print(N, "squared is", square(N))
# 相对路径
%run ./myscript.py
1 squared is 1
2 squared is 4
3 squared is 9
# 绝对路径
%run D:\\02-coder\\07_pythonCode\\code\\myscript.py
1 squared is 1
2 squared is 4
3 squared is 9
# 调用外部文件函数
square(5)
25
square??  # 查看源码  

Signature: square(x)
Source:
def square(x):
“”“square a number”“”
return x ** 2
File: d:\02-coder\07_pythoncode\code\myscript.py
Type: function

注意:当我们使用魔法命令执行了一个外部文件时,该文件的函数就能在当前会话中使用

运行计时

%time statement 计算statement的运行时间:
%time sum([i for i in range(100000)])
Wall time: 6.89 ms





4999950000
%timeit statement计算statement的平均运行时间:

timeit会多次运行statement,最后得到一个更为精准的预期运行时间

%time一般用于耗时长的代码段
%timeit一般用于耗时短的代码段

# it = iteration  迭代
%timeit sum([i for i in range(100000)])
2.95 ms ± 194 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%%timeit 计算多行代码的平均运行时间:

`
%%timeit

statement1

statement2

statement3

`

%%timeit
sum([i for i in range(100000)])
square(100000)
2.82 ms ± 166 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
导入python自带的 time包计算运行时间
import time
start = time.time()
%run myscript.py
end = time.time()
print(f'花费时间: {end - start}')

1 squared is 1
2 squared is 4
3 squared is 9
花费时间: 0.0076024532318115234

查看当前会话中的所有变量与函数

%who快速查看当前会话的所有变量与函数名称
%who
N	 add	 end	 square	 start	 time	 
%whos查看当前会话的所有变量与函数名称的详细信息
%whos
Variable   Type        Data/Info
--------------------------------
N          int         3
add        function    <function add at 0x000001BF2C29FEE8>
end        float       1708583142.299468
square     function    <function square at 0x000001BF2C3A1F78>
start      float       1708583142.2918656
time       module      <module 'time' (built-in)>
%who_ls返回一个字符串列表,里面元素是当前会话的所有变量与函数名称
%who_ls
['N', 'add', 'end', 'square', 'start', 'time']

执行系统指令

Window指令:

$ dir # dir = list working directory contents
notebooks projects

在window指令之前加上 !,即可在ipython当中执行window指令。

注意会将标准输出以字符串形式返回

!dir
 驱动器 D 中的卷是 软件
 卷的序列号是 9E1D-C3F5

 D:\02-coder\07_pythonCode\code 的目录

2024/02/22  14:30    <DIR>          .
2024/02/21  17:35    <DIR>          ..
2024/02/21  18:54    <DIR>          .ipynb_checkpoints
2024/02/22  14:30            24,459 IPython.ipynb
2024/02/22  11:47               119 myscript.py
2024/02/21  19:01             1,772 Untitled.ipynb
               3 个文件         26,350 字节
               3 个目录 325,063,933,952 可用字节
# 可以通过这种方式, 使用pip安装包
!pip install pillow -i https://pypi.douban.com/simle
Looking in indexes: https://pypi.douban.com/simle
Requirement already satisfied: pillow in d:\02-coder\07_pythoncode\python_env\venv\lib\site-packages (9.5.0)



[notice] A new release of pip is available: 23.3.1 -> 24.0
[notice] To update, run: python.exe -m pip install --upgrade pip

更多魔法命令

lsmagic列出所有魔法命令
lsmagic
Available line magics:
%alias  %alias_magic  %autoawait  %autocall  %automagic  %autosave  %bookmark  %cd  %clear  %cls  %colors  %conda  %config  %connect_info  %copy  %ddir  %debug  %dhist  %dirs  %doctest_mode  %echo  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %macro  %magic  %matplotlib  %mkdir  %more  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %pip  %popd  %pprint  %precision  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %ren  %rep  %rerun  %reset  %reset_selective  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%cmd  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.

查看魔法命令的文档:
使用?

notebook的快捷键

1、命令模式 ( Esc 进入命令模式)

• Shift-Enter : 运行本单元,选中下个单元
• Ctrl-Enter : 运行本单元
• Alt-Enter : 运行本单元,在下面插入一单元
• Y : 单元转入代码状态
• M :单元转入markdown状态
• A : 在上方插入新单元
• B : 在下方插入新单元

2、编辑模式 ( Enter 键启动)

• Tab : 代码补全或缩进  
• Shift-Tab : 提示  
• Ctrl-A : 全选  
• Ctrl-Z : 复原  

更多快捷键点击命令配置查看
查看jupyter notebook 快捷键

  • 22
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值