选择题 共25道
判断题 共10道
编程题 共3道
运行下面代码的正确结果是?( )
with open("example.txt", "a") as file:
file.write("I see you.")
其中example.txt文件内容如下:
This is an example.
在 Python 中,以下哪个函数可以用于创建一个新的文件?( )
filename = "example.txt"
line_count = 0
with open(filename, "r") as file:
for line in file:
line_count += 1
print(f"The file 'example' has {line_count} lines.")
My Favorite Animal
Once upon a time, I had a pet dog named Max.
Max was the most obedient dog I knew.
We played fetch in the park, went on long walks in the woods, and even took naps together on lazy
afternoons.
with open("myfile.txt", "w") as out_file:
out_file.write("This is my first Python program.")
with open("myfile.txt", "r") as in_file:
myfile = in_file.read()
print(myfile)
其中myfile.txt文件内容如下:
Hello World!
编写程序绘制如下图所示的直线,程序空白处应填?( )
import matplotlib.pyplot as p
import numpy as np
x= np.array([0,1,2,____,4,5])
p.plot(x,'o:r')
p.show()
已知程序1绘制的图形如下图所示,要绘制相同的图形,请补全程序2空白?( )
程序1:
x= np.array([0,1,0,1,0,1,0])
程序2:
x= np.array([3,4,3,____,3,4,3])
在命令行窗口分别运行以下代码,输出结果是?( )
>>>import numpy as np
>>>np.full(6,'6')
运行以下关于二维数组读取的程序,输出结果是?( )
a=[[1,2,3],[4,5,6],[7,8,9]]
print(a[1][2])
运行以下代码,绘制出来的第六个柱形图颜色是?( )
x=np.array(['a','b','c','d','e','f'])
h=np.array([1,4,5,6,4,3])
c=np.array(['red','blue','green'])
p.bar(x=x,height=h,color=c)
关于JSON格式数据转为Python数据格式,运行以下程序,输出结果是?( )
import json
a='{"name": "张三", "age": 30, "city": "北京"}'
b=json.loads(a)
c=list(b.keys())
d=list(b.values())
print(d[2])
下列哪个选项不能在SQLite数据库中运行?( )
CREAT TABLE Users (id,name,password,role)
关于上述语句,说法错误的是?( )
import sqlite3
conn = sqlite3.connect('./mydb.sqlite')
cur = conn.cursor()
sql = '''INSERT INTO Users (name,password,role) VALUES (?,?,?)'''
cur.execute(sql,('admin','123456','管理员'))
cur.execute(sql,('user','123456','普通用户'))
conn.commit()
关于以上代码,说法错误的是?( )
有如下Python程序段:
n=3
m=2
dp=[[0 for i in range(n)]for j in range(m)]
dp.append([0,0,n-m])
dp.insert(-1,[n for i in range(n)])
print(dp)
执行程序后,下列选项中值为1的是?( )
有如下python程序段:
import random
a=[0]*6
for i in range(1,6):
tmp=random.randint(5,24)
if tmp%2==0 or i%2==1:
a[i]=a[i-1]+tmp
print(a)
执行程序后,列表a的值可能是?( )
conn = sqlite3.connect('mydatabase.db')
c = conn.cursor()
c.execute("SELECT * FROM users WHERE age > ?", (30,))
results = c.fetchall()
for row in results:
print(row)
conn.close()
上述代码会查询users表中哪些人的年龄?( )
class Person():
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
return f"My name is {self.name} and I am {self.age} years old."
p = Person("Alice", 30)
print(p.introduce())
以上Python代码,运行结果是?( )
下列代码的输出结果是?( )
class MyClass():
def __init__(self):
self.x = 10
self.y = 20
def add(self):
return self.x * self.y
obj = MyClass()
print(obj.add())
下列代码中,c.method1()和c.method2()的输出结果分别是?( )
class Parent():
def method1(self):
return "Parent's method1"
class Child(Parent):
return "Child's method1"
def method2(self):
return super().method1()
c = Child()
print(c.method1())
print(c.method2())
下列有关该代码的说法中,不正确的是?( )
class Jdage():
def __init__(self,name,age):
def jd(self):
if self. age<18:
print(self.name+"还未成年。")
else:
print(self.name+"已成年")
my_stu =Jdage("Peter",26)
my_stu.jd()
你正在为一个小型游戏设计界面,需要一个按钮,玩家点击后会显示一个消息表示游戏开始。如何绑定一个函数到按钮点击事件,以便在点击时执行?( )
你想创建一个简单的程序,显示一个窗口,用于收集用户的反馈。下列哪个选项是正确的方式来创建一个窗口并运行它?( )
你正在开发一个图书管理系统,需要在界面上显示“书名”这个词。如何添加一个标签控件到你的窗口中显示文本“书名”?( )
在一个注册界面中,你需要将一个按钮放置在窗口的底部中央。下列哪个布局管理器最适合实现这个需求?( )
file=open('data1.csv')
file1=csv.reader(file)
next(file1)
for i in file1:
print(_______)
'data1.csv'文件的内容如下图,若要打印每个同学的数学成绩,划线处的代码是?( )
在Python中,可以使用 with 语句来自动关闭一个文件。( )
下面代码的输出结果是:Hello World! ( )
file = open("exam.txt")
print(file)
file.close()
sqlite3.connect('路径/文件名'),如果文件不存在,connect函数会自动创建这个数据库文件。( )
json.dumps() 用于将 Python 对象编码成 JSON 字符串。( )
json.loads()用于将json字符串恢复成Python对象。( )
在Python的matplotlib库中,plt.scatter()函数可用来绘制散点图。( )
下列Python代码中,self参数的作用表示MyClass类的一个实例。 ( )
def my_method(self, other_arg):
print(self, other_arg)
obj.my_method("Hello")
下列代码的输出结果是5。( )
class A():
self.value = 10
class B(A):
super().__init__()
self.value += 5
b = B()
print(b.value)
阅读以下代码,请问图表中会显示2条曲线。( )
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, label='sin(x)')
plt.plot(x, y2, label='cos(x)')
plt.legend()
plt.show()
你正在为一个小型的图书管理系统设计界面,其中包括一个“添加图书”按钮,用户点击后可以将新书信息添加到系统中。点击Button控件可以触发一个函数或方法。( )
运动时长统计
汪龙对不同年龄段的人群进行运动时长的调研,数据存储在文件“sport_240622.csv”中,数据内容如下图显示:
用Python编写一段代码帮他完成本次调用的运动时长的统计。具体要求如下:
(1)读取文件sport_240622.csv中的姓名、年龄、运动时长;
(2)输出年龄在40岁以上(含40岁)人群的平均运动时长(保留2位小数);
请根据要求,补全代码。
import csv
with open("/data/sport_240622.csv") as f:
rows = list( ① )
s=0
c=0
pj=0
for row in rows[1:]:
if( ② ):
s=s+ ③
④
pj=s/c
print( ⑤ )
创建学生库
使用Python的sqlite3库完成以下问题。
(1)创建一个名为students的数据库;
(2)在这个数据库中,创建一个名为students_table的表,包含以下字段:id(主键),name(学生的名
字),age(学生的年龄),grade(学生的年级);
(3)向students_table中插入至少5个学生的数据;
(4)查询年龄大于18岁的所有学生,并打印结果;
(5)将名字为"Alice"的学生的年龄增加1岁;
(6)删除名字为"Bob"的学生。
(本题无需运行通过,写入代码即可)
conn = sqlite3.connect(' ① ')
cursor = conn.cursor()
cursor.execute(''' ② students_table(id INTEGER PRIMARY KEY AUTOINC
students = [ ('Alice', 17, '10th'), ('Bob', 18, '11th'), ('Charlie', 16, '10th'),
cursor.executemany('''INSERT INTO students_table (name, age, grade) VALUES (?, ?,
cursor.execute('SELECT * FROM students_table ③ ')
print("年龄大于18岁的学生:")
print(cursor. ④ )
cursor.execute('UPDATE students_table SET age = age + 1 WHERE name = "Alice"')
cursor.execute('DELETE FROM students_table WHERE name = "Bob"')
BMI计算器
BMI,身体质量指数,在一定程度反映了人体密度。BMI的计算方法是:体重(kg)除以身高(m)的平方。一般情况下,我国成年人身体质量指数在18.5至23.9内属正常范围,低于18.5表示体重偏瘦,高于23.9表示体重偏胖。
利用类编写程序计算BMI指数,当输入体重和身高后,输出BMI值,并输出体形情况:偏瘦、偏胖、体形标准。程序部分运行情况如下图所示,请补全以下代码。
class Boy():
def __init__(self,tizhong,shengao):
self.tizhong=tizhong
self.shengao=shengao
def bmi(self):
return ①
a=int(input('输入体重(kg):'))
b= ②
c=Boy(a,b)
if c.bmi()<18.5:
print("BMI:%d偏瘦。" % c.bmi())
elif ③ :
print("BMI:%d偏胖。" % c.bmi())
print("BMI:%d体形标准。" % c.bmi())