python元組tuple(14)

在上一篇文章中我們講解了關(guān)于? python列表List? 的相關(guān)內(nèi)容,今天給大家解釋一下列表List的兄弟 – 元組,俗稱: tuple.

在鎮(zhèn)寧等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè) 網(wǎng)站設(shè)計制作定制制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),網(wǎng)絡(luò)營銷推廣,成都外貿(mào)網(wǎng)站建設(shè)公司,鎮(zhèn)寧網(wǎng)站建設(shè)費用合理。

python 元組tuple(14)

?

元組tuple和列表List類似,元組有如下特點:

1.由一個或者多個數(shù)據(jù)構(gòu)成,數(shù)據(jù)的類型可以不相同也可以相同;

2.元組中的數(shù)據(jù)需要寫在()中括號內(nèi)部,數(shù)據(jù)與數(shù)據(jù)之間用逗號隔開;

3.元組是一個有序的集合,下標(biāo)索引默認(rèn)重 0 開始,和字符串類似;

4.元組的數(shù)據(jù)不能被修改

python 元組tuple(14)

元組其實也稱為只讀列表,列表支持的函數(shù)元組同樣也支持,唯一區(qū)別是元組tuple中的數(shù)據(jù)不能被修改,這就意味著不能刪除元組tuple中的數(shù)據(jù),也不能直接給元組tuple中的數(shù)據(jù)賦值。

?

一.元組tuple定義

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(個人博客地址): shuopython.com

@WeChat Official Account(微信公眾號):猿說python

@Github:www.github.com

?

@File:python_tuple.py

@Time:2019/9/26 20:45

?

@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!

"""

?

tuple1=tuple()# 定義一個空元組,元組的數(shù)據(jù)不能修改,意味永遠(yuǎn)都是一個空元組

print(tuple1)

print(type(tuple1))# 獲取數(shù)據(jù)類型

?

tuple2=("python","study")# 定義元組tuple2 ,該元組由兩個字符串?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查詢

元組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])# 獲取元組中的最后一個元素,注意是len(tuple)-1,并非len(tuple)

print(tuple1[1:4])# 獲取元組索引值1-4的數(shù)據(jù)

print(tuple1[:5])# 如果冒號之前沒有設(shè)置參數(shù),默認(rèn)重0開始,表示獲取元組索引值0-5的數(shù)據(jù)

print(tuple1[3:])# 如果冒號之后沒有設(shè)置參數(shù),默認(rèn)到元組最后一個數(shù)據(jù)介紹,包括最后一個數(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ù)

元組tuple中的數(shù)據(jù)只能讀取,不能修改也不能刪除,如果對元組tuple中的數(shù)據(jù)刪除或者修改會報錯,代碼演示:

1

2

3

# 測試修改元組數(shù)據(jù)

tuple1=("python","s",False,2.5,40,"tuple")# 元組中的數(shù)據(jù)可以由不同類型的數(shù)據(jù)構(gòu)成

tuple1[0]=False

編譯器會報錯: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)成

deltuple1[0]

編譯器會報錯:TypeError: ‘tuple’ object doesn’t support item deletion(翻譯:元組tuple不支持刪除)

?

四.元組tuple與列表list的相互轉(zhuǎn)換

兩者之間直接強制轉(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個*

?

# 列表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'>

?

五.重點總結(jié)

1.注意元組tuple與列表list的區(qū)別,元組的數(shù)據(jù)不能被修改,其他使用和列表一樣。

2.注意元組tuple/列表list/字符串str三者的寫法區(qū)別:

1

2

3

a="python教程"????# 字符串

b=["python教程"]??# 列表,列表中只有一個字符串?dāng)?shù)據(jù)

c=("python教程")??# 元組,元組中只有一個字符串?dāng)?shù)據(jù)

?

猜你喜歡:

1.python字符串

2.pycharm設(shè)置字體顏色/模板頭文件

3.python列表list

?

轉(zhuǎn)載請注明:猿說Python???python元組tuple

網(wǎng)頁題目:python元組tuple(14)
URL鏈接:http://bm7419.com/article32/jdejsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、全網(wǎng)營銷推廣、面包屑導(dǎo)航、企業(yè)網(wǎng)站制作網(wǎng)站策劃、Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站