Python学习(元组、字典)
若元组中只有一个元素,要在最后一个元素后面加上逗号
元组的定义
tup1=("abc","def",2000,2020)
访问元组
(1)直接访问下标
tup1=("abc","def",2000,2020,333,444,555,666)
print(tup1[0])
print(tup1[-1])
print(tup1[1:5])
增添元组中的元素
tup1=(12,23,45)
tup2=("abc","xyz")
tup=tup1+tup2
print(tup)
字典的定义
info={"name":"吴彦祖","age":18}
字典的访问
print(info["name"])
print(info["age"])
print(info.get("gender"))
print(info.get("gender","m"))
增
info={"name":"吴彦祖","age":18}
newId=input("请输入新的学号")
info["Id"]=newId
print(info["ID"])
删
info={"name":"吴彦祖","age":18}
print("删除前:%s"%info["name"])
del info["name"]
info={"name":"吴彦祖","age":18}
print("删除前:%s"%info)
info.clear()
print("清空后:%s"%info)
改
info={"name":"吴彦祖","age":18}
info["age"]=20
print(info["age"])
查
info={"id":1,"name":"吴彦祖","age":18}
print(info.keys())
print(info.values())
print(info.items())
for key in info.keys():
print(key)
for value in values():
print(value)
for key,value in info.items():
print("key=%s,value=%s"%(key,value))
使用枚举函数输出列表中元素的下标和内容
mylist=["a","b","c","d"]
for i,x in enumerate(mylist):
print(i,x)
四种数据结构对比
本篇文章资源来自于B站网课视频!
|