Python实验报告

Python实验报告

我很菜

控制流 编程练习1

1 两个变量的最大值

变量a = 1,变量b = 2 打印输出两者之间的最大值

1
2
3
4
5
6
a=1
b=2
if a>b:#如果a大输出a
print(a)
else:#否则输出b
print(b)

2 三个变量的最大值

变量a = 1,变量b = 3,变量c = 2
打印输出三者之间的最大值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
a=1
b=3
c=2
#先比较a b
if a>b:
max=a
else:
max=b
#比较a b中最大值和c
if max<c:
max=c
else:
pass
print (max)

3 计算累加和

计算从1加到100的累加和

1+2+3+ … +98+99+100

1
2
3
4
5
6
a=0
i=1
while i<=100:#循环100次
a=a+i
i=i+1
print(a)

4 计算6的阶乘

计算1 *2 *3 *4 *5 *6
要求使用while循环

1
2
3
4
5
6
a=1
i=1
while i<=6:
a=a*i
i=i+1
print(a)

5打印输出如下图形

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
要求使用while循环

1
2
3
4
5
6
7
8
9
10
11
12
i=1
j=1
while i<=5:#有5行
while j<=i:
if j<i:
print (j,end=' ')#控制空格
else:
print (j)
j=j+1
j=1
i=i+1

控制流 编程练习2

1 计算累加和

计算从1加到100的累加和
1+2+3+ … +98+99+100
要求使用for循环

1
2
3
4
a=0
for i in range(1,101):#从1取到100
a=a+i
print (a)

2 打印构成一个整数的所有数字

打印输出构成该整数的所有数字
要求先打印最高位,再打印最低位
例子
整数12306由5个数字构成
输出结果为
1
2
3
0
6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
a=int(input())#输入
b=0
i=0
#获得倒序数b
while a>0:
b=b*10+a%10#在b最后后面获得a的最后一位
a=int(a/10)#去除最后一位
i=i+1
#逐位输出b
while i>0:
c=b%10
print(c)
b=int(b/10)
i=i-1

3 矩阵打印

使用嵌套的列表表示一个二维矩阵
[[1,2,3],[4,5,6],[7,8,9]]
将以上列表表示的矩阵输出如下
1 4 7
2 5 8
3 6 9

1
2
3
4
5
6
7
8
9
10
11
12
a=[[1,2,3],[4,5,6],[7,8,9]]
i=0
j=0
c=0
#通过a[i][j]访问每个数字
for i in range(3):
for j in range(3):
if j<=2:
print(a[i][j],end=' ')
else:
print(a[i][j],end='')
print()

4 打印输出如下乘法口诀表

1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
使用for循环

1
2
3
4
5
6
7
8
for a in range(1,5):
#每行都是1到a乘以a
for b in range(1,a)
if b<a:
print('%dX%d=%d'%(b,a,a*b),end=' ')
else:
print('%dX%d=%d'%(b,a,a*b))
print()

5 已知列表[1,2,3,4,50,6,7,8,9]

编程找出最大的值

1
2
3
4
5
6
7
8
a=[1,2,3,4,50,6,7,8,9]
max=0
for b in a:#用for来遍历每个数字
if b>max:
max=b
else:
pass
print(max)

6已知列表[1,9,2,8,3,7,4,6,5]

将列表按从小到大进行排序

1
2
3
4
5
6
7
8
9
10
11
12
13
a = [1,9,2,8,3,7,4,6,5]
i=0
#冒泡排序
while i<=len(a)-2:
j=i+1
while j<=len(a)-1:
if a[i]>a[j]:#交换
c=a[j]
a[j]=a[i]
a[i]=c
j=j+1
i=i+1
print (a)

函数 编程练习1

1 编写函数max(a,b)

返回参数a和参数b之间的最大值

1
2
3
4
5
6
def max(a,b):
if(a>b):
return a
else:
return b
print(max(3,4))#测试

2 编写函数max3(a,b,c)

返回参数a、参数b和参数c之间的最大值
要求通过调用上一题中的函数max(a,b)来实现

1
2
3
4
5
6
7
8
9
10
11
12
13
def max(a,b):
if(a>b):
return a
else:
return b
def max3(a,b,c):
#输出ab最大值与bc最大值较大一个
if(max(a,b)>max(b,c)):#调用max
return max(a,b)
else:
return max(b,c)
print(max3(1,2,3))#测试

3 实现如下程序中的函数sum(start, end)

  • 计算范围[first, last]内的整数的累加和

def sum(start, end):

  • 打印1+2+ … + 10的累加和,预期输出为55

print(sum(1, 10))

  • 打印1+2+ … + 100的累加和,预期输出为5050

print(sum(1, 100))

1
2
3
4
5
6
7
8
def sum(a,b):
c=0
for i in range(a,b+1):#i取从a到b
c+=i
return c
#测试
print(sum(1,10))
print(sum(1,100))

4 实现如下程序中的函数sort(list)和函数dump(list)

