打赏

python元组

python元组

1.定义

在一堆小括号中的数据,不同元素使用逗号隔开,列表可变,元组不可变。

tuple_a = ("nginx","httpd","tomcat")
print(type(tuple_a))

# 元组数据类型可以任意
tuple_b = (10,3.14,"nginx",["redis","monogodb"],("mysql","oracle"))
print(type(tuple_b))

# 单个元素的元组
tuple_c = ("nginx",)   # 注意元素后面的逗号,不可少
print(type(tuple_c))

2.元组的常规操作

len() in , not in

2.1 索引
tuple_a = ("nginx","httpd","iis")
print(tuple_a[1])
tuple_a[1] = "tomcat"  # 错误:元组是不可变的

# 元组
data = ("nginx","httpd",["redis","mongodb"])
data[-1][0] = "memcached"
print(data)
posted @ 2021-11-13 11:02  苍山落暮  阅读(47)  评论(0编辑  收藏  举报