1.编写函数,求1 + 2 + 3 +…N的和
def sum1_func(N):
sum1 = 0
for x in range(1, N+1):
sum1 += x
print(sum1)
sum1_func(100)
2.编写一个函数,求多个数中的最大值
def max_nums(*nums):
max_num = max(nums)
print(max_num)
max_nums(-1, -3, -4, -9, -7)
3.编写一个函数,实现摇骰子的功能,打印N个骰子的点数和
def dice_sums(n):
sum1 = 0
for _ in range(n):
import random
num = random.randint(1, 6)
print('骰子点数:%d' % num)
sum1 += num
print(sum1)
dice_sums(3)
4.编写一个函数,交换指定字典的key和value。
例如: dict1 = {'a': 1, 'b': 2, 'c': 3} --> dict1 = {1: 'a', 2: 'b', 3: 'c'}
dict1 = {'a': 1, 'b': 2, 'c': 3}
def exchange_dict (dict1:dict):
dict2 = dict1.copy()
dict1.clear()
for x in dict2:
dict1[dict2[x]] = x
print(dict1)
exchange_dict({'a': 1, 'b': 2, 'c': 3})
5.编写一个函数,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串。
例如: 传入'12a&bc12d-+' --> 'abcd'
def chr_func1(str1):
for item in str1:
if 'a' <= item <= 'z' or 'A' <= item <= 'Z':
print(item, end='')
chr_func1('12a&bc12d-+')
6.写一个函数,求多个数的平均值
def avg_func1(*nums):
avg1 = sum(nums)/len((nums))
print(avg1)
avg_func1(1, 2, 3, 4)
7.写一个函数,默认求10的阶乘,也可以求其他数字的阶乘
def factorial_func1(x=10):
acc = 1
for i in range(1,x+1):
acc *= i
print(acc)
factorial_func1()
8.写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
def capitalize_func1(str1):
if 'a' <= str1[0] <= 'z':
new_word = chr(ord(str1[0])-32)
str1 = new_word + str1[1:]
print(str1)
else:
print(str1)
capitalize_func1('1aBC123a')
9.写一个自己的endswith函数,判断一个字符串是否已指定的字符串结束
def endswith_func(str1, str2):
n = len(str2)
for i in range(-1, -n-1, -1):
if not(str1[i] == str2[i]):
print('False')
break
else:
print('True')
endswith_func('abc231abd', '1abd')
10.写一个自己的isdigit函数,判断一个字符串是否是纯数字字符串
def isdigit_func(str1):
for item in str1[0:]:
if not('0' <= item <= '9'):
print('False')
break
else:
print('True')
isdigit_func('91234')
11.写一个自己的upper函数,将一个字符串中所有的小写字母变成大写字母
def upper_func1(str1):
for item in str1:
if 'a'<= item <= 'z':
item =chr(ord(item) - 32)
print(item, end='')
else:
print(item, end='')
upper_func1('abH23好rp1')
12.写一个自己的rjust函数,创建一个字符串的长度是指定长度,原字符串在新字符串中右对齐,剩下的部分用指定的字符填充
def rjust_func1(original_char, width, char):
n = width-len(original_char)
new_char =char * n + original_char
print(new_char)
rjust_func1('abc', 7, '^')
rjust_func1('你好吗', 7, '0')
13.写一个自己的index函数,统计指定列表中指定元素的所有下标,如果列表中没有指定元素返回-1
def index_func1(list1, item1):
n = len(list1)
count = 0
for i in range(n):
#print(type(list1[i]))
if list1[i] == item1:
count += 1
print(i, end=' ')
if count ==0:
print('-1')
index_func1 ([1, 2, 45, 'abc', 1, '你好', 1, 0], 9)
14.写一个自己的len函数,统计指定序列中元素的个数')
def len_func1(orders):
sum1 = 0
for item in orders:
sum1 += 1
print(sum1)
len_func1([1, 3, 5, 6])
len_func1([1, 33, 'a', 45, 'bbb'])
15.写一个自己的max函数,获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值'
def max_func1(orders):
if type(orders) == dict:
for key in orders:
max1 = orders[key]
for key in orders:
if max1 < orders[key]:
max1 = orders[key]
print(max1)
else:
max1 = orders[0]
for item in orders:
if max1 <= item:
max1 = item
print(max1)
max_func1([-7, -12, -1, -9])
max_func1('abcdpzasdz')
max_func1({'小明':90, '张三': 76, '路飞':30, '小花': 98} )
16.写一个函数实现自己in操作,判断指定序列中,指定的元素是否存在
def in_func1(orders, items):
for item in orders:
if items == item:
print('True')
break
else:
print('False')
in_func1((12, 90, 'abc'), '90')
17.写一个自己的replace函数,将指定字符串中指定的旧字符串转换成指定的新字符串
def replace_func1(str1, old_str, new_str):
for i in range(len(str1)):
if str1[i:i+len(old_str)] == old_str:
str1 = str1[:i]+new_str+str1[i+len(old_str):]
print(str1)
replace_func1('how are you? and you?', 'you', 'me')
18.写四个函数,分别实现求两个列表的交集、并集、差集、对称差集的功能
def list_func1(list1, list2):
new_list = []
for item1 in list1:
for item2 in list2:
if item1 == item2:
new_list.append(item1)
print(new_list)
list_func1([1, 2, 2, 3, 3, 4], [3, 4, 5, 6])
def list_func2(list1, list2):
new_list = []
for item1 in list1:
new_list.append(item1)
for item2 in list2:
if item2 not in list2:
new_list.append(item2)
print(new_list)
list_func2([1, 2, 2, 3, 3, 4], [3, 4, 5, 6])
def list_func3(list1, list2):
new_list = []
for item1 in list1:
new_list.append(item1)
for item2 in list2:
if item2 in list1:
new_list.remove(item2)
print(new_list)
list_func3([1, 2, 2, 3, 3, 4], [3, 4, 5, 6])
def list_func4(list1, list2):
new_list = []
for item1 in list1:
new_list.append(item1)
if item1 in list2:
new_list.remove(item1)
print(1111, new_list)
for item2 in list2:
if item2 not in list1:
new_list.append(item2)
print(new_list)
list_func4([1, 2, 2, 3, 3, 4], [3, 4, 5, 6]) # 对称差集