编程小白Python字典学习小结

本文介绍了Python字典的概念,包括其定义、表示方式以及键和值的数据类型。接着详细阐述了如何访问、添加、修改和删除字典中的键值对,并展示了如何判断键是否存在。此外,还讨论了字典的常用方法如clear(),get(),update(),items(),keys(),values(),pop()和popitem(),并演示了它们的使用。最后,文章提到了字典的遍历循环,包括遍历键、值和键值对的方法。
摘要由CSDN通过智能技术生成

一、Python字典介绍

1、字典简单含义

字典就是用大括号括起来的键值对,且字典中的键不能重复,值就相当于是键的属性,每个键都可以映射出一个值。
键和值通过毛好了链接,不同键值对通过逗号隔开。
键值对之间没有顺序且不能重复。

2、字典的基本表示方法

如果键key或值vlaue是字符串则需要用单引号括起来(‘key’:‘value’)

x = {key①:value①, key②:value②, key③:value③, ......}   #字典里面的元素格式 '键':值

3、字典key和value的数据类型

字典中键key的数据类型要求其不可变,比如元组,字符串等,而列表是可变数据类型因此列表不能作字典中的键;而字典中的值value可以是任意数据类型。

二、字典的基本用法

1、访问字典的值

通过key来访问value

student = {'班长':'张三', '副班':'李四', '学委':'王五', '体委':'小红'}
print(student['班长'])

输出结果为:

张三

2、添加键值对

通key添加键值对

student = {'班长':'张三', '副班':'李四', '学委':'王五', '体委':'小红'}
student['团支书'] = '小明'
print(student)

输出结果为:

{'班长': '张三', '副班': '李四', '学委': '王五', '体委': '小红', '团支书': '小明'}

3、修改字典的值

通key修改键值对

student = {'班长':'张三', '副班':'李四', '学委':'王五', '体委':'小红'}
student['班长'] = '阿山'
print(student)

输出结果为:

{'班长': '阿山', '副班': '李四', '学委': '王五', '体委': '小红'}

4、删除字典中的键值对

通del key删除键值对

student = {'班长':'张三', '副班':'李四', '学委':'王五', '体委':'小红'}
del student['班长']
print(student)

输出结果为:

{'副班': '李四', '学委': '王五', '体委': '小红'}

5 、判断键值对是否存在

通过in或not in运算符判断指定的键key是否在字典当中

student = {'班长':'张三', '副班':'李四', '学委':'王五', '体委':'小红'}
print('班长' in student)
print('团支书' not in student)

输出结果为:

True
True

6、创建空列表

x = dict()
print(x)
print(type(x))

输出结果为:

{}
<class 'dict'>

x = {}
print(x)
print(type(x))

输出结果为:

{}
<class 'dict'>

三、字典的常用方法

1、常用方法

①clear()方法

student = {'班长':'张三', '副班':'李四', '学委':'王五', '体委':'小红'}
student.clear()
print(student)

输出结果为:

{}

②get()方法

student = {'班长':'张三', '副班':'李四', '学委':'王五', '体委':'小红'}
print(student.get('团支书'))

输出结果为:

None   #避免了报错,代码可继续执行

如果直接print(student[‘团支书’])会出现报错

③update()方法

update方法会更新已有的键值对,会添加没有的键值对。

student = {'班长':'张三', '副班':'李四', '学委':'王五', '体委':'小红'}
student.update({'班长':'阿山', '团支书':'小明'})
print(student)

输出结果为:

{'班长': '阿山', '副班': '李四', '学委': '王五', '体委': '小红', '团支书': '小明'}

④items()方法

items ()方法返回值是一个dict_items类型的可遍历的数据

student = {'班长':'张三', '副班':'李四', '学委':'王五', '体委':'小红'}
x = student.items()
for i in x:    #证明student.items()的返回值是可遍历的
    print(i)
print(type(x))   #查看返回值的数据类型

输出结果为:

('班长', '张三')
('副班', '李四')
('学委', '王五')
('体委', '小红')
<class 'dict_items'>  #返回值的数据类型

⑤keys()方法和values()方法

用keys()方法返回字典中所有的键

student = {'班长':'张三', '副班':'李四', '学委':'王五', '体委':'小红'}
x = student.keys()
print(type(x))  #查看返回值的数据类型
print(x)

输出结果为:

<class 'dict_keys'>
dict_keys(['班长', '副班', '学委', '体委'])

keys()方法的返回值也是可遍历的

student = {'班长':'张三', '副班':'李四', '学委':'王五', '体委':'小红'}
x = student.keys()
for i in x:
    print(i, end='  ')

输出结果为:

班长  副班  学委  体委

values()方法与keys()方法用法相同

⑥pop()方法

pop()方法用于获取指定键 key 对应的 值value,并删除这个键值对

student = {'班长':'张三', '副班':'李四', '学委':'王五', '体委':'小红'}
x = student.pop('班长')
print(student)
print(x)

输出结果为:

{'副班': '李四', '学委': '王五', '体委': '小红'}
张三

⑦popitem()方法

popitem() 方法用于弹出字典中最后一个 键值对

student = {'班长':'张三', '副班':'李四', '学委':'王五', '体委':'小红'}
x = student.popitem()
print(x)

输出结果为:

('体委', '小红')

⑧len()方法

len()方法用于计算字典键值对的数量

student = {'班长':'张三', '副班':'李四', '学委':'王五', '体委':'小红'}
print(len(student))

输出结果为:

4

2、字典的遍历循环

①遍历 键key

student = {'班长':'张三', '副班':'李四', '学委':'王五', '体委':'小红'}
for key in student:
    print(key)

输出结果为:

班长
副班
学委
体委

②遍历 值value

student = {'班长':'张三', '副班':'李四', '学委':'王五', '体委':'小红'}
for value in student.values():
    print(value)

输出结果为:

张三
李四
王五
小红

③遍历字典键值对

student = {'班长':'张三', '副班':'李四', '学委':'王五', '体委':'小红'}
for key in student:
    print(key + ':' + str(student[key]))

输出结果为:

班长:张三
副班:李四
学委:王五
体委:小红

以上是今天的学习成果和学习小结,关注小白带你玩转Python。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值