Python列表和元組的定義與使用操作示例
本文實例講述了Python列表和元組的定義與使用操作。分享給大家供大家參考,具體如下:
#coding=utf8 print ''''' 可以將列表和元組當(dāng)成普通的“數(shù)組”,它能保存任意數(shù)量任意類型的Python對象。 列表和元組通過數(shù)字索引來訪問元素(從0開始)。 列表和元組的區(qū)別: ------------------------------------------------------------------------------------ 元組 \ 列表 ------------------------------------------------------------------------------------ 列表元素用中括號[] \ 元組元素用小括號() 元素的個數(shù)及元素的值可以改變 \ 元素的個數(shù)及元素的值不可改變 ------------------------------------------------------------------------------------ 元組可以看出只讀的列表。 列表和元組可以通過使用索引運算符([])和切片運算符([:])可以得到子集 ''' NumberList=[1,2,3,4,5,6,7,8.9,0101,017,0xab] StringList=['hello',"hello world",'''''goddness'''] MixList=[12,13.2,01,'abc','hello'] NumberTouple=(1,2,3,4,5,6,7,8.9,0101,017,0xab) StringTouple=('hello',"hello world",'''''goddness''') MixTouple=(12,13.2,01,'abc','hello') print "output the element of the NumberList by index--------->",NumberList[0],NumberList[1],NumberList[2],NumberList[-1] print "output the element of the StringList by index--------->",StringList[0],StringList[1],StringList[2],StringList[-1] print "output the element of the MixList by index--------->",MixList[0],MixList[1],MixList[2],MixList[-1] print "output the element of the NumberTouple by index--------->",NumberTouple[0],NumberTouple[1],NumberTouple[2],NumberTouple[-1] print "output the element of the StringTouple by index--------->",StringTouple[0],StringTouple[1],StringTouple[2],StringTouple[-1] print "output the element of the MixTouple by index--------->",MixTouple[0],MixTouple[1],MixTouple[2],MixTouple[-1] print "output the element of the NumberList by slice--------->",NumberList[0:2],NumberList[1:3],NumberList[0:],NumberList[:-1] print "output the element of the StringList by slice--------->",StringList[0:1],StringList[2:3],StringList[0:],StringList[:-1] print "output the element of the MixList by slice--------->",MixList[0:],MixList[:1],MixList[0:2],MixList[2:-1] print "output the element of the NumberTouple by slice--------->",NumberTouple[0:2],NumberTouple[1:3],NumberTouple[2:],NumberTouple[:-1] print "output the element of the StringTouple by slice--------->",StringTouple[0:2],StringTouple[1:3],StringTouple[2],StringTouple[-1] print "output the element of the MixTouple by slice--------->",MixTouple[0:],MixTouple[1:3],MixTouple[2],MixTouple[:-1] NumberList[0]=59 #NumberTouple[0]=56 print "Change the value of NumberList[0] to 59------------",NumberList[0] #print "Can not change the value of NumberTouple[0] to 56------------",NumberTouple[0]
運行結(jié)果:
更多Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python列表(list)操作技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
關(guān)于python爬蟲應(yīng)用urllib庫作用分析
這篇文章主要介紹了關(guān)于python爬蟲應(yīng)用urllib庫作用分析,想要進(jìn)行python爬蟲首先我們需要先將網(wǎng)頁上面的信息給獲取下來,這就是utllib庫的作用,有需要的朋友可以借鑒參考下2021-09-09Pycharm-community-2020.2.3 社區(qū)版安裝教程圖文詳解
這篇文章主要介紹了Pycharm-community-2020.2.3 社區(qū)版安裝教程圖文詳解,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12詳解Python中range()與xrange()的區(qū)別
range()?和?xrange()?是兩個函數(shù),可用于在?Python的?for?循環(huán)中迭代一定次數(shù)。本文將通過示例詳細(xì)說說二者的區(qū)別與使用,需要的可以參考一下2022-09-09Tensorflow的可視化工具Tensorboard的初步使用詳解
這篇文章主要介紹了Tensorflow的可視化工具Tensorboard的初步使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02python3學(xué)習(xí)筆記之多進(jìn)程分布式小例子
本篇文章主要介紹了python3學(xué)習(xí)筆記之多進(jìn)程分布式小例子,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02PIL對上傳到Django的圖片進(jìn)行處理并保存的實例
今天小編就為大家分享一篇PIL對上傳到Django的圖片進(jìn)行處理并保存的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08用實例詳解Python中的Django框架中prefetch_related()函數(shù)對數(shù)據(jù)庫查詢的優(yōu)化
這篇文章主要介紹了用實例詳解Python中的Django框架中prefetch_related()函數(shù)對數(shù)據(jù)庫查詢的優(yōu)化,可減少對數(shù)據(jù)庫的查詢次數(shù)從而優(yōu)化性能,需要的朋友可以參考下2015-04-04