自学python---字典

=字典=====

1.简介

Python 内置了字典:dict 的支持,dict 全称 dictionary,在其他语言中也称为 map,使用键-值(key-value)存储,
具有极快的查找速度

2.定义字典

	student = {"name":"admin","age":22}

3.字典基本操作

#===========字典================
#创建
student = {"name":"admin","age":22}
people= {"n":"root"}

#添加
student['address']="北京市"
#print(student)

#修改
student['name']="tom"
#print(student)

#获取
#print(student["name"])
#print(student.get("namea")) # key不存在,则返回None 
#print(student.get("name","默认值"))# 没有,不返回None ,则返回默认值

#删除
#print(student.pop("age"))  # 没有对应key报错,有则返回 key对应的 值
#print(student.popitem())  # 随机删除其中一项
#print(student.clear())
#del student["name"]
#print(student)

#遍历   keys() 返回所有的建
#for key in student.keys():
	# print(key,stunt.get(key))

#for key ,value in student.items():
	# print(key,value)


#获取所有的值
#print(student.values())	
student.update(people)
print(student)	

4.字典内置函数&方法

在这里插入图片描述
python字典包含了以下内置方法:
在这里插入图片描述

5.字典特点:

1:查找和插入的速度极快,不会随着 key 的增加而增加
 
2:浪费空间
 
3:key不可以重复,且不可变
 
4:数据无序排放

5 字典可以必须是不可以变的对象 ,( str   number 元组)

猜你喜欢

转载自blog.csdn.net/weixin_47580822/article/details/113666424