计算机二级Python编程题记录(上)

简介: 计算机二级Python编程题记录

第一套


1




s = input("请输入一个字符串:")
print("{:*^30}".format(s))


2




a, b = 0, 1
while a<=50:
    print(a, end=',')
    a, b = b, a+b


3




import jieba
txt = input("请输入一段中文文本:")
ls= jieba.lcut(txt)
for i in ls[::-1]:
    print(i,end='')


4




import turtle
for i in range(3):
    turtle.seth(i*120)
    turtle.fd(100)


5




fo = open("PY202.txt", "w")
txt = input("请输入类型序列: ")
fruits = txt.split(" ")
d = {}
for fruit in fruits:
    d[fruit] = d.get(fruit, 0) + 1
ls = list(d.items())
ls.sort(key=lambda x: x[1], reverse=True)  # 按照数量排序
for k in ls:
    fo.write("{}:{}\n".format(k[0], k[1]))
fo.close()


6




fi = open("小女孩.txt", "r")
fo = open("PY301-1.txt", "w")
txt = fi.read()
d = {}
exclude = ", 。 ! ? 、 () 【】 《》 <> = : :+-*__“”..."
for word in txt:
    if word in exclude:
        continue
    else:
        d[word] = d.get(word, 0) + 1
ls = list(d.items())
ls.sort(key=lambda x:x[1], reverse=True)
fo.write("{}:{}".format(ls[0][0], ls[0][1]))
fo.close()
fi.close()


fi = open("小女孩.txt", "r")
fo = open("PY301-2.txt", "w")
txt = fi.read()
d = {}
for word in txt:
    d[word] = d.get(word, 0) + 1
del d["\n"]
ls = list(d.items())
ls.sort(key=lambda x: x[1], reverse=True)
for i in range(10):
    fo.write(ls[i][0])
fi.close()
fo.close()


fi = open("小女孩.txt", "r")
fo = open("小女孩-频次排序", "w")
txt = fi.read()
d = {}
for word in txt:
    d[word] = d.get(word, 0) + 1
del d[" "]
del d["\n"]
ls = list(d.items())
ls.sort(key=lambda x: x[1], reverse=True)  # 此行可以按照词频由高到低排序
fo.write(",".join(ls))
fo.close()
fi.close()


第二套


1




import random
brandlist = ['三星','苹果','vivo','OPPO','魅族']
random.seed(0)
name = brandlist[random.randint(0,4)]
print(name)


2



import jieba
s = input("请输入一个字符串")
n = len(s) 
m = len(jieba.lcut(s))
print("中文字符数为{},中文词语数为{}。".format(n, m))


3




n = eval(input("请输入数量:"))
if n == 1:
    cost = 150
elif n >= 2 and n <= 3:
    cost = int(n * 150 * 0.9)
elif n >= 4 and n <= 9:
    cost = int(n * 150 * 0.8)
elif n >= 10:
    cost = int(n * 150 * 0.7)
print("总额为:", cost)


4



from turtle import *
for i in range(5):
   fd(200)
   right(144)


5




fo = open("PY202.txt", "w")
data = input("请输入一组人员的姓名、性别、年龄:")  # 姓名 年龄 性别
women_num = 0
age_amount = 0
person_num = 0
while data:
    name, sex, age = data.split(' ')
    if sex == '女':
        women_num += 1
    age_amount += int(age)
    person_num += 1
    data = input("请输入一组人员的姓名、性别、年龄:")
average_age = age_amount / person_num
fo.write("平均年龄是{:.1f} 女性人数是{}".format(average_age, women_num))
fo.close()


6




fi = open("PY301-vacations.csv", "r")
ls = []
for line in fi:
    ls.append(line.strip("\n").split(","))
s = input("请输入节假日名称:")
for line in ls:
    if s == line[1]:
        print("{}的假期位于{}-{}之间".format(line[1], line[2], line[3]))
fi.close()


fi = open("PY301-vacations.csv","r")
ls = []
for line in fi:
    ls.append(line.strip("\n").split(","))
s = input("请输入节假日序号:").split(" ")
while True:
    for i in s:
        for line in ls:
            if i == line[0]:
                print("{}({})假期是{}月{}日至{}月{}日之间".format((line[1]),(line[0]),line[2][:-2],line[2][-2:],line[3][:-2],line[3][-2:]))
    s = input("请输入节假日序号:").split(" ")
fi.close()


fi = open("PY301-vacations.csv","r")
ls = []
for line in fi:
    ls.append(line.strip("\n").split(","))
s = input("请输入节假日序号:").split(" ")
while True:
      for i in s:
            flag = False
            for line in ls:
                  if i == line[0]:
                        print("{}({})假期是{}月{}日至{}月{}日之间".format((line[1]),(line[0]),line[2][:-2],line[2][-2:],line[3][:-2],line[3][-2:]))
                        flag = True
            if flag == False:
                  print("输入节假日编号有误!")
      s = input("请输入节假日序号:").split(" ")
fi.close()


第三套


1




n = eval(input("请输入正整数:"))
print("{:@>30,}".format(n))


2




a = [11, 3, 8]
b = eval(input())  # 例如:[4,5,2]
s = 0
for i in range(3):
    s += a[i] * b[i]
print(s)


3




import random
random.seed(255)
for i in range(5):
    print(random.randint(1,50), end=" ")


4




import turtle
turtle.pensize(2)
d = 72
for i in range(5):
    turtle.seth(d)
    d += 72
    turtle.fd(200)


5




fo = open("PY202.txt", "w")
names = input("请输入各个同学行业名称,行业名称之间用空格间隔(回车结束输入):")
names = names.split(" ")
d = {}
for name in names:
    d[name] = d.get(name, 0) + 1
ls = list(d.items())
ls.sort(key=lambda x: x[1], reverse=True)  # 按照数量排序
for k in ls:
    fo.write("{}:{}\n".format(k[0], k[1]))
fo.close()


6




fi = open("论语.txt", "r")
fo = open("论语-原文.txt", "w")
flag = False
for line in fi:
    if "【" in line:
        flag = False
    if "【原文】" in line:
        flag = True
        continue
    if flag == True:
        fo.write(line.lstrip())
fi.close()
fo.close()


fi = open("论语-原文.txt", ______)
fo = open("论语-提纯原文.txt", ______)
for line in fi:
    for i in range(1,23):
        line=line.replace("({})".format(i), "")
    fo.write(line)
fi.close()
fo.close()


第四套


1




ntxt = input("请输入4个数字(空格分隔):")
nls = ntxt.split(" ")
x0 = eval(nls[0])
y0 = eval(nls[1])
x1 = eval(nls[2])
y1 = eval(nls[3])
r = pow(pow(x1-x0, 2) + pow(y1-y0, 2), 0.5)
print("{:.1f}".format(r))


2




import jieba
txt = input("请输入一段中文文本:")
ls = jieba.lcut(txt)
print("{:.1f}".format(len(txt)/len(ls)))


3




n = eval(input("请输入一个数字:"))
print("{:+^11}".format(chr(n-1)+chr(n)+chr(n+1)))


4




import turtle
d = 0
for i in range(4):
    turtle.fd(200)
    d = d+90
    turtle.seth(d)


5




fo = open("PY202.txt", "w")
data = input("请输入课程名及对应的成绩:")  # 课程名 考分
course_score_dict = {}
while data:
    course, score = data.split(" ")
    course_score_dict[course] = eval(score)
    data = input("请输入课程名及对应的成绩:")
course_list = sorted(list(course_score_dict.values()))
max_score, min_score = course_list[-1], course_list[0]
average_score = sum(course_list) / len(course_list)
max_course, min_course = '', ''
for item in course_score_dict.items():
    if item[1] == max_score:
        max_course = item[0]
    if item[1] == min_score:
        min_course = item[0]
fo.write("最高分课程是{} {}, 最低分课程是{} {}, 平均分是{:.2f}".format(max_course, max_score, min_course, min_score, average_score))
fo.close()


6



fi = open("sensor.txt", "r")
fo = open("earpa001.txt", "w")
txt = fi.readlines()
for line in txt:
    ls = line.strip("\n").split(",")
    if " earpa001" in ls:
        fo.write('{},{},{},{}\n'.format(ls[0], ls[1], ls[2], ls[3]))
fi.close()
fo.close()


fi = open("earpa001.txt", "r")
fo = open("earpa001_count.txt", "w")
d = {}
for line in fi:
    split_data = line.strip("\n").split(",")
    floor_and_area = split_data[-2] + "-" + split_data[-1]
    d[floor_and_area] = d.get(floor_and_area, 0) + 1
    # if floor_and_area in d:
    #     d[floor_and_area] += 1
    # else:
    #     d[floor_and_area] = 1
ls = list(d.items())
ls.sort(key=lambda x: x[1], reverse=True)  # 该语句用于排序
for i in range(len(ls)):
    fo.write('{},{}\n'.format(ls[i][0], ls[i][1]))
fi.close()
fo.close()


第五套




s = eval(input("请输入一个数字:"))
ls = [0]
for i in range(65,91):
    ls.append(chr(i))
print("输出大写字母:{}".format(ls[s]))


2




s = input("请输入一个十进制数:")
num = int(s)
print("转换成二进制数是:{:b}".format(num))


3




import jieba
s = input("请输入一个中文字符串,包含标点符号:")
m = jieba.lcut(s)
print("中文词语数:{}".format(len(m)))


4




import turtle
turtle.color("red","yellow")
turtle.begin_fill()
for i in range(36):
    turtle.fd(200)
    turtle.left(170)
turtle.end_fill()


5




fo = open("PY202.txt","w")
def prime(num):
    #此处可以是多行代码
    for i in range(2, num):
        if num%i==0:
            return False
    return True
ls = [51,33,54,56,67,88,431,111,141,72,45,2,78,13,15,5,69]
lis = []
for i in ls:
    if prime(i) == False:
        lis.append(i)#此处为一行代码
fo.write(">>>{},列表长度为{}".format(lis,len(lis)))
fo.close()


6




fi = open("arrogant.txt","r")
fo = open("PY301-1.txt","w")
txt = fi.read()
d = {}
for s in txt:
    d[s] = d.get(s,0)+1
del d["\n"]
ls =list(d.items())
for i in range(len(ls)):
    fo.write("{}:{}\n".format(ls[i][0],ls[i][1]))
fo.close()
fi.close()


fi = open("arrogant.txt","r")
fo = open("arrogant-sort.txt","w")
txt = fi.read()
d = {}
for s in txt:
    d[s] = d.get(s,0)+1
del d["\n"]
ls =list(d.items())
ls.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
    fo.write("{}:{}\n".format(ls[i][0],ls[i][1]))
fi.close()
fo.close()


目录
相关文章
|
7月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1178 102
|
7月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
454 104
|
7月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
353 103
|
7月前
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
335 82
|
6月前
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
453 3
|
6月前
|
数据处理 Python
Python编程:类型转换与输入输出
本教程介绍Python中输入输出与类型转换的基础知识,涵盖input()和print()的使用,int()、float()等类型转换方法,并通过综合示例演示数据处理、错误处理及格式化输出,助你掌握核心编程技能。
674 3
|
6月前
|
并行计算 安全 计算机视觉
Python多进程编程:用multiprocessing突破GIL限制
Python中GIL限制多线程性能,尤其在CPU密集型任务中。`multiprocessing`模块通过创建独立进程,绕过GIL,实现真正的并行计算。它支持进程池、队列、管道、共享内存和同步机制,适用于科学计算、图像处理等场景。相比多线程,多进程更适合利用多核优势,虽有较高内存开销,但能显著提升性能。合理使用进程池与通信机制,可最大化效率。
471 3
|
6月前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
679 0
|
7月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的多面手
Python:现代编程的多面手
344 0
|
7月前
|
存储 人工智能 算法
Python实现简易成语接龙小游戏:从零开始的趣味编程实践
本项目将中国传统文化与编程思维相结合,通过Python实现成语接龙游戏,涵盖数据结构、算法设计与简单AI逻辑,帮助学习者在趣味实践中掌握编程技能。
642 0

推荐镜像

更多