python學習--元組

來源:互聯網
上載者:User

標籤:pos   元組   建立   字串   --   python元組   需要   body   print   

元組的基本操作:
1.建立一個元組:

tuple=(1,26);tuple1=("15","sy");

建立一個空元組:

tuple=();

元組中只包含一個元素時,需要在元素後面添加逗號來消除歧義;

tuple=(50,)

2.訪問元組:

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]

3.修改元組:

tup1 = (12, 34.56);tup2 = (‘abc‘, ‘xyz‘);# 以下修改元組元素操作是非法的。# tup1[0] = 100;# 建立一個新的元組tup3 = tup1 + tup2;print tup3;#以上執行個體輸出結果:#(12, 34.56, ‘abc‘, ‘xyz‘)

4.刪除元組:

tup = (‘physics‘, ‘chemistry‘, 1997, 2000);print tup;del tup;print "After deleting tup : "print tup;

5.與字串一樣,元組之間可以使用 + 號和 * 號進行運算。這就意味著他們可以組合和複製,運算後會產生一個新的元組。

6.

Python元組包含了以下內建函數
1、cmp(tuple1, tuple2):比較兩個元組元素。
2、len(tuple):計算元組元素個數。
3、max(tuple):返回元組中元素最大值。
4、min(tuple):返回元組中元素最小值。
5、tuple(seq):將列錶轉換為元組。

python學習--元組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.