いまさら IPython を初めて使ってみる
いまさらですが、IPython は python のインタラクティブシェルを拡張したもので、タブ補完機能などが使えるみたいです。
Python for Data Analysis の本を読む機会があって、そこで初めて試してみました。
起動
ipython コマンドで起動可能です。
実はインストールした覚えがないのですが、恐らく Anaconda を使って python インストールした時に、一緒に入ったのではないかと思います。
Python 3.6.3 に対して、IPython はもう 6.1.0 ですか。
$ ipython Python 3.6.3 |Anaconda, Inc.| (default, Oct 13 2017, 12:02:49) Type 'copyright', 'credits' or 'license' for more information IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.
試行
とりあえず、試してみます。
最近、python2 に慣れているので、単純に文法ミス。。
In [1]: a = 5 In [2]: print a File "<ipython-input-2-9d7b17ad5387>", line 1 print a ^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(a)?
通常のインタラクティブシェルと同様に変数だけ指定しても出力してくれます。
その場合、In に対して Out が分かり易く表示されています。
In [3]: print(a) 5 In [4]: a Out[4]: 5
文字列の場合。
In [24]: greeting = "hello ipython!!" In [25]: greeting Out[25]: 'hello ipython!!'
%run ファイル名 を指定することで、ローカルにあるスクリプトファイルを実行することも可能です。
In [23]: %run test.py this is test.
集合の中のデータ長が長くなると、改行して表示してくれる。
In [17]: data = {i : i + 1000000000 for i in range(7)} In [18]: data Out[18]: {0: 1000000000, 1: 1000000001, 2: 1000000002, 3: 1000000003, 4: 1000000004, 5: 1000000005, 6: 1000000006}
うれしい補完機能。
感想
補完機能もついていますし、環境があるのであれば通常のインタラクティブシェルの代わりに使ってみたくなるものでした。