IPython的簡單用法

剛剛接觸python時用的python shell,之後發現IPython有着更加強大的地方,在此做了些筆記,分享一些小小的tips分享給大家。

1.打開IPython環境

如果你裝了anaconda,使用win+r調出命令窗口,輸入ipython就可調出python環境(前提是anaconda加入了環境變量)。

2.Tab鍵補全

    1.補全變量:

In [3]: people = 100

In [4]: peo<tab>    #自動將people變量補全

    2.補全對象方法和屬性:

In [6]: import datetime

In [7]: datetime.<tab>
                  date()        MAXYEAR       timedelta
                  datetime      MINYEAR       timezone
                  datetime_CAPI time()        tzinfo()        # 將datetime模塊的方法和屬性顯示出來

    3.補全文件路徑:

In [7]: C://Users/ZQX/Desktop/py<tab>
                                 "C://Users/ZQX/Desktop/Python 1.0"
                                 "C://Users/ZQX/Desktop/Python 2.0"
                                 C://Users/ZQX/Desktop/pydata-book-2nd-edition/    2.
3.內省

    1.在變量的後面加上一個問號(?)就可以將該對象的有關的信息顯示出來:

In [7]: people?
Type:        int
String form: 100
Docstring:
int(x=0) -> integer
int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments
are given.  If x is a number, return x.__int__().  For floating point
numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base.  The literal can be preceded by '+' or '-' and be surrounded
by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4

    2.兩個問好(??)會將函數的源碼顯示出來

In [7]:datetime??
Type:        module
String form: <module 'datetime' from 'E:\\Anaconda3\\lib\\datetime.py'>
File:        e:\anaconda3\lib\datetime.py
Source:
"""Concrete date/time and related types.

See http://www.iana.org/time-zones/repository/tz-link.html for
time zone and DST data sources.
"""

import time as _time
import math as _math

def _cmp(x, y):
    return 0 if x == y else 1 if x > y else -1

MINYEAR = 1
MAXYEAR = 9999
_MAXORDINAL = 3652059  # date.max.toordinal()

# Utility functions, adapted from Python's Demo/classes/Dates.py, which
# also assumes the current Gregorian calendar indefinitely extended in
# both directions.  Difference:  Dates.py calls January 1 of year 0 day
# number 1.  The code here calls January 1 of year 1 day number 1.  This is
# to match the definition of the "proleptic Gregorian" calendar in Dershowitz
# and Reingold's "Calendrical Calculations", where it's the base calendar
# for all computations.  See the book for algorithms for converting between
# proleptic Gregorian ordinals and many other calendar systems.
    3.搜索IPython命名空間
In [17]: np.*load*?    *是通配符,表示load前面後面不確定,?表示顯示所有匹配的函數
np.__loader__
np.load
np.loads
np.loadtxt
np.pkgload
4.%run、%timeit、%reset

    1.將文件傳給%run,回車直接執行test.py。result是test.py中的變量,直接傳到了IPytoon中

In [18]: %run C://Users/ZQX/Desktop/test.py

In [19]: result
Out[19]: 1.4666666666666666

如果腳本要訪問IPython命名空間中的變量,就要使用%run -i

    2.%timeit :測試python語句的執行時間

In [22]: %timeit a=1
11.9 ns ± 0.00606 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

    3.%reset:刪除命名空間中的全部變量/名稱

In [26]: people=100

In [27]: %reset
Once deleted, variables cannot be recovered. Proceed (y/[n])? y

In [28]: people
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-28-75fc5975ba42> in <module>()
----> 1 people

NameError: name 'people' is not defined

在使用%reset時會詢問是否刪除,輸入y刪除。


5.快捷鍵

ctrl-a    光標到行首                        ctrl-e    光標到行尾

ctrl-b    光標左移一格                    ctrl-f    光標右移一格

ctrl-k    刪除後面所有                    ctrl-u    刪除前面所有

ctrl-p    搜索前一條命令                ctrl-n    搜索後一條命令

ctrl-c    中斷當前執行代碼             ctrl-l     清屏

6.總結
    IPython與其他python shell最大的不同就在於IPython能夠進行交互,能夠直接在輸入框中輸入windows命令或者Linux命令(以自己的操作系統爲準,windows系統下當然不能輸入Linux命令了)。

以上內容當然還是不可能全部瞭解到IPython,這只是我的一些小心得。再見



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章