Python中的元組(tuple) 詳解 及 代碼

來源:互聯網
上載者:User

元組是存放任意元素集合,不能修改其內容;

len()是求元組的長度, 可以使用下標標示符("[]")去訪問元組的資料;

元組內還可以包含元組, 可以通過建立一個元組, 擴充已有的元素;

代碼如下:

# -*- coding: utf-8 -*-        #====================  #File: abop.py  #Author: Wendy  #Date: 2013-12-03  #====================        #eclipse pydev, python3.3        #元組, 表示不能修改的一組的值        zoo = ('python', 'elephant', 'penguin')  print('Number of animals in the zoo is', len(zoo))        new_zoo = ('monkey', 'camel', zoo) #建立元組, 不是修改  print('Number of cages in the new zoo are', len(new_zoo))  print('All animals in new zoo are', new_zoo)  print('Animals brought from old zoo are', new_zoo[2]) #把zoo放入new_zoo的第2個位置(從0開始)  print('Last animal brought from old zoo is', new_zoo[2][2]) #元組包含元組  print('Number of animals in the new zoo is', len(new_zoo)-1+len(new_zoo[2])) #減去當前元素[2]和[2]的長度(3-1+3)

輸出:

Number of animals in the zoo is 3  Number of cages in the new zoo are 3  All animals in new zoo are ('monkey', 'camel', ('python', 'elephant', 'penguin'))  Animals brought from old zoo are ('python', 'elephant', 'penguin')  Last animal brought from old zoo is penguin  Number of animals in the new zoo is 5

返回欄目頁:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

作者:csdn部落格 Spike_King

聯繫我們

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