0.寫(xiě)一個(gè)匿名函數(shù),判斷指定的年是否是閏年
程序:
# 0.寫(xiě)一個(gè)匿名函數(shù),判斷指定的年是否是閏年
leap_year = lambda years: '是閏年' if (years % 4 == 0 and years % 100 != 0) \
or years % 400 == 0 else '不是閏年'
print(leap_year(2000))
print(leap_year(2018))
結(jié)果:

1.寫(xiě)一個(gè)函數(shù)將一個(gè)指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使列表自帶的逆序函數(shù))
程序:
def reverse_list1(l:list):
"""輸出列表l的逆序"""
for index in range(len(l)//2):
l[index], l[len(l) - index - 1] = l[len(l) - index - 1], l[index]
return l
list1 = [1, 2, 3]
reverse_list1(list1)
print(list1)
list2 = [1, 2, 3, 4, 'y']
reverse_list1(list2)
print(list2)
結(jié)果:

2.寫(xiě)一個(gè)函數(shù),提取出字符串中所有奇數(shù)位上的字符
程序:
# 2.寫(xiě)一個(gè)函數(shù),提取出字符串中所有奇數(shù)位上的字符
def extract(str1: str):
"""返回字符串str1所有奇數(shù)位上的字符"""
str2 = str1[1::2]
return str2
str1 = '012346789'
print(extract(str1))
str2 = 'abcdefghi'
print(extract(str2))
結(jié)果:

3.寫(xiě)一個(gè)函數(shù),統(tǒng)計(jì)指定列表中指定元素的個(gè)數(shù)(不用列表自帶的count方法)
程序:
# 3.寫(xiě)一個(gè)函數(shù),統(tǒng)計(jì)指定列表中指定元素的個(gè)數(shù)(不用列表自帶的count方法)
def my_count(l: list, item1):
"""返回列表l中值為item1元素的個(gè)數(shù)"""
count = 0
for item in l:
if item1 == item:
count += 1
return count
list1 = ['a', 'b', 1, 1, 1, 'c', 'j', 'c', 'j', 'c']
print('list1中c字符的個(gè)數(shù)為:', my_count(list1, 'c'))
print('list1中1的個(gè)數(shù)為:', my_count(list1, 1))
結(jié)果:

4.寫(xiě)一個(gè)函數(shù),獲取指定列表中指定元素的下標(biāo)(如果指定元素有多個(gè),將每個(gè)元素的下標(biāo)都返回)
例如: 列表是:[1, 3, 4, 1] ,元素是1, 返回:0,3
程序:
# 4.寫(xiě)一個(gè)函數(shù),獲取指定列表中指定元素的下標(biāo)(如果指定元素有多個(gè),將每個(gè)元素的下標(biāo)都返回)
# 例如: 列表是:[1, 3, 4, 1] ,元素是1, 返回:0,3
def get_index(l: list, item):
"""獲取列表l中元素值為item的元素下標(biāo)"""
index_l = []
for index in range(len(l)):
if item == l[index]:
index_l.append(index)
str1 = ''
for item1 in index_l:
str1 += str(item1)
return ','.join(str1)
list1 = ['a', 'b', 1, 'a', 1, 'c', 'j', 'c', 'j', 'c']
print('list1中值為字符b的元素下標(biāo)有:', get_index(list1, 'b'))
print('list1中值為1的元素下標(biāo)有:', get_index(list1, 1))
print('list1中值為a的元素下標(biāo)有:', get_index(list1, 'a'))
結(jié)果:

5.寫(xiě)一個(gè)函數(shù),能夠?qū)⒁粋€(gè)字典中的鍵值對(duì)添加到另外一個(gè)字典中(不使用字典自帶的update方法)
程序:
# 寫(xiě)一個(gè)函數(shù),能夠?qū)⒁粋€(gè)字典中的鍵值對(duì)添加到另外一個(gè)字典中(不使用字典自帶的update方法)
def my_update(dict1: dict, dict2: dict):
"""將字典dict2中的鍵值對(duì)添加到字典dict1中,如果dict1中已有dict2中的key,
則更新其對(duì)應(yīng)的值。完成后打印出更新后的字典dict1"""
for key2 in dict2:
for key1 in dict1:
if key1 == key2:
dict1[key1] = dict2[key2]
dict1[key2] = dict2[key2]
print(dict1)
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'c': 'j', 'd': 4, 'e': 5}
my_update(dict1, dict2)
結(jié)果:

