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'>
 



猜你喜欢

转载自www.cnblogs.com/kite123/p/11639845.html