您好,登錄后才能下訂單哦!
在上一篇文章中我們講解了關(guān)于? python列表List? 的相關(guān)內(nèi)容,今天給大家解釋一下列表List的兄弟 – 元組,俗稱: tuple.
?
元組tuple和列表List類似,元組有如下特點(diǎn):
1.由一個(gè)或者多個(gè)數(shù)據(jù)構(gòu)成,數(shù)據(jù)的類型可以不相同也可以相同;
2.元組中的數(shù)據(jù)需要寫在()中括號(hào)內(nèi)部,數(shù)據(jù)與數(shù)據(jù)之間用逗號(hào)隔開;
3.元組是一個(gè)有序的集合,下標(biāo)索引默認(rèn)重 0 開始,和字符串類似;
4.元組的數(shù)據(jù)不能被修改
元組其實(shí)也稱為只讀列表,列表支持的函數(shù)元組同樣也支持,唯一區(qū)別是元組tuple中的數(shù)據(jù)不能被修改,這就意味著不能刪除元組tuple中的數(shù)據(jù),也不能直接給元組tuple中的數(shù)據(jù)賦值。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | # !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:何以解憂 @Blog(個(gè)人博客地址): shuopython.com @WeChat Official Account(微信公眾號(hào)):猿說python @Github:www.github.com ? @File:python_tuple.py @Time:2019/9/26 20:45 ? @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅(jiān)持不懈地積累! """ ? tuple1 = tuple() # 定義一個(gè)空元組,元組的數(shù)據(jù)不能修改,意味永遠(yuǎn)都是一個(gè)空元組 print(tuple1) print(type(tuple1)) # 獲取數(shù)據(jù)類型 ? tuple2 = ("python","study") # 定義元組tuple2 ,該元組由兩個(gè)字符串?dāng)?shù)據(jù)構(gòu)成 print(tuple2) ? tuple3= ("python","s",False,2.5) # 定義元組tuple3 ,該元組中的數(shù)據(jù)可以由不同類型的數(shù)據(jù)構(gòu)成 print(tuple3) |
輸出結(jié)果:
1 2 3 4 | () <class 'tuple'> ('python', 'study') ('python', 's', False, 2.5) |
?
元組tuple的查詢和列表list的操作類似,同樣也可以直接通過下標(biāo)查詢元組中的數(shù)據(jù),演示代碼如下:
1 2 3 4 5 6 7 8 | tuple1= ("python","s",False,2.5,40,"tuple") # 元組中的數(shù)據(jù)可以由不同類型的數(shù)據(jù)構(gòu)成 print(type(tuple1)) # 通過內(nèi)置函數(shù)type獲取數(shù)據(jù)類型 print(tuple1) print(tuple1[1]) # 獲取元組中索引值為1 的數(shù)據(jù) print(tuple1[len(tuple1)-1]) # 獲取元組中的最后一個(gè)元素,注意是len(tuple)-1,并非len(tuple) print(tuple1[1:4]) # 獲取元組索引值1-4的數(shù)據(jù) print(tuple1[:5]) # 如果冒號(hào)之前沒有設(shè)置參數(shù),默認(rèn)重0開始,表示獲取元組索引值0-5的數(shù)據(jù) print(tuple1[3:]) # 如果冒號(hào)之后沒有設(shè)置參數(shù),默認(rèn)到元組最后一個(gè)數(shù)據(jù)介紹,包括最后一個(gè)數(shù)據(jù) |
輸出結(jié)果:
1 2 3 4 5 6 7 | <class 'tuple'> ('python', 's', False, 2.5, 40, 'tuple') s tuple ('s', False, 2.5) ('python', 's', False, 2.5, 40) (2.5, 40, 'tuple') |
?
元組tuple中的數(shù)據(jù)只能讀取,不能修改也不能刪除,如果對(duì)元組tuple中的數(shù)據(jù)刪除或者修改會(huì)報(bào)錯(cuò),代碼演示:
1 2 3 | # 測試修改元組數(shù)據(jù) tuple1= ("python","s",False,2.5,40,"tuple") # 元組中的數(shù)據(jù)可以由不同類型的數(shù)據(jù)構(gòu)成 tuple1[0] = False |
編譯器會(huì)報(bào)錯(cuò):TypeError: ‘tuple’ object does not support item assignment(翻譯:元組tuple不支持修改)
1 2 3 | # 測試刪除元組數(shù)據(jù) tuple1= ("python","s",False,2.5,40,"tuple") # 元組中的數(shù)據(jù)可以由不同類型的數(shù)據(jù)構(gòu)成 del tuple1[0] |
編譯器會(huì)報(bào)錯(cuò):TypeError: ‘tuple’ object doesn’t support item deletion(翻譯:元組tuple不支持刪除)
?
兩者之間直接強(qiáng)制轉(zhuǎn)換即可,演示代碼如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # 元組tuple 轉(zhuǎn)為 列表list tuple1= ("python","s",False,2.5,40,"tuple") list1 = list(tuple1) ? print("tuple1數(shù)據(jù)類型是:",type(tuple1)) print("list1數(shù)據(jù)類型是:",type(list1)) ? print("***"*20) # 小竅門:直接打印60個(gè)* ? # 列表list 轉(zhuǎn)為 元組tuple list2 = [False,"好好學(xué)習(xí)",0,3.14] tuple2 = tuple(list2) print("tuple2數(shù)據(jù)類型是:",type(tuple2)) print("list2數(shù)據(jù)類型是:",type(list2)) |
輸出結(jié)果:
1 2 3 4 5 | tuple1數(shù)據(jù)類型是: <class 'tuple'> list1數(shù)據(jù)類型是: <class 'list'> ************************************************************ tuple2數(shù)據(jù)類型是: <class 'tuple'> list2數(shù)據(jù)類型是: <class 'list'> |
?
1.注意元組tuple與列表list的區(qū)別,元組的數(shù)據(jù)不能被修改,其他使用和列表一樣。
2.注意元組tuple/列表list/字符串str三者的寫法區(qū)別:
1 2 3 | a = "python教程"????# 字符串 b = ["python教程"]??# 列表,列表中只有一個(gè)字符串?dāng)?shù)據(jù) c = ("python教程")??# 元組,元組中只有一個(gè)字符串?dāng)?shù)據(jù) |
?
1.python字符串
2.pycharm設(shè)置字體顏色/模板頭文件
3.python列表list
?
轉(zhuǎn)載請注明:猿說Python???python元組tuple
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。