list0 = [1,3,2,4]
list1 = [11,33,22]

  • 对列表中的元素进行从小到大的排序

def sort(list):

  • 打印列表中的元素

def dump(list):
sort(list0)
dump(list0) # 将排序后的列表输出
sort(list1)
dump(list1) # 将排序后的列表输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
list0 = [1,3,2,4]
list1 = [11,33,22]
def sort(list):
#冒泡排序
for i in range(0,len(list)-1):
for j in range (0,len(list)-i-1):
if list[j]>list[j+1]:
a=list[j]
list[j]=list[j+1]
list[j+1]=a
return list
def dump(list):
#控制输出
for i in range(0,len(list)):
if i<len(list)-1:
print(list[i],end=' ')
else:
print(list[i])
#测试
sort(list0)
dump(list0)# 将排序后的列表输出
sort(list1)
dump(list1)# 将排序后的列表输出

5 实现如下程序中的函数mul_matrix和函数dump_matrix

  • multiply实现对矩阵a和矩阵b进行相乘

def mul_matrix(a, b):

  • 打印矩阵x

def dump_matrix(x):

a = [[1,1],
&emsp;&emsp;[1,1]]
b = [[1,2],
&emsp;&emsp;[3,4]]
c = mul_matrix(a, b)
dump_matrix(c)
x = [[1,2,3],
&emsp;&emsp;[4,5,6]]
y = [[1,4],
&emsp;&emsp;[2,5],
&emsp;&emsp;[3,6]]
z = mul_matrix(x, y)
dump_matrix(z)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def mul_matrix(a, b):
n=0
c=[]
for g in range(len(a)):
m=[]
for i in range(len(b[0])):
for j in range(len(a[0])):
n+=a[g][j]*b[j][i]#计算元素n
m.append(n)#将元素放入[]中
n=0#初始化
c.append(m)
return c
def dump_matrix(x):
#打印矩阵
print('[',end='')
for j in range(len(x)):
if j ==0:
print(x[j],end=',\n')
elif j<len(x)-1:
print('',x[j],end=',\n')
else:
print('',x[j],end='')
print(']')
a = [[1,1],
[1,1]]
b = [[1,2],
[3,4]]
c = mul_matrix(a, b)
dump_matrix(c)#打印矩阵
x = [[1,2,3],
[4,5,6]]
y = [[1,4],
[2,5],
[3,6]]
z = mul_matrix(x, y)
dump_matrix(z)#打印矩阵

项目

  • 实现一个通讯录管理程序
  • 综合使用复合数据结构、控制流、函数等知识点
  • 提供4项功能
  • 创建联系人
  • 删除联系人
  • 查询联系人
  • 列出所有联系人
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def create(a):#创建一个联系人
b={}
#按要求输入信息
print('name:',end=' ')
b['name']=input()
print('address:',end=' ')
b['address']=input()
print('phone:',end=' ')
b['phone']=input()
a.append(b)
a[0]+=1
return a #传递a
def delete(a):#删除联系人
temp=input()
for j in range(1,a[0]):
if a[j]['name']==temp:
del a[j]
a[0]-=1#人数减少
break#防止超出范围
return a
def query(a):#查询联系人
temp=input()
for j in range(1,a[0]):
if a[j]['name']==temp:
print(a[j]['name'],end=' ')
print(a[j]['address'],end=' ')
print(a[j]['phone'])
return a
def listall(a):#列出所有联系人
for j in range(1,a[0]):
print(a[j]['name'],end=' ')
print(a[j]['address'],end=' ')
print(a[j]['phone'])
return a
def exit():#退出
quit()
def begin(a):#主程序
print("1. create person")
print("2. delete person")
print("3. query person")
print("4. list all persons")
print("5. quit")
print("Enter a number(1-5):",end='')
lime=int(input())
#分类讨论
if lime==1:
a=create(a)
elif lime==2:
a=delete(a)
elif lime==3:
a=query(a)
elif lime==4:
a=listall(a)
elif lime==5:
exit()
else:
pass
begin(a)#自己调用自己来保证不断循环
a=[]#初始化
i=1#计联系人数
a.append(i)
begin(a)#进入程序

面向对象 编程练习1

1 编写Person类,能够通过如下测试代码

class Person:

  • 以下为测试代码

tom = Person(‘tom’, 10)
tom.greet()

  • 程序输出如下:

Hello, my name is tom, I’m 10 years old

  • 以下为测试代码

jerry = Person(‘jerry’, 12)
jerry.greet()

  • 程序输出如下:

Hello, my name is jerry, I’m 11 years old

2使用面向对象的方法,重新实现通讯录的程序,实现提示如下:

class Person:
包括三个属性: name address phone
使用类描述一个人的所有信息
class AddressBook:
&emsp;def __init__:
&emsp;&emsp;&emsp;self.persons = [ ]
&emsp;def addPerson:
&emsp;def delPerson:
&emsp;def queryPerson:
&emsp;def listAllPerson:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Person:#类
def __init__(self,name,age):#初始化
self.name = name
self.age = age
def greet(self):
print("Hello, my name is %s, I'm %d years old"%(self.name,self.age))
tom = Person('tom', 10)
tom.greet()
# 程序输出如下:
# Hello, my name is tom, I'm 10 years old
jerry = Person('jerry', 12)
jerry.greet()
# 程序输出如下:
# Hello, my name is jerry, I'm 12 years old
  • 程序主循环如下:

