1. 寫一個匿名函數(shù),判斷指定的年是否是閏年
leap = lambda y: (y % 400 == 0)or(y % 4 == 0 and y % 100 != 0)
2. 寫一個函數(shù)將一個指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使用列表自帶的逆序函數(shù))
z_reverse = lambda list1:list1[::-1]
3. 寫一個函數(shù),獲取指定列表中指定元素的下標(如果指定元素有多個,將每個元素的下標都返回)
例如: 列表是:[1, 3, 4, 1] ,元素是1, 返回:0,3
def z_index(list1:list, item):
"""說明:獲取列表list1中所有item的下標"""
count = 0
index_list = []
for item1 in list1:
if item1 == item:
index_list.append(count)
count +=1
return str(index_list)[1:-1]
print(z_index([1, 3, 4, 1], 1))
4. 寫一個函數(shù),能夠?qū)⒁粋€字典中的鍵值對添加到另外一個字典中(不使用字典自帶的update方法)
def z_update(dict1:dict, dict2:dict):
"""說明:將dict1中的鍵值對添加到dict2中"""
for key in dict1:
dict2[key] = dict1[key]
return dict2
print(z_update({'a':1, 'b':2, 'c':3},{'d':4, 'e':5, 'c':6}))
5. 寫一個函數(shù),能夠?qū)⒅付ㄗ址械乃械男懽帜皋D(zhuǎn)換成大寫字母;所有的大寫字母轉(zhuǎn)換成小寫字母(不能使用字符串相關(guān)方法)
def z_switch(str1: str):
str2 = ''
for index in range(len(str1)):
if 'a' <= str1[index] <= 'z':
str2 += chr(ord(str1[index]) - 32)
elif 'A' <= str1[index] <= 'Z':
str2 += chr(ord(str1[index]) + 32)
else:
str2 +=str1[index]
return str2
print(z_switch('aaAgGhJ12J45'))
6. 實現(xiàn)一個屬于自己的items方法,可以將自定的字典轉(zhuǎn)換成列表。列表中的元素是小的列表,里面是key和value
(不能使用字典的items方法)
例如:{'a':1, 'b':2} 轉(zhuǎn)換成 [['a', 1], ['b', 2]]
def z_item(dict1: dict):
list1 = []
for key in dict1:
list2 = []
list2.append(key)
list2.append(dict1[key])
list1.append(list2)
return list1
print(z_item({'a':1, 'b':2}))
7. 寫一個函數(shù),實現(xiàn)學生的添加功能:
def add_student(num1=0, choice=1):
#stu_str = ''
stu_list = []
while choice == 1:
print('=============添加學生================')
name = input('輸入學生姓名:')
age = int(input('輸入學生年齡:'))
tel = int(input('輸入學生電話:'))
num1 += 1
print('============添加成功!===============')
# if num1 == 1:
# stu_str += "姓名':%s,'年齡’:%d,'電話:%d','學號':'%s'" % (name, age, tel, str(num1).zfill(4))
# else:
# stu_str += "\n姓名':%s,'年齡’:%d,'電話:%d','學號':'%s'" % (name, age, tel, str(num1).zfill(4))
# print(stu_str)
stu_list.append({"'姓名':": name, "'年齡':": age, "'電話:'": tel, "學號:":str(num1).zfill(4)})
for stu in stu_list:
print(str(stu)[1:-1])
print('=====================================')
print('1.繼續(xù)')
print('2.返回')
choice = int(input('請選擇:'))
for stu in stu_list:
print(str(stu)[1:-1])
add_student()
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。