一、启动程序
执行以下命令:
jupyter notebook
[NotebookApp] Serving notebooks from local directory: /home/nanfengpo
[NotebookApp] 0 active kernels
[NotebookApp] The IPython Notebook is running at: http://localhost:8888/
[NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
注意以下几点:
- 打开地址为当前bash的目录,默认的根目录
- 浏览器地址为http://localhost:8888/
- 通过control -C终止jupyter程序
几个基本操作:
- 双击D:删除当前cell
- 单击M:转为markdown文档
- markdown文档下运行变为预览模式
二、IPython的帮助文档
1. 使用help()
通过以下命令来获得帮助文档:
help(len)
Help on built-in function len in module builtins:
len(obj, /)
Return the number of items in a container.
help(len)
Help on built-in function len in module builtins:
len(obj, /)
Return the number of items in a container.
len([1,2,3])
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
2. 使用?
或者使用问号:
len?
还可以应用到自定义的变量和自定义的函数上来返回帮助文档
此外,使用两个??可以把函数的源代码显示出来
len??
#上面插入一行
#Jupyter ,代码可以时时进行,没有先后行数的概念
square(101)
10201
square?
square??
#随意定义一个方法
def square(num):
'''
该方法,返回数字的平方
'''
ret = num*num
return ret
3. tab自动补全
敲击tab键能自动补全
L.
也可以在import的时候自动补全
import nu
import numpy as np
import time as time
三、IPython魔法命令
1. 运行外部Python文件
使用下面命令运行外部python文件(默认是当前目录,最好加上绝对路径)
%run *.py
例如在当前目录下有一个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
尤其要注意的是,当我们使用魔法命令执行了一个外部文件时,该文件的函数就能在当前会话中使用
square(5)
#魔法指令,可以拓展我们的Jupyter ,更加强大
#魔法指令,%开头
%run '/home/nanfengpo/Desktop/test.py'
4950
#调用外部文件中定义的方法
calc_sum(1024)
523776
2. 运行计时
用下面命令计算statement的运行时间:
%time statement
#below 下面,自动向下插入一行
%time calc_sum(1024)
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 97.8 µs
523776
用下面命令计算statement的平均运行时间:
%timeit statement
timeit会多次运行statement,最后得到一个更为精准的预期运行时间
#cell 单元前面
%timeit calc_sum(2048)
111 µs ± 7.18 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
可以使用两个百分号来测试多行代码的平均运行时间:
`
%%timeit
statement1
statement2
statement3
`
记住:
- %time一般用于耗时长的代码段
- %timeit一般用于耗时短的代码段
%%timeit
calc_sum(1024)
calc_sum(512)
square(100000)
80 µs ± 1.98 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
3. 查看当前会话中的所有变量与函数
快速查看当前会话的所有变量与函数名称:
%who
%who
calc_sum np square time
查看当前会话的所有变量与函数名称的详细信息:
%whos
a = 1024
%whos
Variable Type Data/Info
--------------------------------
a int 1024
calc_sum function <function calc_sum at 0x7f7458114b70>
np module <module 'numpy' from '/us<...>kages/numpy/__init__.py'>
square function <function square at 0x7f747074b950>
time module <module 'time' (built-in)>
返回一个字符串列表,里面元素是当前会话的所有变量与函数名称:
%who_ls
#above
len(ls)
5
ls = %who_ls
4. 执行Linux指令
Linux指令:
$ echo "hello world" # echo is like Python's print function
hello world
$ pwd # pwd = print working directory
/home/jake # this is the "path" that we're sitting in
$ ls # ls = list working directory contents
notebooks projects
$ mkdir mm
/home/jake/projects
$touch txt
!touch /home/nanfengpo/Desktop/xx/hello.txt
在Linux指令之前加上 !,即可在ipython当中执行Linux指令。
注意会将标准输出以字符串形式返回
!cd ..
!pwd
/home/nanfengpo/Documents/data_analysis/1-IPython/softpo
!touch '/home/nanfengpo/Desktop/softpo/python.word'
!mkdir '/home/nanfengpo/Desktop/softpo'
!ls
IPython.ipynb test.ipynb
!pwd
/home/nanfengpo/Documents/data_analysis/1-IPython/softpo
!echo 'hello'
hello
5. 更多魔法命令
列出所有魔法命令
lsmagic
查看魔法命令的文档:
使用?
%lsmagic
Available line magics:
%alias %alias_magic %autocall %automagic %autosave %bookmark %cat %cd %clear %colors %config %connect_info %cp %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %lf %lk %ll %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro %magic %man %matplotlib %mkdir %more %mv %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 %rep %rerun %reset %reset_selective %rm %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 %%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.
lsmagic
Available line magics:
%alias %alias_magic %autocall %automagic %autosave %bookmark %cat %cd %clear %colors %config %connect_info %cp %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %lf %lk %ll %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro %magic %man %matplotlib %mkdir %more %mv %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 %rep %rerun %reset %reset_selective %rm %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 %%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、命令模式
• Enter : 转入编辑模式
• Shift-Enter : 运行本单元,选中下个单元
• Ctrl-Enter : 运行本单元,选中下个单元
• Alt-Enter : 运行本单元,在下面插入一单元
• Y : 单元转入代码状态
• M :单元转入markdown状态
• A : 在上方插入新单元
• B : 在下方插入新单元
2、编辑模式 ( Enter 键启动)
• Tab : 代码补全或缩进
• Shift-Tab : 提示
print('hello')
hello
• Ctrl-A : 全选
• Ctrl-Z : 复原
============================================
练习:
在Jupyter上实现以前的代码,包括:
- 简单代码
- 分支
- 循环
- 函数
- 类
========================================