初始化addressBook
while True:
&emsp;if 用户选择功能“创建联系人”
&emsp;&emsp;addressBook.addPerson
&emsp;if 用户选择功能“删除联系人”
&emsp;&emsp;addressBook.delPerson
&emsp;if 用户选择功能“查询联系人”
&emsp;&emsp;addressBook.queryPerson
&emsp;if 用户选择功能“列出所有联系人”
&emsp;&emsp;addressBook.listAllPerson

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Person:#类:联系人
def __init__(self,name,address,phone):
self.name = name
self.address = address
self.phone = phone
def introduce(self):#介绍自己
print(self.name,self.address,self.phone)
class AddressBook:#类:通讯录
def __init__(self):
self.persons = []#用于储存
self.number = 0#用于统计人数
def addPerson(self):#添加联系人
name = input("name:")
address = input("address:")
phone = input("phone:")
person = Person(name,address,phone)
self.persons.append(person)
self.number+=1#增加人数
def delPerson(self):#添加联系人
name = input("name:")
for i in range(self.number):
if self.persons[i].name == name:
del self.persons[i]
self.number-=1#减少人数
break
def queryPerson(self):#查询联系人
name = input()
for i in range(self.number):
if self.persons[i].name == name:
self.persons[i].introduce()#调用介绍函数
break
def listAllPerson(self):
for i in range(self.number):
self.persons[i].introduce()
addressBook = AddressBook()#初始化
while True:#保证程序不断重复运行
g=input("1. create person\n2. delete person\n3. query person\n4. list all persons\n5. quit\n")#相当于先输出在读入
if g=='1':
addressBook.addPerson()
elif g=='2':
addressBook.delPerson()
elif g=='3':
addressBook.queryPerson()
elif g=='4':
addressBook.listAllPerson()
elif g=='5':
exit(0)

面向对象 编程练习2

完成一个形体管理的程序
创建三角形、矩形、正方形
查询形体的周长

  • 命令help列出程序支持的所有命令
  • 命令triangle创建一个三角形
  • 命令square创建一个正方形
  • 命令triangle创建一个矩形
  • 命令count统计形体类型的数量
  • 命令query使用名称查询形体的基本信息
  • 命令circle使用名称查询形体的周长
  • 命令quit退出程序,回到linux命令行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class Shape:#父类
def __init__(self, name):
self.name = name
class Triangle(Shape):
def __init__(self, name, a, b, c):
Shape.__init__(self, name)#继承父类
self.a = a
self.b = b
self.c = c
def query(self):#介绍自己
print('''This is triangle
Sides is %d %d %d'''%(self.a,self.b,self.c))
def circle(self):#输出边长
print('Circle is %d'%(self.a+self.b+self.c))
class Square(Shape):
def __init__(self, name, side):
Shape.__init__(self, name)#继承父类
self.side = side
def query(self):#介绍自己
print('''This is square
Sides is %d'''%(self.side))
def circle(self):#输出边长
print('Circle is %d'%(self.side*4))
class Rectangle(Shape):
def __init__(self,name,width,height):
Shape.__init__(self, name)#继承父类
self.width = width
self.height = height
def query(self):#介绍自己
print('''This is square
Sides is %d %d'''%(self.width,self.height))
def circle(self):#输出边长
print('Circle is %d'%(self.width*2+self.height*2))

shapes = []#初始化
#下面三个变量用于计数
tr = 0
sq = 0
re = 0
while True:#保证程序不断重复运行
command = input('> ')
args = command.split()#将输入分为几个片段
#下面开始分支
if args[0] == 'help':
#通过两个'''多行输出内容
print('''triangle name a b c
square name side
rectangle name width height
query name
count
circle name
quit''')
if args[0] == 'triangle':
triangle = Triangle(args[1],int(args[2]),int(args[3]),int(args[4]))
shapes.append(triangle)#放入shapes中
tr+=1
if args[0] == 'square':
square = Square(args[1],int(args[2]))
shapes.append(square))#放入shapes中
sq+=1
if args[0] == 'rectangle':
rectangle = Rectangle(args[1],int(args[2]),int(args[3]))
shapes.append(rectangle))#放入shapes中
re+=1
if args[0] == 'query':
for i in range(len(shapes)):
#遍历 当找到名字时
if shapes[i].name == args[1]:
shapes[i].query()
if args[0] == 'circle':
for i in range(len(shapes)):
#遍历 当找到名字时
if shapes[i].name == args[1]:
shapes[i].circle()
if args[0] == 'count':
print('''triangles %d
square %d
rectangle %d'''%(tr,sq,re))
if args[0] == 'quit':
exit()