Python每日一练——第21天:元组、字典练习

《100天精通Python》专栏推荐白嫖80g Python全栈视频

《100天精通Python从入门到就业》:本专栏专门针对零基础和需要进阶提升的同学所准备的一套完整教学,从0到100的不断进阶深入,后续还有实战项目,轻松应对面试,专栏订阅地址:https://blog.csdn.net/yuan2019035055/category_11466020.html

  • 优点订阅限时9.9付费专栏进入千人全栈VIP答疑群,作者优先解答机会(代码指导、远程服务),群里大佬众多可以抱团取暖(大厂内推机会)!
  • 专栏福利简历指导、招聘内推、每周送实体书、80G全栈学习视频、300本IT电子书:Python、Java、前端、大数据、数据库、算法、爬虫、数据分析、机器学习、面试题库等等
    在这里插入图片描述
    在这里插入图片描述

1. 牛客运动会

描述: 又到了一年一度的牛客运动会,Tom和Andy报名参加了项目,
但由于比赛前一天,Andy喝了太多碳酸饮料,导致身体不适,所以临时让Allen上场了,
换人参赛需要修改参赛名单,请完成以下内容模拟整个过程。

请创建一个依次包含字符串’Tom’和’Andy’的元组my_tuple,
先使用print()语句一行打印字符串’Here is the original tuple:‘,再使用for循环将元组my_tuple的内容打印出来;

请使用try-except代码块执行语句my_tuple[1] = ‘Allen’,
若引发TypeError错误,先输出一个换行,再使用print()语句一行打印字符串"my_tuple[1] = ‘Allen’ cause cause a TypeError: ‘tuple’ object does not support item assignment";

再重新对my_tuple赋值一个新元组,新元组依次由字符串’Tom’和’Allen’构成。
输出一个换行,先使用print()语句一行打印字符串’The tuple was changed to:’,再使用for循环将元组my_tuple的内容打印出来,确定修改无误。

输入描述:无

输出描述:按题目描述进行输出即可(注意前后两个输出部分需以一个空行进行分隔)。
Here is the original tuple:
Tom
Andy

my_tuple[1] = ‘Allen’ cause a TypeError: ‘tuple’ object does not support item assignment

my_tuple was changed to:
Tom
Allen

实现代码:

my_tuple=('Tom', 'Andy')
print('Here is the original tuple:')
for x in my_tuple:
    print(x)
    
try:
    my_tuple[1]='Allen'
except TypeError:
    print()
    print("my_tuple[1] = 'Allen' cause a TypeError: 'tuple' object does not support item assignment")

my_tuple=('Tom', 'Allen')
print()
print('my_tuple was changed to:')
for x in my_tuple:
    print(x)

运行结果:

Here is the original tuple:
Tom
Andy

my_tuple[1] = 'Allen' cause a TypeError: 'tuple' object does not support item assignment

my_tuple was changed to:
Tom
Allen

2. 遍历字典

描述: 创建一个依次包含键-值对’<‘: ‘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 k,v in sorted(operators_dict.items()):
    print(f'Operators {k} means {v}')
operators_dict['>']='greater than'
print()
print('The dict was changed to:')

for k,v in sorted(operators_dict.items()):
    print(f'Operators {k} means {v}')

运行结果:

Here is the original dict:
Operators < means less than
Operators == means equal

The dict was changed to:
Operators < means less than
Operators == means equal
Operators > means greater than

3. 毕业生就业调查

描述: 又到了毕业季,牛牛作为牛客大学的学生会主席,决定对本校的应届毕业生进行就业调查。
他创建了一个依次包含字符串’Niumei’、‘Niu Ke Le’、‘GURR’和’LOLO’的列表survey_list,作为调查名单;
又创建了一个依次包含键-值对’Niumei’: ‘Nowcoder’和’GURR’: 'HUAWEI’的字典result_dict,作为已记录的调查结果。
请遍历列表survey_list,如果遍历到的名字已出现在 包含字典result_dict的全部键的列表 里,
则使用print()语句一行输出类似字符串’Hi, Niumei! Thank you for participating in our graduation survey!'的语句以表达感谢,
否则使用print()语句一行输出类似字符串’Hi, Niu Ke Le! Could you take part in our graduation survey?'的语句以发出调查邀请。

输入描述:无

输出描述:按题目描述进行输出即可。
Hi, Niumei! Thank you for participating in our graduation survey!
Hi, Niu Ke Le! Could you take part in our graduation survey?
Hi, GURR! Thank you for participating in our graduation survey!
Hi, LOLO! Could you take part in our graduation survey?

实现代码:

survey_list = ['Niumei', 'Niu Ke Le', 'GURR', 'LOLO']
result_dict = {'Niumei': 'Nowcoder', 'GURR': 'HUAWEI'}

for i in survey_list:
    if i in result_dict.keys():
        print(f'Hi, {i}! Thank you for participating in our graduation survey!')
    else:
        print(f'Hi, {i}! Could you take part in our graduation survey?')

运行结果:

Hi, Niumei! Thank you for participating in our graduation survey!
Hi, Niu Ke Le! Could you take part in our graduation survey?
Hi, GURR! Thank you for participating in our graduation survey!
Hi, LOLO! Could you take part in our graduation survey?

4. 姓名与学号

描述
创建一个依次包含键-值对{‘name’: ‘Niuniu’和’Student ID’: 1}的字典my_dict_1,
创建一个依次包含键-值对{‘name’: ‘Niumei’和’Student ID’: 2}的字典my_dict_2,
创建一个依次包含键-值对{‘name’: ‘Niu Ke Le’和’Student ID’: 3}的字典my_dict_3,
创建一个空列表dict_list,使用append()方法依次将字典my_dict_1、my_dict_2和my_dict_3添加到dict_list里,
使用for循环遍历dict_list,对于遍历到的字典,使用print()语句一行输出类似字符串"Niuniu’s student id is 1."的语句以打印对应字典中的内容。

输入描述:无

输出描述:按照题意输出
Niuniu’s student id is 1.
Niumei’s student id is 2.
Niu Ke Le’s student id is 3.

代码实现:

my_dict_1 = {'name': 'Niuniu', 'Student ID': 1}
my_dict_2 = {'name': 'Niumei', 'Student ID': 2}
my_dict_3 = {'name': 'Niu Ke Le', 'Student ID': 3}
dict_list = []
dict_list.append(my_dict_1)
dict_list.append(my_dict_2)
dict_list.append(my_dict_3)

for i in dict_list:
    # 字典获取元素的方法i['key值'],或者i.get('key值')
    print(f"{i['name']}'s student id is {i.get('Student ID')}.")

运行结果:

Niuniu's student id is 1.
Niumei's student id is 2.
Niu Ke Le's student id is 3.
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

袁袁袁袁满

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值