python报错 scrolled: false_python 元组tuple - python基础入门(14)
在上一篇文章中我們講解了關(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ù)賦值。
一.元組tuple定義
# !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é)果:
() <class 'tuple'> ('python', 'study') ('python', 's', False, 2.5)二.元組tuple查詢
元組tuple的查詢和列表list的操作類似,同樣也可以直接通過下標(biāo)查詢?cè)M中的數(shù)據(jù),演示代碼如下:
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é)果:
<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ù)
元組tuple中的數(shù)據(jù)只能讀取,不能修改也不能刪除,如果對(duì)元組tuple中的數(shù)據(jù)刪除或者修改會(huì)報(bào)錯(cuò),代碼演示:
# 測(cè)試修改元組數(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不支持修改)
# 測(cè)試刪除元組數(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不支持刪除)
四.元組tuple與列表list的相互轉(zhuǎn)換
兩者之間直接強(qiáng)制轉(zhuǎn)換即可,演示代碼如下:
# 元組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é)果:
tuple1數(shù)據(jù)類型是: <class 'tuple'> list1數(shù)據(jù)類型是: <class 'list'> ************************************************************ tuple2數(shù)據(jù)類型是: <class 'tuple'> list2數(shù)據(jù)類型是: <class 'list'>五.重點(diǎn)總結(jié)
1.注意元組tuple與列表list的區(qū)別,元組的數(shù)據(jù)不能被修改,其他使用和列表一樣。
2.注意元組tuple/列表list/字符串str三者的寫法區(qū)別:
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)載請(qǐng)注明:猿說Python ? python元組tuple
想了解更多python內(nèi)容請(qǐng)直接搜索微信公眾號(hào):猿說python
Python教程 - 猿說Python?www.shuopython.com本人也還在學(xué)習(xí)python中,博客會(huì)持續(xù)更新ing,有興趣的小伙伴關(guān)注走一波,推薦瀏覽個(gè)人博客網(wǎng)站:猿說python,文章采用樹狀分類,結(jié)構(gòu)目錄清晰一點(diǎn),文章內(nèi)容有問題的話歡迎給出建議或者直接留言.
總結(jié)
以上是生活随笔為你收集整理的python报错 scrolled: false_python 元组tuple - python基础入门(14)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 修复表 阿里云_MySql数
- 下一篇: python maketrans_Pyt