003、IPython解释器
本文最后更新于 67 天前,其中的信息可能已经过时,如有错误请发送邮件到wuxianglongblog@163.com

IPython解释器

通常不使用Python自带的解释器,而使用另一个更强大的解释器——IPython解释器。Anaconda自带IPython解释器。命令行输入IPython进入:

ipython

如果系统提示没有IPython可以通过pip命令安装:

pip install ipython

所有在Python解释器下可以运行的代码都可以在IPython解释器下运行:

print("hello, world")
hello, world

赋值:

a = 1

输入变量名查看值:

a
1

变量可以任意被覆盖:

a = "test"
a
'test'

魔术命令

相对于Python解释器,IPython解释器额外提供了很多以百分号%开头的魔术命令,这些命令很像Mac/Linux系统下的命令行命令(事实上有些是一样的)。

查看所有的模式命令:

%lsmagic
Available line magics:
%alias  %alias_magic  %autoawait  %autocall  %automagic  %autosave  %bookmark  %cat  %cd  %clear  %colors  %conda  %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  %pip  %popd  %pprint  %precision  %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.

其中:

  • line magic以一个百分号开头,作用与一行。
  • cell magic以两个百分号开头,作用于整个cell。
  • 最后一行Automagic is ON, % prefix IS NOT needed for line magics.说明在此时即使不加上%也可以使用line magic命令。

使用 whos 查看当前的变量空间:

%whos
Variable   Type    Data/Info
----------------------------
a          str     test

使用 reset 重置当前变量空间:

%reset -f

再使用 whos 查看当前的变量空间:

%whos
Interactive namespace is empty.

使用 pwd 查看当前工作文件夹:

%pwd
'/Users/lijin/Documents/Projects/notes-python3/ch01-Python介绍'

使用 mkdir 产生新文件夹:

%mkdir demo_test

使用 cd 改变工作文件夹:

%cd demo_test
/Users/lijin/Documents/Projects/notes-python3/ch01-Python介绍/demo_test

使用 writefile 将cell中的内容写入文件:

%%writefile hello_world.py
print("hello world")
Writing hello_world.py

使用 ls 查看当前工作文件夹的文件:

%ls
hello_world.py

返回上一层文件夹:

%cd ..
/Users/lijin/Documents/Projects/notes-python3/ch01-Python介绍

使用 rm 删除文件:

%rm demo_test/hello_world.py

使用 rmdir 删除文件夹:

%rmdir demo_test

使用 hist 查看历史命令:

%hist
print("hello, world")
a = 1
a
a = "test"
a
%lsmagic
%whos
%reset -f
%whos
%pwd
%mkdir demo_test
%cd demo_test
%%writefile hello_world.py
print("hello world")
%ls
%cd ..
%rm demo_test/hello_world.py
%rmdir demo_test
%hist

其他用法

使用 ? 查看函数的帮助:

sum?

使用 ?? 查看函数帮助和函数源代码(如果是用python实现的):

# 导入numpy和matplotlib两个包
%pylab
# 查看其中sort函数的帮助
sort??
Using matplotlib backend: 
%pylab is deprecated, use %matplotlib inline and import the required libraries.
Populating the interactive namespace from numpy and matplotlib

使用 _ 使用上个cell的输出结果:

a = 12
a
12
_ + 13
25

可以使用 ! 来执行一些系统命令。

!python -c 'print("hello world")'
hello world

输入代码不合法时,会给出错误提示:

1 + 'hello'
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Input In [24], in ()
----> 1 1 + 'hello'

TypeError: unsupported operand type(s) for +: 'int' and 'str'

虽然输入错误,但解释器不会停止运行,还可以继续输入:

b = 12345
谨此笔记,记录过往。凭君阅览,如能收益,莫大奢望。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇
运行时间
Theme Argon