当前位置:   article > 正文

python章节练习题_第十三章 第二周python练习题

使用字典的方式,按如下要求编写程序。 (1)假设学生有两个属性:姓名、考试分数(包

#coding=gbk

##习题1:

print "##习题1##----------------------------------------------\n"

#列表

a = [11,22,24,29,30,32]

print "#1 把28插入到列表的末端\n"

a.insert(-2,"28")

print a

a.append(28)

print a

print "#2 在元素29后面插入元素57\n"

a.insert(4,"57")

print  a

print "#3 把元素11修改成6\n"

a[0] = 6

print a

print "#4 删除元素32\n"

del a[-1]

print a

a.pop(-2)

print a

print "#5 对列表从小到大排序\n"

a.sort()

print a

print "##习题2##----------------------------------------------\n"

#列表

b = [1,2,3,4,5]

print "#1 用2种方法输出下面的结果:"

print "#[1,2,3,4,5,6,7,8]\n"

print "#方法一"

a = [6,7,8]

b.extend(a)

print b

print "#方法二"

c = b + [6,7,8]

print c

print "#2 用列表的2种方法返回结果:[5,4]\n"

print "#方法一"

b = [1,2,3,4,5]

a = b[3:]

a.reverse()

print a

print "#方法二"

a = [b[x] for x in range(len(b)) if x >2]

a.sort(reverse=True)

print a

print "#方法三"

print b[-1:-3:-1]

print "方法四"

d = []

d.append(b.pop())

d.append(b.pop())

print d

print "#3 判断2是否在列表里"

if 2 in b:

print "2 in b"

else:

print "2 not in b"

print "##习题3:"

b = [23,45,22,44,25,66,78]

print "#用列表解析完成下面习题:\n"

print "#1 生成所有奇数组成的列表\n"

print [x  for x in b  if x %  2 == 1 ]

print "#2 输出结果: ['the content 23','the content 45']\n"

print ['the content %s' % b[x]  for x in range(len(b)) if x < 2 ]

print ['the content %s' % x for x in b[0:2:1]]

print "#3 输出结果: [25, 47, 24, 46, 27, 68, 80]\n"

print [x + 2  for x in b ]

print "##习题4:#用range方法和列表推导的方法生成列表:#[11,22,33]\n"

print [m * 11 for m in range(1,4,1)]

print [x  for x in xrange(34) if x >10 and x % 11 == 0]

print range(11,34,11)

print "##习题5:"

#已知元组:

a = (1,4,5,6,7)

print "#1 判断元素4是否在元组里"

if 4 in a:

print "4 in a"

else:

print "4 not in a"

print "#2 把元素5修改成8"

b = list(a)

b[2] = 8

a = tuple(b)

print a

print "##习题6:"

print "#已知集合:setinfo = set('acbdfem')和集合finfo = set('sabcdef')完成下面操作:\n"

setinfo = set('acbdfem')

finfo = set('sabcdef')

print "#1 添加字符串对象'abc'到集合setinfo\n"

setinfo.add('abc')

print setinfo

print "#2 删除集合setinfo里面的成员m\n"

setinfo.remove('m')

print setinfo

print "#3 求2个集合的交集和并集\n"

print setinfo | finfo #并集

print setinfo & finfo #交集

print """##习题7:

用字典的方式完成下面一个小型的学生管理系统。

1学生有下面几个属性:姓名,年龄,考试分数包括:语文,数学,英语得分。

#比如定义2个同学:

#姓名:李明,年龄25,考试分数:语文80,数学75,英语85

#姓名:张强,年龄23,考试分数:语文75,数学82,英语78

"""

student_info = {'liming':{'name':'李明',"年龄":25,"分数":\

{"语文":80,"数学":75,"英语":85}}}

student_info['zhangqiang'] = {'name':'张强','年龄':23,'分数':\

{'语文':75,'数学':82,'英语':78}}

print "#2 给学生添加一门python课程成绩,李明60分,张强:80分\n"

student_info['liming']['分数']['python'] = 60

student_info['zhangqiang']['分数']['python'] = 80

print ['liming']['分数']['python']

print ['zhangqiang']['分数']['python']

print "#3 把张强的数学成绩由82分改成89分\n"

student_info['zhangqiang']['分数']['数学'] = 89

print "#4 删除李明的年龄数据\n"

del student_info['liming']['年龄']

print "#5 对张强同学的课程分数按照从低到高排序输出。\n"

b = student_info["zhangqiang"]["分数"].values()

b.sort

print b

print "#6 外部删除学生所在的城市属性,不存在返回字符串 beijing\n"

print student_info.pop("城市","北京")

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/344080
推荐阅读
相关标签
  

闽ICP备14008679号