1. 写一个匿名函数,判断指定的年是否是闰年
yc_year = lambda x: (x % 4 == 0 and x % 100 != 0) or x % 400 == 0
print(yc_year(2008))
2. 写一个函数将一个指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])
(注意:不要使用列表自带的逆序函数)
def yc_sort(list1:list):
"""
#将一个指定的列表中的元素逆序
:param list1:
:return:
"""
length = len(list1)
for index1 in range(0,length-1):
for index2 in range(index1+1,length):
if list1[index2] > list1[index1]:
list1[index1],list1[index2] = list1[index2],list1[index1]
return list1
print(yc_sort([1, 2, 3]))
3. 写一个函数,获取指定列表中指定元素的下标(如果指定元素有多个,
将每个元素的下标都返回) 例如: 列表是:[1, 3, 4, 1] ,元素是1, 返回:0,3
def yc_find(list1:list,item):
length = len(list1)
list2 = []
for index in range(0,length):
if list1[index] == item:
list2.append(index)
list2 = str(list2)[1:-1]
return list2
print(yc_find([1, 3, 4, 1],1))
4. 写一个函数,能够将一个字典中的键值对添加到另外一个字典中
(不使用字典自带的update方法)
def yc_add_dict(sequence1:dict,sequence2:dict):
list1 = []
list2 = []
for key in sequence1:
list1.append(key)
list2.append(sequence1[key])
for key2 in sequence2:
list1.append(key2)
list2.append(sequence2[key2])
return dict(zip(list1,list2))
print(yc_add_dict({'a':1,'b':2},{'3':'c'}))
5. 写一个函数,能够将指定字符串中的所有的小写字母转换成大写字母;
所有的大写字母转换成小写字母(不能使用字符串相关方法)
def yc_change(sequence:str):
str1 = ''
for char in sequence:
if 'A' <= char <= 'Z':
str1 += chr(ord(char)+32)
elif 'a' <= char <= 'z':
str1 += chr(ord(char) -32)
else:
str1 += char
return str1
print(yc_change('abcDEF'))
6. 实现一个属于自己的items方法,可以将自定的字典转换成列表。
列表中的元素是小的列表,里面是key和value (不能使用字典的items方法)
例如:{'a':1, 'b':2} 转换成 [['a', 1], ['b', 2]]
def yc_items(**kwargs):
list1 = []
list2 = []
for key in kwargs:
list1.append(key)
list1.append(kwargs[key])
list2.append(list1)
list1 = []
return list2
print(yc_items(a=1,b=2,c=3))
7. 写一个函数,实现学生的添加功能:
list1 = []
def add_stu_ifo():
global list1
name = input('请输入姓名:')
age = input('请输入年龄:')
tel = input('请输入您的电话号码:')
print('====添加成功')
dict1 = {'name':name,'age':age,'tel':tel}
list1.append(dict1)
return list1
print(add_stu_ifo())