6.寫(xiě)一個(gè)函數(shù),能夠?qū)⒅付ㄗ址械乃械男?xiě)字母轉(zhuǎn)換成大寫(xiě)字母;所有的大寫(xiě)字母轉(zhuǎn)換成小寫(xiě)字母(不能使用字符串相關(guān)方法)
程序:
# 6.寫(xiě)一個(gè)函數(shù),能夠?qū)⒅付ㄗ址械乃械男?xiě)字母轉(zhuǎn)換成大寫(xiě)字母;所有的大寫(xiě)字母
# 轉(zhuǎn)換成小寫(xiě)字母(不能使用字符串相關(guān)方法)
def my_swapcase(str1: str):
"""將字符串str1中的所有的小寫(xiě)字母轉(zhuǎn)換成大寫(xiě)字母;
所有的大寫(xiě)字母轉(zhuǎn)換成小寫(xiě)字母"""
str2 = ''
for item in str1:
if 'a' <= item <= 'z':
str2 += chr(ord(item)-32)
elif 'A' <= item <= 'Z':
str2 += chr(ord(item) + 32)
else:
str2 += item
print(str2)
str1 = 'aha2312sduhdsASKDkddlad'
my_swapcase(str1)
結(jié)果:

7.寫(xiě)一個(gè)函數(shù),能夠?qū)⒅付ㄗ址械闹付ㄗ哟鎿Q成指定的其他子串(不能使用字符串的replace方法)
例如: func1('abcdaxyz', 'a', '') - 返回: '\bcd\xyz'
程序:
# 7.寫(xiě)一個(gè)函數(shù),能夠?qū)⒅付ㄗ址械闹付ㄗ哟鎿Q成指定的其他子串(不能使用字符串
# 的replace方法)例如: func1('abcdaxyz', 'a', '\') - 返回: '\bcd\xyz'
# 1
def my_replace(str1: str, item1, item2):
"""將str1中值為item1的字符換為item2輸出"""
if len(item1) == 1:
str2 = ''
for item in str1:
if item1 == item:
str2 += item2
else:
str2 += item
else:
str2 = ''
d = len(item1)
index = 0
while index < len(str1):
if item1 == str1[index:index+d]:
str2 += item2
index += d
else:
str2 += str1[index]
index += 1
print(str2)
str1 = 'aha2312sahadduhdsahaASKDkddlahad'
my_replace(str1, 'ah', '\\')
my_replace(str1, 'aha', '\\')
my_replace(str1, 'a', '\\')
# 2
def my_replace(str1: str, item1, item2):
"""將str1中值為item1的字符換為item2輸出"""
str_list = str1.split(item1)
strstr = str_list[0]
for index in range(1,len(str_list)):
strstr += item2 + str_list[index]
return strstr
str1 = 'aha2312sduhdsASKDkddlad'
print(my_replace(str1, 'a', '\\'))
結(jié)果:

8.實(shí)現(xiàn)一個(gè)輸入自己的items方法,可以將自定的字典轉(zhuǎn)換成列表。列表中的元素是小的列表,里面是key和value (不能使用字典的items方法)
例如:{'a':1, 'b':2} 轉(zhuǎn)換成 [['a', 1], ['b', 2]
程序:
# 8.實(shí)現(xiàn)一個(gè)輸入自己的items方法,可以將自定的字典轉(zhuǎn)換成列表。列表中的元素是小的
# 列表,里面是key和value (不能使用字典的items方法)
# 例如:{'a':1, 'b':2} 轉(zhuǎn)換成 [['a', 1], ['b', 2]
def my_items(dict1: dict):
"""將字典dict1轉(zhuǎn)成列表。列表中的元素是小的
列表,里面是key和value,并打印出改列表"""
list1 = []
i = 0
for key in dict1:
list2 = []
list2.append(key)
list2.append(dict1[key])
list1.append(list2)
list1[i] = list2
i += 1
print(list1)
my_items({'a': 1, 'b': 2})
my_items({'a': 1, 'b': 2, 'c': 3})
結(jié)果:
