1. 写一个匿名函数,判断指定的年是否是闰年
leap = lambda year: True if year % 400 == 0 or year % 4 == 0 and year % 100 != 0 else False
2. 写一个函数将一个指定的列表中的元素逆序(如[1, 2, 3] -> [3, 2, 1])(注意:不要使用列表自带的逆序函数)
def runaround(old_list: list):
for index in range(len(old_list) // 2):
old_list[index], old_list[-index - 1] = old_list[-index - 1], old_list[index]
return old_list
3. 写一个函数,获取指定列表中指定元素的下标(如果指定元素有多个,将每个元素的下标都返回)
例如: 列表是:[1, 3, 4, 1], 元素是1, 返回: 0, 3
def find_value(one_list: list, value):
for index in range(len(one_list)):
if one_list[index] == value:
print(index, end=' ')
4. 写一个函数,能够将一个字典中的键值对添加到另外一个字典中(不使用字典自带的update方法)
def dict_move(dict_1: dict, dict_2: dict):
for key_1 in dict_1:
dict_2[key_1] = dict_1[key_1]
return dict_2
5. 写一个函数,能够将指定字符串中的所有的小写字母转换成大写字母;所有的大写字母转换成小写字母(不能使用字符串相关方法)
def up_low(old_str: str):
new_str = ''
for item in old_str:
if 'A' <= item <= 'Z':
new_str += chr(ord(item) + 32)
elif 'a' <= item <= 'z':
new_str += chr(ord(item) - 32)
else:
new_str += item
return new_str
6. 实现一个属于自己的items方法,可以将自定的字典转换成列表。列表中的元素是小的列表,里面是key和value(不能使用字典的items方法)
def items_(dict_: dict):
key_value = []
for key in dict_:
key_value.append([key, dict_[key]])
return key_value
7. 写一个函数,实现学生的添加功能:
students = []
def add_stu():
while True:
name = input('输入学生姓名:')
age = input('输入学生年龄:')
tel = input('输入学生电话:')
stu_id_1 = str(len(students) + 1)
stu_id_2 = stu_id_1.rjust(4, '0')
student = {'name': name, '年龄': age, '电话': tel, '学号': stu_id_2}
students.append(student)
print('添加成功!')
print(students)
chose = input('1.继续\n2.返回\n请选择:')
if chose == '1':
continue
elif chose == '2':
break
else:
break
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。