a=[['a','b','c'],[1,2,3],['d','e','f'],[4,5,6]]
print(a[0:2])
import numpy as np
print(np.linspace(0, 10, 11))
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
class Employee:
employee_count = 0
def __init__(self, name):
self.name = name
Employee.employee_count += 1
在一个动物园管理软件中,你需要创建一个Bird类作为Animal类的子类。创建一个Bird实例时,以下哪个描述是正确的?( )
class Animal:
class Bird(Animal):
def fly(self):
print("Flapping wings")
有Python程序段如下,下列选项错误的是?( )
class Cat():
def __init__(self,name,color):
self.name=name
self.color=color
def sit(self):
print(self.color +self.name+"is sitting.")
有如下Python代码:
with open('data.txt') as f:
data1=f.readline()
print(data1)
with open('data1.txt') as f:
data=f.readline()
with open('data2.txt','w') as f:
f.write(data)
'data1.txt'内容如下图所示,
'data2.txt'文件无内容
执行下列代码,输出结果是?( )
x = [(0,1,2),(3,4,5),(6,7,8)]
a = np.asarray(x)
print (a)
15.使用matplotlib模块绘制如图所示的图像:
import matplotlib.pyplot as plt
x=np.linspace(0,2,50)
plt.plot(x,x**2,label="quadratic")
plt.plot(x,x**3,label="cubic")
plt.xlabel("X axis")
______________________
plt.title("Simple Plot")
plt.legend()
plt.show()
划线处应填入的代码为?( )
如图所示是使用Python编程完成的一组图像,其程序代码如下:
x=np.linspace(-1,1,5)
for i in _______________:
y=x*i
plt.scatter(x ,y)
横线处为一个列表,该列表中有几个元素?( )
有如下Python程序:
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
arr = np.add(a, b)
print(arr[0][1])
运行程序后,输出的结果是?( )
有如下Python程序段:
import sqlite3
conn = sqlite3.connect('cj.db')
cur = conn.cursor()
a= cur.execute("SELECT * FROM student").fetchall()
conn.commit()
conn.close
s=0
for i in a:
s=s+i[1]
pirnt(s)
其中'cj.db'的student表中内容如下图所示,则执行程序后,打印的结果是?( )
cursor = conn.cursor()
cursor.execute('SELECT * FROM student')
rows = cursor.fetchmany(5)
for row in rows:
print(row)
cursor.close()
conn.close()
已知查询的数据表中的数据超过5行,则执行程序后,下列说法正确的是?( )
def f(*args):
print(comboxlist.get()) #打印选中的值
import tkinter as tk
from tkinter import ttk
root=tk.Tk() #构造窗体
comboxlist=ttk.Combobox(root)
comboxlist["values"]=("a","b","c","d")
comboxlist.current(0)
comboxlist.bind("<<ComboboxSelected>>",f)
comboxlist.pack()
以下说法正确的是?( )
from tkinter import *
def close_app():
root.destroy()
root = Tk()
root.geometry('300x200')
root.title('my window')
btn1 = Button(root,text='按钮1',bg='blue',command=close_app)
btn1.pack(side=BOTTOM)
root.mainloop()
执行代码后,说法正确的是?( )
在使用csv.writer向CSV文件写入数据时,newline=''参数的作用是?( )
Python的tkinter库中,使用grid()方法管理布局,需要将Label标签放入第一行第一列的写法是:grid(row=1,column=1) 。( )
JSON数据是纯文本格式,因此它可以很容易地被机器解析和生成。( )
使用json.loads( )函数将Python对象转换为JSON字符串。( )
绘制图形如图所示,画线处的的语句填写是否正确 ?( )
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.plot(x, y)
plt.xlabel('y=x**2')
有一个文本文件(data.txt),该文件中包含多行文本。每行有一个整数。下面这段程序能从该文件中读取每一行的内容,并将其转换为整数,然后求和并输出结果。( )
with open("data.txt") as f:
total = 0
for line in f:
total += int(line)
print(total)
使用matplotlib的 plt.text() 函数可以在图表的任意位置添加文本注释,但是无法控制文本的对齐方式。( )
在Python中,子类可以覆盖父类中的方法,但不能覆盖父类中的属性。( )
readline()函数读取文本文件内容,返回的是一个列表,其中每一行的数据为一个元素。
Python的SQLite库中的execute()方法只能执行SQL查询,不能执行SQL命令。( )
__init__是一个类的构造器,Python中每个类只能有一个__init__方法。( )
计算圆形的面积
编写一个父类Shape,具有一个属性color和一个方法get_area(),用于计算形状的面积。然后,基于Shape类创建子类Circle,表示圆形。子类需要实现父类的方法get_area()来计算自身的面积。
具体要求:
(1)Shape类包含以下属性和方法:
属性:color(字符串类型,表示颜色)
方法:get_area()(计算并返回面积,方法体为空,由子类实现)
(2)Circle类是Shape类的子类,包含以下属性和方法:
属性:radius(浮点数类型,表示圆形的半径)
方法:重写并实现get_area()方法,根据圆形的半径计算并返回面积。
请根据要求,补全代码。
import math
class Shape:
def ①:
self.color = color
def get_area(self):
Pass
class Circle( ② ):
def ③ :
super().__init__(color)
self.radius = radius
return math.pi * self.radius ** 2
circle = ④ ("blue", 2)
print(⑤ )
学生数据库
编写程序操作SQLite数据库,并读出表中的数据。
具体要求如下:
(1)打开数据库连接;
(2)清除已存在的表 -students;
(3)创建一个表students;
(4)向新表插入数据;
(5)读取表students中数据。
(本题无需运行通过,写入代码即可)
#打开数据库连接
conn = sqlite3. ① ('test.db')
print("Opend database successfully")
#清除已存在的表 -students
conn. ② ('''DROP TABLE students''');
conn. ③
#创建一个表students
conn.execute(''' ④ students
(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL);''')
print("Table created successfully");
#插入数据
conn.execute("INSERT INTO students(ID,NAME,AGE) VALUES(1,'Allen',25)");
conn.execute("INSERT INTO students(ID,NAME,AGE) VALUES(2,'Maxsu',20)");
conn.execute("INSERT INTO students(ID,NAME,AGE) VALUES(3,'Teddy',24)");
print("Records Insert successfully");
print("-------------------");
#读取表students
⑤ =conn.execute("SELECT * from students")
print ("ID NAME AGE")
for it in cursor:
for i in range(len(it)):
print(it[i])
print ('\n')
进制问题
如图所示为'data1.txt'中存储的数据,其中每一行都为24个由'0'和'1'组成的二进制数字,现编写Python程序读取'data1.txt'文件中的数据,并将二进制数字转换成十进制数字,转换规则为每八位二进制数字转换为一个十进制数,相应的Python代码如下,请补充完整。
f=open('/data/ ① ','r')
line=f.readline().strip('\n')
s1=''
while line:
for i in range(len(line)):
s=s*2+ ②
if (i+1)%8==0:
s1=s1+str(s)+','
③
s1=s1+'\n'
line= ④ .strip('\n')
print(s1)