# Your first python code # print Hello World to console print("Hello World")
2920 · 获取输入数据并打印它
1 2 3
# Please write your code here name = input() print(name)
2924 · 定义字符串和数字并打印
1 2 3 4 5 6 7
# Please write your code here str_1 = 'Hello world' num_1 = 1 print(type(str_1)) print(type(num_1)) print(str_1) print(num_1)
2407 · 计算 a + aa + aaa + aaaa 的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
defcalculate_sum(int_1: int) -> None: """ :param int_1: Input number :return: None """ # -- write your code here -- answer = 0 temp = 0
for i inrange(4): temp += int_1 answer += temp temp *= 10
print(answer)
2271 · 整数运算(Python 版)
1
基本数据类型
2271 · 整数运算(Python 版)
1 2 3 4 5 6 7 8 9 10
# write your code here # read data from console a = int(input()) b = int(input())
# output the answer to the console according to the requirements of the question print(a + b) print(a - b) print(a * b) print(a // b)
2919 · 整数运算2
1 2 3 4 5 6 7 8 9
# write your code here # read data from console a = int(input()) b = int(input())
# output the answer to the console according to the requirements of the question print(a % b) print(a ** b) print(a // b)
2269 · 交换两个整数(Python 版)
1 2 3 4 5 6 7 8
# write your code here # read data from console a = int(input()) b = int(input()) a, b = b, a # output the answer to the console according to the requirements of the question print('a =', a, end='') print(', b =', b)
2267 · 求三个数的最大值(Python 版)
1 2 3 4 5 6 7 8
import sys
a = int(sys.argv[1]) b = int(sys.argv[2]) c = int(sys.argv[3]) # write your code here # you need to print the maximum print(max(a, b, c))
2271 · 整数运算(Python 版)
1 2 3 4 5 6 7
# output the answer to the console according to the requirements of the question a = int(input()) b = int(input()) print(a + b) print(a - b) print(a * b) print(a // b)
2269 · 交换两个整数(Python 版)
1 2 3 4
a = int(input()) b = int(input()) a , b = b , a print("a = {}, b = {}".format(a , b))
2267 · 求三个数的最大值(Python 版)
1 2 3 4 5 6
import sys
a = int(sys.argv[1]) b = int(sys.argv[2]) c = int(sys.argv[3]) print(max(a,b,c))
2917 · 两个变量间的逻辑运算
1 2 3 4 5 6 7 8
# read data from console a = eval(input()) b = eval(input()) # write your code here print(a and b) print(a or b) print(not a) print(not b)
2418 · 位运算左移三位(Python 版)
1 2 3 4 5
# write your code here # read data from console n = int(input()) # output the answer to the console according to the requirements of the question print(n << 3)
2937 · 二进制数最右边的 1
1 2 3 4 5
# Get the number a num_1 = int(input()) # Please write your code here x = num_1&(num_1-1) print(num_1-x)
2948 · 给会员打折
1 2 3 4 5 6 7 8 9 10 11
# Get Variables price = int(input()) customer = eval(input()) vip = eval(input()) blacklist = eval(input()) # please write your code here if customer in vip: # 判断顾客是否是VIP price="%.2f"%(0.70*price) elif customer in blacklist: # 判断顾客是否在黑名单 price=-1 print(price)
2950 · 查看是否是同一对象
1 2 3 4 5
# Please write your code here print(True) print(True) print(True) print(True)
基本数据类型
2216 · 字符串的相加、重复输出、切片
1 2 3 4 5 6 7 8 9 10 11 12
defadd_repeat_slicing(str_1: str, str_2: str) -> tuple: """ :param str_1: The first source string :param str_2: The Second source string :return: tuple: A tuple containing three new strings """ # -- write your code here -- a = str_1 + str_2 b = str_1 * 2 c = str_1[1:4] res = (a,b,c) return res
2363 · 根据行边界符拆分字符串并找出最长行
1 2 3 4 5 6 7
defsplitlines(src: str) -> str: """ :param src: The source string needs to be processed :return: The maximum length of the string """ # -- write your code here -- returnmax(src.splitlines(),key=len)
2395 · 统计字符串出现的次数
1 2 3 4 5 6 7 8 9
defcount_string(str_1: str) -> tuple: ''' :param str_1: Input string :return: Count the number of occurrences of a string ''' # -- write your code here -- coun_a = str_1.count('a') coun_going = str_1.count('going',0,40) return (coun_a,coun_going)
2923 · 定义一个列表
1 2 3 4 5 6
# read data from console str_1 = str(input()) num_1 = int(input()) # Please print the list of definitions list_1=[str_1,num_1] print(list_1)
# read data from console num_1 = int(input()) num_2 = eval(input()) # Please print the list of definitions t =(num_1,num_2) print(t,type(t),sep='\n')
3137 · 分割元组
1 2 3 4 5 6
# read data from console my_tuple = eval(input()) # write your code here i = int(len(my_tuple) / 2) print(my_tuple[:i]) print( my_tuple[i:])
3138 · 元素在元组内的出现次数
1 2 3 4 5
# read data from console my_tuple = eval(input()) x = int(input()) # write your code here print(my_tuple.count(x))
3139 · 集合中最大的元素
1 2 3 4
# read data from console my_set = eval(input()) # write your code here print(max(my_set))
3140 · 集合运算
1 2 3 4 5 6
# read data from console my_set1 = eval(input()) my_set2 = eval(input()) my_set3 = eval(input()) # write your code here print(len(my_set1 & my_set2 - my_set3))
3141 · 集合中绝对值小于 10 的元素之和
1 2 3 4
# read data from console my_set = eval(input()) # write your code here print(sum([num for num in my_set ifabs(num) <10]))
3142 · 字典中不同的 value 数目
1 2 3 4
# read data from console my_dict = eval(input()) # write your code here print(len(set(my_dict.values())))
3143 · 字典合并后的大小
1 2 3 4 5 6
# read data from console my_dict1 = eval(input()) my_dict2 = eval(input()) # write your code here my_dict1.update(my_dict2) print(len(my_dict1))
3144 · 根据字典替换列表元素
1 2 3 4 5
# read data from console my_dict = eval(input()) nums = eval(input()) # write your code here print([my_dict[i] for i in nums ])
控制语句
2315 · 判断三角形(Python 版)
1 2 3 4 5 6 7 8 9 10 11
# write your code here # read data from console a = int(input()) b = int(input()) c = int(input()) # output the answer to the console according to the requirements of the question lis = [a, b, c] if2 * max(lis) < sum(lis) : print("Is a triangle") else: print("Not a triangle")
2202 · 求和(Python 版)
1 2 3 4 5 6 7 8
# write your code here # read data from console n = int(input()) # output the answer to the console according to the requirements of the question num = 0 for i inrange(1, n+1): num = num + i print(num)
2356 · 打印九九乘法表(Python 版)
1 2 3 4 5
# write your code here # output the answer to the console according to the requirements of the question for i inrange(1, 10): for j inrange(1, i+1): print(f"{j}*{i}={i*j}", end=" "if j<i else'\n')
3167 · 交错和(Python 版)
1 2 3 4 5 6 7 8 9 10
# read data from console n = eval(input()) # write your code here sum=0 for i inrange(1,n+1): if i%2==1: sum+=-i else : sum+=i print(sum)
3170 · 找到第一个平方大于 n 的整数
1 2 3 4 5 6 7
# read data from console n = eval(input()) # write your code here candidate = 1# 从整数1开始递增 while candidate ** 2 <= n: # 循环直到找到第一个平方大于n的整数 candidate += 1 print(candidate)
3174 · 值为下标的倍数的元素个数(Python 版)
1 2 3 4 5 6 7 8 9 10 11
# read data from console arr = eval(input()) # write your code here count = 0# 初始化计数器 for i, num inenumerate(arr): # 遍历列表arr if i == 0: if num == 0: # i为0时,特判arr[i]是否为0 count += 1 elif num % i == 0: # i不为0时,判断arr[i]是否为i的倍数 count += 1 print(count)
3175 · 组成三角形的元组个数
1 2 3 4 5 6 7 8 9 10 11
# read data from console n = eval(input()) # write your code here count = 0 for i inrange(1,n+1): # 遍历所有元素值不超过n的三元组 for j inrange(1,n+1): for k inrange(1,n+1): triplet = sorted((i,j,k)) # 对每个三元组进行排序 if triplet[0]+triplet[1] > triplet[2]: # 判断最小的两个数之和是否大于第三个数 count +=1# 计数器加1 print(count)
函数
2932 · 打印学生平均成绩
1 2 3 4 5 6
# Please write your code here defprint_avg(*score, **info): return ("name: %s, age: %d, avg: %.2f" % (info['student_name'], info['student_age'], sum(score)/len(score)))
2124 · 打印 Hello Python
1 2 3
defhello_python(): # --write your code here-- print("Hello Python!")
3166 · 求两数绝对值之和(Python 版)
1 2 3 4 5
# Write your code here. defmy_func(num1,num2): return(numAbs(num1)+numAbs(num2)) defnumAbs(num): return (-num if num<0else num)
3171 · 找出 1 - n 中能被 x 整除的数
1 2 3 4
# Write your code here. defmy_func(n:int,x:int): list=[x*i for i inrange(1,n//x+1)] returnlist,len(list)
2126 · 导入一个模块文件夹下的中一个文件
1 2 3 4
# write your code here from branch import solution # keep the code below solution.do()
3164 · 重命名导入的同名文件
1 2 3 4 5 6
# Write your code here from branch1 import solution as a from branch2 import solution as b # Keep the code below a.do() b.do()
3176 · 输出对应日期的正午时间点
1 2 3 4
# Write your code here. from datetime import datetime defmy_func(year,month,day): return datetime(year,month,day,12)
3148 · 使用 lambda 函数构造一元二次多项式
1 2 3 4
defequation(a, b, c): # Write your code here. k = lambda x : a*x**2 + b*x + c return k
3162 · 根据年龄对成员进行排序
1 2 3
defsort_list(members: list): # Write your code here. returnsorted(members,key=lambda x:x[2])
文件操作
2930 · 向文件里写入列表
1 2 3 4 5
defwrite_list(path: str, list_1: list): # Please write your code withopen(path,'w') as f: f.write(str(list_1)) f.close()
2931 · 从文件中读取字典并修改它
1 2 3 4 5 6 7 8 9 10
import json
defget_write_dict(path:str): # Please write your code withopen(path) as fp: py_dic = json.load(fp) py_dic['age']=18 js_str = f'{py_dic}' withopen(path,'w') as fps: json.dump(js_str,fps)
2934 · 读取 csv 文件并修改
1 2 3 4 5 6 7 8 9 10
import csv
defget_write_csv(path:str): # Please write your code withopen(path,'r') as f: lines=f.readlines() lines[0]=lines[0].replace('name','student_name') withopen(path,'w') as f: f.writelines(lines) f.close()
异常处理
2942 · 阻止黑名单上的人
1 2 3 4 5 6 7 8 9 10
classDiscoverBlacklist(Exception): pass
defselection(blacklist: list, customers: list): # please write your code here for customer in customers: if customer in blacklist: raise DiscoverBlacklist('Alert, %s is on the blacklist.' % customer) return'Welcome to the next visit'