元組是存放任意元素集合,不能修改其內容;
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