跟老齊學Python之資料類型總結_python

來源:互聯網
上載者:User

下面的表格中列出了已經學習過的資料類型,也是python的核心資料類型之一部分,這些都被稱之為內建對象。

對象,就是你面對的所有東西都是對象,看官要逐漸熟悉這個稱呼。所有的資料類型,就是一種對象。英文單詞是object,直接的漢語意思是物體,這就好像我們在現實中一樣,把很多我們看到和用到的都可以統稱為“東西”一樣。“東西”就是“對象”,就是object。在編程中,那個所謂物件導向,也可以說成“面向東西”,是嗎?容易有歧義吧。

物件類型 舉例
int/float 123, 3.14
str 'qiwsir.github.io'
list [1, [2, 'three'], 4]
dict {'name':"qiwsir","lang":"python"}
tuple (1, 2, "three")
set set("qi"), {"q", "i"}

不論任何類型的資料,只要動用dir(object)或者help(obj)就能夠在互動模式下查看到有關的函數,也就是這樣能夠查看相關協助文檔了。舉例:

複製代碼 代碼如下:

>>> dir(dict)

看官需要移動滑鼠,就能夠看全(下面的本質上就是一個list):

複製代碼 代碼如下:

['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']

先略過__雙底線開頭的哪些,看後面的,就是dict的內建函數。至於詳細的操作方法,通過類似help(dict.pop)的方式獲得。這是前面說過的,再說一遍,加深印象。

我的觀點:學習,重要的是學習方法,不是按部就班的敲代碼。

今天既然是複習,就要在原來基礎上提高一點。所以,也要看看上面那些以雙底線開頭的東西,請看官找一下,有沒有發現這個:"__doc"。這是什麼,它是一個檔案,裡面記錄了對當前所查看的對象的詳細解釋。可以在互動模式下這樣查看:

>>> dict.__doc__
顯示應該是這樣的:

複製代碼 代碼如下:

"dict() -> new empty dictionary\ndict(mapping) -> new dictionary initialized from a mapping object's\n (key, value) pairs\ndict(iterable) -> new dictionary initialized as if via:\n d = {}\n for k, v in iterable:\n d[k] = v\ndict(**kwargs) -> new dictionary initialized with the name=value pairs\n in the keyword argument list. For example: dict(one=1, two=2)"

注意看上面亂七八糟的英文中,是不是有\n符號,這是什嗎?前面在講述字串的時候提到了轉義符號\,這是換一行。也就是說,如果上面的文字,按照排版要求,應該是這樣的(當然,在文本中,如果開啟,其實就是排好版的樣子)。

複製代碼 代碼如下:

"dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
(key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
d = {}
for k, v in iterable:
d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)"

可能排版還是不符合願意。不過,看官也大概能看明白了。我要說的不是排版,要說的是告訴看官一種查看某個資料類型含義的方法,就是通過obj.doc檔案來看。

嘿嘿,其實有一種方法,可以看到排版的結果的:

複製代碼 代碼如下:

>>> print dict.__doc__
dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
    (key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
    d = {}
    for k, v in iterable:
        d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
    in the keyword argument list.  For example:  dict(one=1, two=2)

上面那麼折騰一下,就是為了湊篇幅,不然這個總結的東西太少了。

總之,只要用這種方法,你就能得到所有協助文檔,隨時隨地。如果可以上網,到官方網站,是另外一種方法。

還需要再解釋別的嗎?都多餘了。唯一需要的是看官要能會點英語。不過我相信看官能夠讀懂,我這個二把刀都不如的英語水平,還能湊合看呢,何況看官呢?

總結不是意味著結束,是意味著繼往開來。精彩還在後面,這裡只是休息。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.