#tuple元组
tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
#一般写元组的时候,推荐在最后加一个,
#元素不可修改,不能添加或删除
#1.索引
v = tu[1]
print(v)
#2.切片
v = tu[1:5]
print(v)
#3.可以for循环.可迭代对象
for item in tu:
print(item)
#转换
s = "xoajsfjfq"
li = ["alex","xiamio"]
tu = ("asffg","sahjjs",)
#字符串转换元组
v = tuple(s)
print(v)
#列表转换元组
m = tuple(li)
print(m)
#元组转换为列表
li1 = list(tu)
print(li1)
#元组转换为字符串:元组中只包含字符串时
s = ""
v = "".join(tu)
print(v)
#列表扩展加入元组
li = ["alex","xiamio"]
tu = ("asffg","sahjjs",)
li.extend(tu)
print(li)
#6.元组的一级元素不可修改/删除/增加
tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
#元组,有序
v = tu[3]#得到列表
tu[3][0]=567
print(tu)#(111, 'alex', (11, 22), [567], True, 33, 44)
#字典dict
#字典value值可以为任何类型
# info={
# "name":"小明",
# "age":18,
# "爱好":["爬山","打篮球","打游戏"],
# "毕业时间":(1992.26,)
# }
#布尔值(在有0或1时),列表,字典不能作为字典的key
# info={
# "name":"小明",
# "age":18,
# [11,22,33]:["爬山","打篮球","打游戏"],
# {"ka":12}:(1992.26,)
# }
#
# print(info)
#字典是无序的
#print(info)#每次打印出来的顺序都同的
#索引方式找到指定元素
# info={
# "name":"小明",
# "age":18,
# "爱好":["爬山","打篮球","打游戏"],
# "毕业时间":(1992.26,)
# }
#
# v = info["name"]
# print(v)
# v = info["爱好"][2]
# print(v)
#
# for item in info.keys():#for循环打印字典key的值
# print(item)
#
# for item in info.values():#for循环打印字典value的值
# print(item)
# for k,v in info.items():#for循环打印字典keys和values的值
# print(k,v)
#字典支持del方式索引删除
# info={
# "name":"小明",
# "age":18,
# "爱好":["爬山","打篮球","打游戏"],
# "毕业时间":(1992.26,)
# }
# del info["age"]
# print(info)
# del info["爱好"][1]
# print(info)
#=================字典函数方法===============
info={
"name":"小明",
"age":18,
"爱好":["爬山","打篮球","打游戏"],
"毕业时间":(1992.26,)
}
#1.根据序列,创建字典,并指定统一值
v = dict.fromkeys(["k1",1234,"5654"],"小明")
print(v)#{'k1': '小明', 1234: '小明', '5654': '小明'}
#2.根据key获取值,key不存在时,可以指定默认值
v = info.get("name",56)
print(v)#小明
v = info.get("name1",56)
print(v)#56
#3.删除字典数据,key不存在时,返回指定值
v = info.pop("name",6565)
print(v)#小明
v = info.pop("name2",6565)
print(v)6565
v = info.popitem()#随机删除字典中一个键值对,并返回这个键值对
print(v)
#4.设置值
#已存在.不设置,获取当前key值对应的值
#不存在,设置,获取当前key对应值
v = info.setdefault("name","小丽")
print(v)#小丽
#5.更新#
dic={
"k1":12,
"k2":23
}
dic.update({"k1":22,"k2":33,"k3":44})
#dic.update("k1"=22,"k2"=33,"k3"=44)
print(dic)#{'k1': 22, 'k2': 33, 'k3': 44}