<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之旅 廣告
                # Python?元組 Python的元組與列表類似,不同之處在于元組的元素不能修改。 元組使用小括號,列表使用方括號。 元組創建很簡單,只需要在括號中添加元素,并使用逗號隔開即可。 如下實例: ~~~ tup1 = ('physics', 'chemistry', 1997, 2000); tup2 = (1, 2, 3, 4, 5 ); tup3 = "a", "b", "c", "d"; ~~~ 創建空元組 ~~~ tup1 = (); ~~~ 元組中只包含一個元素時,需要在元素后面添加逗號 ~~~ tup1 = (50,); ~~~ 元組與字符串類似,下標索引從0開始,可以進行截取,組合等。 ## 訪問元組 元組可以使用下標索引來訪問元組中的值,如下實例: ~~~ #!/usr/bin/python tup1 = ('physics', 'chemistry', 1997, 2000); tup2 = (1, 2, 3, 4, 5, 6, 7 ); print "tup1[0]: ", tup1[0] print "tup2[1:5]: ", tup2[1:5] ~~~ 以上實例輸出結果: ~~~ tup1[0]: physics tup2[1:5]: (2, 3, 4, 5) ~~~ ## 修改元組 元組中的元素值是不允許修改的,但我們可以對元組進行連接組合,如下實例: ~~~ #!/usr/bin/python # -*- coding: UTF-8 -*- tup1 = (12, 34.56); tup2 = ('abc', 'xyz'); # 以下修改元組元素操作是非法的。 # tup1[0] = 100; # 創建一個新的元組 tup3 = tup1 + tup2; print tup3; ~~~ 以上實例輸出結果: ~~~ (12, 34.56, 'abc', 'xyz') ~~~ ## 刪除元組 元組中的元素值是不允許刪除的,但我們可以使用del語句來刪除整個元組,如下實例: ~~~ #!/usr/bin/python tup = ('physics', 'chemistry', 1997, 2000); print tup; del tup; print "After deleting tup : " print tup; ~~~ 以上實例元組被刪除后,輸出變量會有異常信息,輸出如下所示: ~~~ ('physics', 'chemistry', 1997, 2000) After deleting tup : Traceback (most recent call last): File "test.py", line 9, in <module> print tup; NameError: name 'tup' is not defined ~~~ ## 元組運算符 與字符串一樣,元組之間可以使用 + 號和 * 號進行運算。這就意味著他們可以組合和復制,運算后會生成一個新的元組。 | Python 表達式 | 結果 | 描述 | |--|--|--| | len((1, 2, 3)) | 3 | 計算元素個數 | | (1, 2, 3) + (4, 5, 6) | (1, 2, 3, 4, 5, 6) | 連接 | | ['Hi!'] * 4 | ['Hi!', 'Hi!', 'Hi!', 'Hi!'] | 復制 | | 3 in (1, 2, 3) | True | 元素是否存在 | | for x in (1, 2, 3): print x, | 1 2 3 | 迭代 | ## 元組索引,截取 因為元組也是一個序列,所以我們可以訪問元組中的指定位置的元素,也可以截取索引中的一段元素,如下所示: 元組: ~~~ L = ('spam', 'Spam', 'SPAM!') ~~~ | Python 表達式 | 結果 | 描述 | |--|--|--| | L[2] | 'SPAM!' | 讀取第三個元素 | | L[-2] | 'Spam' | 反向讀取;讀取倒數第二個元素 | | L[1:] | ('Spam', 'SPAM!') | 截取元素 | ## 無關閉分隔符 任意無符號的對象,以逗號隔開,默認為元組,如下實例: ~~~ #!/usr/bin/python print 'abc', -4.24e93, 18+6.6j, 'xyz'; x, y = 1, 2; print "Value of x , y : ", x,y; ~~~ 以上實例允許結果: ~~~ abc -4.24e+93 (18+6.6j) xyz Value of x , y : 1 2 ~~~ ## 元組內置函數 Python元組包含了以下內置函數 | 序號 | 方法及描述 | |--|--| | 1 | [cmp(tuple1, tuple2)](http://www.runoob.com/python/att-tuple-cmp.html)比較兩個元組元素。 | | 2 | [len(tuple)](http://www.runoob.com/python/att-tuple-len.html)計算元組元素個數。 | | 3 | [max(tuple)](http://www.runoob.com/python/att-tuple-max.html)返回元組中元素最大值。 | | 4 | [min(tuple)](http://www.runoob.com/python/att-tuple-min.html)返回元組中元素最小值。 | | 5 | [tuple(seq)](http://www.runoob.com/python/att-tuple-tuple.html)將列表轉換為元組。 |
                  <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>

                              哎呀哎呀视频在线观看