##习题1:
列表a = [11,22,24,29,30,32]
1 把28插入到列表的末端
a.append(28)
2 在元素29后面插入元素57
a.insert(4,57)
3 把元素11修改成6
a[0]=6
3 删除元素32
a.pop(6) # 或者 del a[6]
4 对列表从小到大排序
a.sort() # 倒排序是reverse
##习题2:
列表b = [1,2,3,4,5]
1 用2种方法输出下面的结果:
[1,2,3,4,5,6,7,8]
b = [1,2,3,4,5]
b.append(6)
b.append(7)
b.append(8)
print (b)
b = [1,2,3,4,5]
c=[6,7,8]
print (b+c)
b.extend(c)
print (b)
2 用列表的2种方法返回结果:[5,4]
b = [1,2,3,4,5]
c=[b[-1],b[-2]]
print (c)
b = [1,2,3,4,5]
del b[0:3]
b.reverse()
print (b)
b = [1,2,3,4,5]
print (b[-1:-3:-1])
3 判断2是否在列表里
b = [1,2,3,4,5]
print (2 in b)
##习题3:
b = [23,45,22,44,25,66,78]
用列表解析完成下面习题:
1 生成所有奇数组成的列表
b = [23,45,22,44,25,66,78]
c=list((x for x in b if x%2==1))
print (c)
2 输出结果: ['the content 23','the content 45']
b = [23,45,22,44,25,66,78]
c=list(('the content %s' % x for x in b[:2]))
print (c)
3 输出结果: [25, 47, 24, 46, 27, 68, 80]
b = [23,45,22,44,25,66,78]
c=list((x+2 for x in b))
print (c)
##习题4:
用range方法和列表推导的方法生成列表:
[11,22,33]
a=list(range(11,34,11))
print (a)
print ([m * 11 for m in range(1,4)])
##习题5:
已知元组:a = (1,4,5,6,7)
1 判断元素4是否在元组里
a = (1,4,5,6,7)
print (4 in a)
2 把元素5修改成8
a = (1,4,5,6,7)
b=list(a)#元组转换列表
b[2]=8
a=tuple(b)#列表转换元组
print (a)
##习题6:
已知集合:setinfo = set('acbdfem')和集合finfo = set('sabcdef')完成下面操作:
1 添加字符串对象'abc'到集合setinfo
setinfo = set('acbdfem')
setinfo.add('abc') # setinfo.update('abc') 会将字符串的字符添加进去
print (setinfo)
2 删除集合setinfo里面的成员m
setinfo = set('acbdfem')
setinfo.remove('m')
print (setinfo)
3 求2个集合的交集和并集
setinfo = set('acbdfem')
finfo = set('sabcdef')
print (setinfo&finfo)
print (setinfo|finfo)
print (setinfo.intersection(finfo))
print (setinfo.union(finfo))
##习题7:
用字典的方式完成下面一个小型的学生管理系统。
1 学生有下面几个属性:姓名,年龄,考试分数包括:语文,数学,英语得分。
比如定义2个同学:
姓名:李明,年龄25,考试分数:语文80,数学75,英语85
姓名:张强,年龄23,考试分数:语文75,数学82,英语78
lm={'name':'liming','age':25,'Grade':{'yuwen':80,'shuxue':75,'yingyu':85}}
zq={'name':'zhangqiang','age':23,'Grade':{'yuwen':75,'shuxue':82,'yingyu':78}}
print (lm)
print (zq)
2 给学生添加一门python课程成绩,李明60分,张强:80分
lm={'name':'liming','age':25,'Grade':{'yuwen':80,'shuxue':75,'yingyu':85}}
zq={'name':'zhangqiang','age':23,'Grade':{'yuwen':75,'shuxue':82,'yingyu':78}}
lm['Grade']['python']=60
zq['Grade']['python']=80
print (lm)
print (zq)
3 把张强的数学成绩由82分改成89分
zq={'name':'zhangqiang','age':23,'Grade':{'yuwen':75,'shuxue':82,'yingyu':78}}
zq['Grade']['shuxue']=89
print (zq)
4 删除李明的年龄数据
lm={'name':'liming','age':25,'Grade':{'yuwen':80,'shuxue':75,'yingyu':85}}
lm.pop('age','noage')
print (lm)
5 对张强同学的课程分数按照从低到高排序输出。
zq={'name':'zhangqiang','age':23,'Grade':{'yuwen':75,'shuxue':82,'yingyu':78}}
a=list(zq['Grade'].values())
a.sort()
print (a)
6 外部删除学生所在的城市属性,不存在返回字符串 beijing
lm={'name':'liming','age':25,'Grade':{'yuwen':80,'shuxue':75,'yingyu':85}}
zq={'name':'zhangqiang','age':23,'Grade':{'yuwen':75,'shuxue':82,'yingyu':78}}
print (lm.pop('city','beijing'))
print (zq.pop('city','beijing'))
习题八:
1 用while语句的2种方法输出数字:1到10
i=0
while i<10:
i+=1;
print (i);
i=0
while True:
i+=1;
print (i);
if i==10:
break;
2 用for语句和continue 输出结果:1 3 5 7 9
for i in range(10):
if i%2==0:
continue;
print (i);
习题九:假设有列表
a = [1,2,3,4,5,6]
1 用for if else 的方法查找数字8是否在列表a里,如果在的话,输出字符串'find',如果不存在的话,
输出字符串'not find'
a = [1,2,3,4,5,6]
for i in a:
if i==8:
print ("find");
else:
print ("not find");
2 用while语句操作上面的列表a,输出下面结果:
[2,3,4,5,6,7]
a = [1,2,3,4,5,6]
b=[]
x=1;
while x in a:
x+=1;
b.append(x);
else:
print (b);