<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 元組 元組和列表十分類似,只不過元組和字符串一樣是 不可變的 即你不能修改元組。元組通過圓括號中用逗號分割的項目定義。元組通常用在使語句或用戶定義的函數能夠安全地采用一組值的時候,即被使用的元組的值不會改變。 ``` #!/usr/bin/python # Filename: using_tuple.py zoo = ('wolf', 'elephant', 'penguin') print 'Number of animals in the zoo is', len(zoo) new_zoo = ('monkey', 'dolphin', zoo) print 'Number of animals in the new zoo is', len(new_zoo) print 'All animals in new zoo are', new_zoo print 'Animals brought from old zoo are', new_zoo[2] print 'Last animal brought from old zoo is', new_zoo[2][2] ``` (源文件:[code/using_tuple.py](code/using_tuple.py)) ## 輸出 ``` $ python using_tuple.py Number of animals in the zoo is 3 Number of animals in the new zoo is 3 All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin')) Animals brought from old zoo are ('wolf', 'elephant', 'penguin') Last animal brought from old zoo is penguin ``` ## 它如何工作 變量`zoo`是一個元組,我們看到`len`函數可以用來獲取元組的長度。這也表明元組也是一個[序列](ch09s05.html)。 由于老動物園關閉了,我們把動物轉移到新動物園。因此,`new_zoo`元組包含了一些已經在那里的動物和從老動物園帶過來的動物。回到話題,注意元組之內的元組不會失去它的身份。 我們可以通過一對方括號來指明某個項目的位置從而來訪問元組中的項目,就像我們對列表的用法一樣。這被稱作 索引 運算符。我們使用`new_zoo[2]`來訪問`new_zoo`中的第三個項目。我們使用`new_zoo[2][2]`來訪問`new_zoo`元組的第三個項目的第三個項目。 **含有0個或1個項目的元組。**一個空的元組由一對空的圓括號組成,如`myempty = ()`。然而,含有單個元素的元組就不那么簡單了。你必須在第一個(唯一一個)項目后跟一個逗號,這樣Python才能區分元組和表達式中一個帶圓括號的對象。即如果你想要的是一個包含項目`2`的元組的時候,你應該指明`singleton = (2 , )`。 給Perl程序員的注釋 列表之中的列表不會失去它的身份,即列表不會像Perl中那樣被打散。同樣元組中的元組,或列表中的元組,或元組中的列表等等都是如此。只要是Python,它們就只是使用另一個對象存儲的對象。 元組最通常的用法是用在打印語句中,下面是一個例子: ``` #!/usr/bin/python # Filename: print_tuple.py age = 22 name = 'Swaroop' print '%s is %d years old' % (name, age) print 'Why is %s playing with that python?' % name ``` (源文件:[code/print_tuple.py](code/print_tuple.py)) ## 輸出 ``` $ python print_tuple.py Swaroop is 22 years old Why is Swaroop playing with that python? ``` ## 它如何工作 `print`語句可以使用跟著`%`符號的項目元組的字符串。這些字符串具備定制的功能。定制讓輸出滿足某種特定的格式。定制可以是`%s`表示字符串或`%d`表示整數。元組必須按照相同的順序來對應這些定制。 觀察我們使用的第一個元組,我們首先使用`%s`,這對應變量`name`,它是元組中的第一個項目。而第二個定制是`%d`,它對應元組的第二個項目`age`。 Python在這里所做的是把元組中的每個項目轉換成字符串并且用字符串的值替換定制的位置。因此`%s`被替換為變量`name`的值,依此類推。 `print`的這個用法使得編寫輸出變得極其簡單,它避免了許多字符串操作。它也避免了我們一直以來使用的逗號。 在大多數時候,你可以只使用`%s`定制,而讓Python來提你處理剩余的事情。這種方法對數同樣奏效。然而,你可能希望使用正確的定制,從而可以避免多一層的檢驗程序是否正確。 在第二個`print`語句中,我們使用了一個定制,后面跟著`%`符號后的單個項目——沒有圓括號。這只在字符串中只有一個定制的時候有效。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看