3defis_leap(year):
year =int(year)if(year %4==0)and(year %100!=0):returnTrueelif year %400==0:returnTrueelse:returnFalsewhile(True):
year =input("请输入一个年份:")if year =='exit':import sys
sys.exit(0)print("%s是闰年吗? %s"%(year, is_leap(year)))
5deffn(n):if n <2:
exit()sum=1for i inrange(2, n+1):sum+= i **3returnsum
n =int(input("请输入数字:"))
result = fn(n)print("1~%d的立方和是%d"%(n, result))
6deffn(n):
result_list =[1]for i inrange(n):
result_list.append(result_list[-1]*len(result_list))return result_list[-1]
n =int(input("请输入数字:"))
result = fn(n)print("%d的阶乘是%d"%(n, result))
7defremove_duplicate(source_list):
new_list =list({}.fromkeys(source_list).keys())return new_list
# 另一种方法# new_list = []# [new_list.append(i) for i in source_list if not i in new_list]# return new_list
length =int(input("请输入列表的长度:"))
my_list =[]for i inrange(length):
my_list.append(input('请输入字符串:'))print(remove_duplicate(my_list))
8import random
deffn(n):
i, tmp_list =0,[]whileTrue:
num = random.randint(0,100)# 如果随机数不包含在列表中,则保存if num notin tmp_list:
tmp_list.append(num)
i +=1if i == n:break# 将列表转成元组返回returntuple(tmp_list)
n =int(input("请输入整数n:"))print(fn(n))
9import random
deffn(n):
i, tmp_list =0,[]whileTrue:
num = random.randint(65,65+25)# 如果随机数不包含在列表中,则保存ifchr(num)notin tmp_list:
tmp_list.append(chr(num))
i +=1if i == n:break# 将列表转成元组返回returntuple(tmp_list)
n =int(input("请输入整数n:"))print(fn(n))
10import random
deffn(n):# 输出矩阵for i inrange(n):for j inrange(n):print(' %2d '%(i * n + j +1), end="")print()print('-'*(4* n))# 输出转置矩阵for i inrange(n):for j inrange(n):print(' %2d '%(j * n + i +1), end="")print()
n =int(input("请输入整数n:"))
fn(n)