python之元組索引,切片,添加,刪除,修改


# 什么時候用元組?  ---操作數據庫時存放,不支持任何修改(增刪改)
注意1# 元組中的子元素為列表,可以對列表的子元素修改
m = (1, 0.02,'hello',[1,2,3],True)
m[3][2] = 'nihao'
print(m)  # (1, 0.02, 'hello', [1, 2, 'nihao'], True)
#元組的格式: 元組 tuple 符號()
# 1.空元組
c= ()
# 2.元組中可以包含任何類型的數據
# 3.元組中的元素 根據逗號分割
a = (1, 0.02,'hello',[1,2,3],True)
# 4.元組里的元素,也是有索引值的,從0開始
# 5.獲取元組里的單個值:元組[索引值]
print(a[-1])  # True
print(a[2])  # hello
# 6.元組的切片 同字符串,列表的操作 元組名[索引頭:索引尾:步長]
print(a[::2])  # [1, 'hello', True]
print(a[0:5:2])   # [1, 'hello', True]
# 注意2:元組里面只有一個元素,需要加逗號
x = ('hello')
print(type(x))  # <class 'str'>
y = (1)
print(type(y))  # <class 'int'>
z =(1,)
print(type(z))  # <class 'tuple'>

 

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM