选择题 共25道
判断题 共10道
编程题 共3道
在Python中,打开文件并读取所有内容的正确方法是?( )
with open('file.txt', ___) as f:
content = f.read()
在Python中,以下代码的执行结果是?( ) with open('test.txt', 'w+') as f: f.write('HelloWorld') f.seek(5) print(f.read(2))
在Python中,以下关于文件追加操作的描述,正确的是?( )
在Python中,以下哪个选项可以正确写入内容到文件?( ) with open('file.txt', 'w') as f: f.___('Hello World')
编写程序绘制如下图形,①处可以填入的内容是? ( )
import numpy as npfrom matplotlib import pyplot as plt x = np.arange( ① ) y = [12,16,17,12,16,17,12] plt.plot(x, y) plt.show()
已知字母Z的点阵和坐标如下图所示,编写程序用绘制点的方式描出字母Z,关于程序说法正确的是? ( )
import matplotlib.pyplot as plt x1=[2,3,4,5,6,7,8,9] y1=[9,9,9,9,9,9,9,9] x2=① y2=[1,2,3,4,5,6,7,8] x3=[2,3,4,5,6,7,8,9] y3=[0,0,0,0,0,0,0,0] plt.scatter(x1,y1,marker='o',s=150) plt.scatter(x2,y2,marker='o',s=150) plt.scatter(x3,y3,marker='o',s=150) plt.show()
绘制一条用星星组成的直线,程序说法错误的是? ( )
import numpy as np from matplotlib import pyplot as plt x = np.arange(10) y = x + 5 plt.plot(x, y,'①', markersize=30) plt.show()
关于绘制如下坐标图的程序,说法错误的是? ( )
import numpy as np
import numpy as npimport matplotlib.pyplot as plt plt.xlim(0,①) plt.xticks(list(range(0,11))) plt.ylim(0,10) plt.yticks(list(range(0,11))) ax = plt.gca() ax.set_aspect('equal') ax.grid(True) plt.show()
在Python中,下面的选项中能查询users表前10条记录的SQL是?( )
在Python中,下面代码可以正确连接SQLite数据库的是?( )
在Python的SQLite中,用于执行SQL语句的方法是?( )
由下列Python代码:
import sqlite3
conn = sqlite3.connect("d:/stu.db")
cur = conn.cursor( )
cur.execute("SELECT * FROM students")
data = cur.fetchall()
cur.close()
conn.close()
可知当前的数据表名是?( )
如果要将以下Python数据结构保存成JSON格式,应该使用哪个方法?( ) student = {"name": "张三", "age": 15, "score": 95}
在处理CSV文件时,下面哪种文件打开模式最常用?( )
下列关于JSON格式的描述,正确的是?( )
假如有以下二维列表: data = [ ["姓名", "数学", "英语"], ["张三", 90, 85], ["李四", 80, 70] ] 使用Python写入data.csv文件,应该选择的代码是?( )
假设你读取了一个CSV文件,内容如下: 姓名,成绩 小明,90 小红,85 想将它读成字典,应该使用 csv 库中的哪个方法最合适? ( )
下列有关类和对象的概念,说法正确的是?( )
你在开发一个宠物管理系统,使用面向对象的方式来表示不同的动物,运行后输出的结果是?( )
class Animal: def __init__(self, name): self.name = name def speak(self): return "发出声音" class Dog(Animal): def speak(self): return f"{self.name} 汪汪叫" dog = Dog("旺财") print(dog.speak())
在一个学校管理系统中,有一个基类 Person 表示人员,有属性 name 和 age,还有一个子类 Student 继承自 Person,并额外有属性 student_id。以下代码的空白处应填入什么来正确初始化子类实例?( )
class Person: def __init__(self, name, age): self.name = name self.age = age class Student(Person): def __init__(self, name, age, student_id): ____ self.student_id = student_id # 创建学生实例 student = Student("小明", 18, "001")
有如下Python程序代码:
class Person(): # ①
def __init__(self, name, age): # ②
self.name = name # ③
self.age = age
zs = Person("张三", "15") # ④
下列说法正确的是?( )
在tkinter中,以下哪个方法用于设置窗口的大小?( )
在tkinter中,按钮(Button)添加到窗口后,若要让它显示,必须调用哪个方法?( )
在设计用户登录界面时,输入框用哪个组件实现用户信息输入?( )
下列关于布局管理器的描述,正确的是? ( )
在Python中,下面这段代码可以正确读取二进制文件并获取前10个字节。( ) with open('data.bin', 'r', encoding='utf-8') as f: data = f.read(10)
在Python语言中,语句numpy.arange(10)和range(10)生成的序列是相类似的。( )
在Python的matplotlib库中,plt.bar()函数可用来绘制散点图。( )
在Python语言的SQLite中,DROP语句用于删除表中的记录。( )
JSON格式的数据可以直接通过Python的字典操作进行修改。( )
二维列表可以直接通过open().write()方法写入文本文件,不需要额外处理。( )
在 Python 里,当子类继承父类后,子类只能使用自己定义的属性和方法,不能访问父类的属性和方法。( )
某车辆租赁系统中有如下代码段; class Vehicle: def start_engine(self): return "引擎启动" class ElectricCar(Vehicle): def start_engine(self): return "电机启动" car = ElectricCar() print(car.start_engine()) # 输出: 电机启动 在车辆租赁系统中,ElectricCar类通过重写Vehicle类的start_engine方法,实现了子类对父类方法的覆盖,这体现了面向对象中“继承允许子类定制父类行为”的特性。( )
在Python语言的tkinter中,Label组件只能用来显示文字,不能用来显示图片。( )
Python中使用'w'模式打开文件时,如果文件已存在会抛出异常。( )
数据合并
如下图所示,学校食堂要将大米的入库和出库数据,合并到文件“合并.csv”中。其中入库数据文件为"入库.csv",出库数据文件为"出库.csv",文件"入库.csv"、文件"出库.csv"和“合并.csv”都放置在程序所在的文件夹,请补全代码。
import csv in1=[] in2=[] with open('/data/入库.csv') as f1: rows = list( ① ) for a in rows: in1. ② with open('/data/出库.csv') as f2: rows = list( csv.reader (f2)) for a in rows: in2. ③ with open('合并.csv', 'w', encoding='utf_8_sig', newline='') as f3: writer = csv.writer(f3) for a in in1: writer.writerow(a) for a in in2: writer. ④
存取款操作
创建一个名为"BankAccount"的类,要求:(1)有构造函数(init),接受账户名和初始余额; (2)有deposit(amount)方法用于存款; (3)有withdraw(amount)方法用于取款(余额不足时提示); (4)有display()方法显示账户信息; (5)创建一个实例,进行存款、取款操作,并显示最终账户信息。
请根据要求,补全代码。
class BankAccount: def ① (self, account_name, initial_balance): self.account_name = account_name self.balance = ② def deposit(self, amount): if amount > 0: ③ print(f"成功存入{amount}元") else: print("存款金额必须大于0") def withdraw(self, amount): if amount > self.balance: print("余额不足") else: ④ print(f"成功取出{amount}元") def display(self): print(f"账户名:{self.account_name}") print(f"当前余额:{ ⑤ }元") account = BankAccount("张三", 1000) account.deposit(500) account.withdraw(200) account.withdraw(2000) # 测试余额不足情况 account.display()
成绩数据库
现有学生成绩数据库school.db,包含表结构:
CREATE TABLE scores (id INT, name TEXT, math INT, class TEXT)
请补全代码完成以下功能:
1.修改指定班级学生的数学成绩
2.统计各班数学成绩>120分的学生人数
3.输出格式化结果
(本题无需运行通过,写入代码即可)
conn = sqlite3.connect('school.db')
cursor = conn.cursor()
# 任务1:修改成绩(示例:将1班张三数学成绩改为135)
update_sql = "UPDATE scores SET math = ? WHERE class = ? AND name = ?"
cursor.execute(update_sql, (135, '1班', '张三'))
①
# 任务2:统计各班数学120高分人数cursor.execute("SELECT class FROM scores WHERE math > 120")
results = ②
class_counts = {}
for row in results:
class_name = row[0]
if class_name in class_counts:
③
else:
class_counts[class_name] = 1
# 任务3:格式化输出结果print("各班数学高分统计:")
for class_name, count in ④ :
print(f"{class_name.ljust(6)}: {'★'*count}")
cursor.close()
⑤