def add(n, m):
x = 0
for i in range(n, m + 1):
x += i
return x
N = int(input('请输入你的N值:'))
y = add(1, N)
print('和为:',y)
运行结果:
请输入你的N值:4
和为: 10
第一种方法:
def max1(*nums):
return max(nums)
result = max1(92, 75, 99, 64)
print(result)
第二种方法:
def test(*nums):
max1 = nums[0]
for num in nums[1:]:
if num > max1:
max1 = num
return max1
result = test(92, 75, 99, 64)
print(result)
运行结果:
99
from random import randint
def play_dice(n):
sum1 = 0
for i in range(n):
sum1 += randint(1, 6)
return sum1
result=play_dice(2)
print(result)
运行结果:
12
第一种方法:
def dict1(dict3):
dict2 = {v: k for k, v in dict3.items()}
return dict2
result = dict1({'a': 1, 'b': 2, 'c': 3})
print('dict1' + '=', result)
第二种方法:
def dict1(dic3):
for key in dic3.keys():
value = dic3.pop(key)
dic3[value] = key
return dic3
result = dict1({'a': 1, 'b': 2, 'c': 3})
print('dict1' + '=', result)
运行结果:
dict1= {1: ‘a’, 2: ‘b’, 3: ‘c’}
第一种方法:
def x(str1):
y = ''
for i in str1:
if i.isalpha():
y += i
return y
result = x('12a&bc12d-+')
print(result)
第二种方法:
def x(str1):
y = ''
for i in str1:
if 'a' <= i <= 'z' or 'A' <= i <= 'Z':
y += i
return y
result = x('12a&bc12d-+')
print(result)
运行结果:
abcd
第一种方法:
def avg(*num):
count = 0
sum1 = 0
for i in num:
count += 1
sum1 += i
x = sum1 / count
return x
result = avg(10, 10, 10, 10)
print(result)
第二种方法:
def avg(*num):
x = sum(num) / len(num)
return x
result = avg(10, 10, 10, 10)
print(result)
第三种方法:
def avg(*nums):
sum1 = 0
for x in nums:
sum1 += x
return sum1 / len(nums)
result = avg(10, 10, 10, 10)
print(result)
运行结果:
10.0
def factorial(n=10):
x = 1
for i in range(1, n + 1):
x *= i
return x
result = factorial(10)
print(result)
运行结果:
3628800
第一种方法:
def str1(str2):
for i in str2:
if 'a' <= str2[0] <= 'z':
x = chr(ord(str2[0]) - 32)
str2 = str2.replace(str2[0], x)
return str2
result = str1('fee15')
print(result)
第二种方法:
def str1(str2):
first = str2[0]
if 'a' <= first <= 'z':
return first.upper() + str2[1:]
return str2
result = str1('fee15')
print(result)
运行结果 :
Fee15
第一种方法:
def str1(str2, str3):
x = []
y = []
for i in str2[-len(str3):]:
x.append(i)
for j in str3:
y.append(j)
if x != y:
print('False')
else:
print('True')
result = str1('abc123ad1', 'ad')
第二种方法:
def str1(str2, str3):
len1 = len(str3)
if str2[-len1:] == str3:
return True
else:
return False
result = str1('abc123ad1', 'ad')
print(result)
运行结果:
False
def str1(str2):
for i in str2:
if not '0' <= i <= '9':
print('False')
break
else:
print('True')
result = str1('dsfwe15')
运行结果:
False
第一种方法:
def str1(str2):
for i in str2:
if 'a' <= i <= 'z':
x = chr(ord(i) - 32)
str2 = str2.replace(i, x)
return str2
result = str1('fee15')
print(result)
第二种方法:
def str1(str2):
str3 = ''
for x in str2:
if x.islower():
str3 += chr(ord(x) - 32)
else:
str3 += x
return str3
result = str1('fee15')
print(result)
运行结果:
FEE15
第一种方法:
def str1(str2, long1, str3):
x = ''
long1 = long1 - len(str2)
x = long1 * str3 + str2
return x
result = str1('adc', 7, '^')
print(result)
第二种方法:
def str1(str2, long1, str3):
if len(str2) >= long1:
return str2
return (long1 - len(str2)) * str3 + str2
result = str1('adc', 7, '^')
print(result)
运行结果:
^^^^adc
def list1(list2, a):
list3 = []
if a not in list2:
return -1
for i in range(len(list2)):
if list2[i] == a:
list3.append(i)
return list3
result = list1(['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权'], '赵云')
print(result)
运行结果:
[0, 4]
def x(a):
count = 0
for i in a:
count += 1
return count
result = x((1, 34, 'a', 45, 'bbb'))
print(result)
运行结果:
5
def x(a, b):
for i in a:
if i == b:
print('True')
break
else:
print('False')
result = x([12, 90, 'abc'], 90)
运行结果:
True
第一种方法:
def str1(str2, str3, str4):
"""
字符串替换
:param str2: 原字符串
:param str3: 需要被替换的字符串
:param str4: ⽤来替换的新字符串
:return: 替换后的字符串
"""
str5 = ""
x = 0
while x < len(str2):
if str2[x:x + len(str3)] == str3:
str5 += str4
x += len(str3)
else:
str5 += str2[x]
x += 1
return str5
result = str1('how are you? and you?', 'you', 'me')
print(result)
第二种方法:
def str1(str2, str3, str4):
str_list = str2.split(str3)
return str4.join(str_list)
result = str1('how are you? and you?', 'you', 'me')
print(result)
运行结果:
how are me? and me?
第一种方法:
def max_num(a):
list1 = []
if type(a) == dict:
for key in a:
list1.append(a[key])
list1.sort()
print(list1[-1])
else:
for x in a:
list1.append(x)
list1.sort()
print(list1[-1])
result = max_num({'小明':90, '张三': 76, '路飞':30, '小花': 98})
第二种方法:
def max_num(a):
if isinstance(a, dict):
a = list(a.values())
else:
a = list(a)
max1 = a[0]
for x in a:
if x > max1:
max1 = x
return max1
result = max_num({'小明': 90, '张三': 76, '路飞': 30, '小花': 98})
print(result)
运行结果:
98
交集:
def jiaoji(list1, list2):
list3=[]
for item in list1:
if item in list2:
list3.append(item)
print(list3)
result1=jiaoji([1,2,3,4,5,6],[4,5,6,7,8,9])
运行结果:
[4, 5, 6]
并集:
第一种方法:
def bingji(list1, list2):
list4 = []
list3 = list1+list2
for item in list3:
if item not in list4:
list4.append(item)
print(list4)
result2=bingji([1,2,3,4,5,6],[4,5,6,7,8,9])
第二种方法:
def bingji(list1, list2):
return list(set(list1 + list2))
result = bingji([1, 2, 3, 4, 5, 6], [4, 5, 6, 7, 8, 9])
print(result)
运行结果:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
差集:
第一种方法:
def chaji(list1, list2):
list3 = list1 + list2
list4 = list3[:]
for item in list4:
if item in list2:
list3.remove(item)
print(list3)
result3=chaji([1,2,3,4,5,6],[4,5,6,7,8,9])
第二种方法:
def chaji(list1, list2):
list3 = []
for item in list1:
if item not in list2 and item not in list3:
list3.append(item)
return list3
result = chaji([1, 2, 3, 4, 5, 6], [4, 5, 6, 7, 8, 9])
print(result)
运行结果:
[1, 2, 3]
补集:
第一种方法:
def buji(list1, list2):
list3 = list1+list2
list4 = list3[:]
for item in list4:
if list4.count(item) > 1:
list3.remove(item)
print(list3)
result4=buji([1,2,3,4,5,6],[4,5,6,7,8,9])
第二种方法:
def buji(list1, list2):
list3 = []
for item in list1 + list2:
if (item in list1 and item not in list2) or (item in list2 and item not in list1):
list3.append(item)
return list3
result = buji([1, 2, 3, 4, 5, 6], [4, 5, 6, 7, 8, 9])
print(result)
运行结果:
[1, 2, 3, 7, 8, 9]