遍历字典(python练习)

创建一个依次包含键-值对'<': 'less than'和'==': 'equal'的字典operators_dict,

先使用print()语句一行打印字符串'Here is the original dict:',

再使用for循环遍历 已使用sorted()函数按升序进行临时排序的包含字典operators_dict的所有键的列表,使用print()语句一行输出类似字符串'Operator < means less than.'的语句;

对字典operators_dict增加键-值对'>': 'greater than'后,

输出一个换行,再使用print()语句一行打印字符串'The dict was changed to:',

再次使用for循环遍历 已使用sorted()函数按升序进行临时排序的包含字典operators_dict的所有键的列表,使用print()语句一行输出类似字符串'Operator < means less than.'的语句,确认字典operators_dict确实新增了一对键-值对。

输入描述:

输出描述:

按题目描述进行输出即可(注意前后两个输出部分需以一个空行进行分隔)。

operators_dict={'<':'less than','==':'equal'}
print('Here is the original dict:')
for item in sorted(operators_dict):
    print("Operator {name1} means {name2}.".format(name1=item,name2=operators_dict[item]))
operators_dict.setdefault('>','greater than')
print()
print('The dict was changed to:')
for item in sorted(operators_dict):
    print("Operator {name1} means {name2}.".format(name1=item,name2=operators_dict[item]))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值