(111,)
tuple
# 定義
l=(11,11.11,"aaa",[222,333]) #背後調用 l=tuple(...)
# 定義單個值需要加逗號(沒加就只是包含的意思)
x = (18)
y = (18,)
print(type(x)) #<class 'int'>
print(type(y)) #<class 'tuple'>
# 一個元組
j=(1,) #加上逗號
print(j,type(j))
(1,) <class 'tuple'>
# 元組的不可變類型解析
tup = (111,"aaa",[222,333])
print(id(tup[0]),id(tup[1]),id(tup[2]))
#140709693587136 2763387504816 2763386278472
tup[0] = 222 #報錯
tup[1] = 333 #報錯
tup[2] = 333 #報錯
# 當改變子列表內元素時
tup[2][0] = 333
print(tup[2]) #[333, 333]
# 修改完成後查看, 元組第一層元素的"id"沒有改變
print(id(tup[0]),id(tup[1]),id(tup[2]))
#140709693587136 2763387504816 2763386278472
tup = (111,222,333,444,555)
# 正向取
print(t[0]) #111
# 反向取
print(t[-1]) #555
tup = (111,222,333,444,555,666)
# 顧頭不顧尾
print(t[0:3]) # (111,222,333)
# 反轉
tup2 = tup[::-1]
print(tup2) #(666,555,444,333,222,111)
tup = (111,222,333,444,555,666)
print(len(tup)) # 6
tup = (111,"aaa",[222,333])
print(111 in tup) #True
print(222 in tup) #False
print([222,333] in tup) #True
tup = (111,"aaa",[222,333])
for i in tup:
print(i)
# 111
# aaa
# [222, 333]
tup = (111,"aaa",222,222,[222,333])
print(tup.count(222)) # 2
tup = (111,"aaa",222[222,333])
print(tup.index("aaa",1,3)) # 1
只是在第一層查找
tup = (111,"aaa",222[222,333])
print(tup.index("aaa",1,3)) # 1