日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python元组的方法_Python元组及其方法

發(fā)布時間:2024/10/14 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python元组的方法_Python元组及其方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

"""

tuple;

其實質(zhì)是一個有序集合。

特點:

1、與列表相似

2、一旦初始化則不能改變

3、使用小括號()

"""

# 創(chuàng)建一個空元組;

tuple1 = ()

print(tuple1)

# 元組中的元素可以為不同類型:

tuple2 = (1, 2, 3, "Hellow", 'W')

print(tuple2)

# 定義一個只有一個元素的元組: 一個元素的后面必須有一個逗號,要不就成為整型。

tuple3 = (1,)

print(tuple3)

print(type(tuple3))

# 元組元素的訪問: tuple[下標]

tuple4 = (1,2,3,4,5)

print(tuple4[1])

print(tuple4[-1]) # 下標為 -1 是最后一個元素的下標 倒數(shù)第一個下標 : -1 -2 -3 -4 -5

print(tuple4[-3])

# 修改元組:

tuple5 = (1,2,3,[4,5,6])

# tuple5[2] = 100 錯誤,元組元素不能改變

# 但是元組中的列表的元素可以改變

tuple5[3][2] = 300

tuple5[3].append(7)

print(tuple5)

# 刪除元組:

tuple6 = (1,2,3,4,'Good')

# del tuple6[*] 錯誤

del tuple6

# print(tuple6) 錯誤此元組被刪除 Not defined!

# 元組的操作:

tuple7 = (1, 2, 3)

tuple8 = (4, 5, 6)

print(tuple7 + tuple8)

print(tuple7 * 2)

# 判斷元素是否在元組中; in / not in 成員關(guān)系

tuple9 = (1,2,3,4)

print(4 in tuple9)

# 元組的截取:

# tuple1[開始下標:結(jié)束下標]

# 截取從開始下標開始到結(jié)束下標之前。前閉后開

tuple10 = (1, 2, 3, 4, 5, 6, 7, 8)

print(tuple10[1:4])

print(tuple10[1:])

print(tuple10[:4])

print(tuple10[:])

# 二維元組:

tuple11 = ((1,2,3),(4,5,6),(6,7,8))

print(tuple11[1][0:1])

# 元組的方法:

# len() 返回元組中元素的個數(shù) ;與長度計算算有關(guān) string, set, list, tuple, dict等

tuple12 = (1,2,3,4,5)

print(len(tuple12))

# max() 返回元組中最大值 min () 返回元組中最小值:

tuple13 = (1,2,3,4,8)

print(min(tuple13), max(tuple13))

# 把列表轉(zhuǎn)化為元組:

list1 = [1,2,3,4]

tuple14 = tuple(list1)

print(tuple14)

# 元組的遍歷:

tuple15 = (1,2,3,4,5)

for i in tuple15:

print(i)

# 其他方法 所有方法可以.后Ctrl+Q、[tab]查看

tuple15.count(2)

tuple15.index(2, 2, )

output:

()

(1, 2, 3, 'Hellow', 'W')

(1,)

2

5

3

(1, 2, 3, [4, 5, 300, 7])

(1, 2, 3, 4, 5, 6)

(1, 2, 3, 1, 2, 3)

True

(2, 3, 4)

(2, 3, 4, 5, 6, 7, 8)

(1, 2, 3, 4)

(1, 2, 3, 4, 5, 6, 7, 8)

(4,)

5

1 8

(1, 2, 3, 4)

1

2

3

4

5

Process finished with exit code 0

總結(jié)

以上是生活随笔為你收集整理的python元组的方法_Python元组及其方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。