编程语言
首页 > 编程语言> > jupyter中ipython的基本使用方法,帮助你更快速高效的学习

jupyter中ipython的基本使用方法,帮助你更快速高效的学习

作者:互联网

精心整理的jupyter notebook基本使用方法,分享给大家。
感谢大家的点赞与关注,欢迎共同学习交流。

jupyter中ipython的基本使用方法

一、启动程序

命令:jupyter notebook

这个命令可以启动jupyter的交互服务器,并且把当前目录作为映射打开一个web界面,加载映射的目录结构

【注意】如果这个命令提示错误,检测环境变量还有anaconda是否安装完全(如果不完全:手动安装pip install jupyter)

二、IPython的帮助文档

1、使用help()

help(list)
Help on class list in module builtins:

class list(object)
 |  list() -> new empty list
 |  list(iterable) -> new list initialized from iterable's items
 |  
 |  Methods defined here:
 |  
 |  __add__(self, value, /)
 |      Return self+value.
 |  
 |  __contains__(self, key, /)
 |      Return key in self.
 |  
 |  __delitem__(self, key, /)
 |      Delete self[key].
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __iadd__(self, value, /)
 |      Implement self+=value.
 |  
 |  __imul__(self, value, /)
 |      Implement self*=value.
 |  
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __len__(self, /)
 |      Return len(self).
 |  
 |  __lt__(self, value, /)
 |      Return self<value.
 |  
 |  __mul__(self, value, /)
 |      Return self*value.n
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __reversed__(...)
 |      L.__reversed__() -- return a reverse iterator over the list
 |  
 |  __rmul__(self, value, /)
 |      Return self*value.
 |  
 |  __setitem__(self, key, value, /)
 |      Set self[key] to value.
 |  
 |  __sizeof__(...)
 |      L.__sizeof__() -- size of L in memory, in bytes
 |  
 |  append(...)
 |      L.append(object) -> None -- append object to end
 |  
 |  clear(...)
 |      L.clear() -> None -- remove all items from L
 |  
 |  copy(...)
 |      L.copy() -> list -- a shallow copy of L
 |  
 |  count(...)
 |      L.count(value) -> integer -- return number of occurrences of value
 |  
 |  extend(...)
 |      L.extend(iterable) -> None -- extend list by appending elements from the iterable
 |  
 |  index(...)
 |      L.index(value, [start, [stop]]) -> integer -- return first index of value.
 |      Raises ValueError if the value is not present.
 |  
 |  insert(...)
 |      L.insert(index, object) -- insert object before index
 |  
 |  pop(...)
 |      L.pop([index]) -> item -- remove and return item at index (default last).
 |      Raises IndexError if list is empty or index is out of range.
 |  
 |  remove(...)
 |      L.remove(value) -> None -- remove first occurrence of value.
 |      Raises ValueError if the value is not present.
 |  
 |  reverse(...)
 |      L.reverse() -- reverse *IN PLACE*
 |  
 |  sort(...)
 |      L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __hash__ = None
help(len)
Help on built-in function len in module builtins:

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

2、使用"?"

len?
list?

“?”调出一个函数的帮助文档,“??”调出一个函数的帮助文档以及源码

len??
def add_sum(a,b):
    "求两个数的和"
    c = a+b
    return c
add_sum??

3、使用"tab"键自动补全

aaaaa=10
import numpy

aaaaa
10

三、IPython魔法命令

1、运行在外部Python文件

%run xx.py
下面例子中的外部test.py文件的内容如下:

def hello(a):
    c = a**2
    m = 10000
    return c
w = 10000

以下为在Jupyter notebook中输入的代码:

%run test.py
hello(10)
100
w
10000
m # m是局部变量
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-24-69b64623f86d> in <module>()
----> 1 m


NameError: name 'm' is not defined

2、查看运行计时

%time print("hello")
hello
Wall time: 0 ns
def func1():
    res = 0
    for i in range(1000):
        res += 1
        for j in range(1000):
            res -= 1
            
%time func1()
Wall time: 65.5 ms
%timeit func1()
76.8 ms ± 4.24 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%%timeit
print("afda")
func1()
list("123")
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
136 ms ± 19.3 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

%time 一般耗时比较多的代码用这个
%timeit 一般是耗时比较少的代码

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

%who
a	 aaaaa	 add_sum	 func1	 hello	 numpy	 w	 
%whos
Variable   Type        Data/Info
--------------------------------
a          int         10
aaaaa      int         10
add_sum    function    <function add_sum at 0x000001957CA21840>
func1      function    <function func1 at 0x000001957CEEB840>
hello      function    <function hello at 0x000001957CEEB8C8>
numpy      module      <module 'numpy' from 'C:\<...>ges\\numpy\\__init__.py'>
w          int         10000
%who_ls
['a', 'aaaaa', 'add_sum', 'func1', 'hello', 'numpy', 'w']

%who、%whos、%who_ls查看当前cell运行的时候,ipython服务器中有那些函数和变量以及框架库(modele)等存在

4、执行系统终端指令

写法:!指令名(在windows系统下应该执行Windows的系统命令,linux要执行对应的Linux版本的系统zhil)

!ipconfig
Windows IP 配置


以太网适配器 以太网:

   连接特定的 DNS 后缀 . . . . . . . : 
   本地链接 IPv6 地址. . . . . . . . : fe80::b5c8:db48:5d06:3657%5
   IPv4 地址 . . . . . . . . . . . . : 10.31.153.97
   子网掩码  . . . . . . . . . . . . : 255.255.255.0
   默认网关. . . . . . . . . . . . . : 10.31.161.1
!cd ..
!mkdir ppp

5、更多魔法指令或者cmd

列出所有的魔法指令
%lsmagic

%lsmagic
Available line magics:
%alias  %alias_magic  %autocall  %automagic  %autosave  %bookmark  %cd  %clear  %cls  %colors  %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  %popd  %pprint  %precision  %profile  %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.
%cd?
print("sdafads")
sdafads

四、快捷键

1、命令模式

enter: 转入编辑模式

shift+enter:运行本行,并且选中下行

ctrl+enter: 运行本行,并且选中本行

alt+enter:运行本行,并且插入一个新的cell

Y:cell转入代码状态

M:cell转入Markdown状态

A: 在上方插入一个新的cell

B:在下方插入一个新的cell

双击D:删除当前cell

2、编辑模式

tab(或shift+tab)键:提示

ctrl+a:全选当前cell

ctrl+z:撤销

标签:__,高效,afda,%%,self,list,value,ipython,jupyter
来源: https://blog.csdn.net/qq_42589613/article/details/123104105