您的位置:首页 > 其它

【第二周】第三章全部习题3-1~3-11

2018-03-12 22:42 316 查看
3-1姓名names = ['LucyZheng','JairZhu','HHL','WilliamZheng']
for name in names:
print (name)
3-2问候语
names = ['LucyZheng','JairZhu','HHL','WilliamZheng']
for name in names:
print (name + ",nice to meet you!")
3-3自己的列表trans = ['train','bike','car']
for tran in trans:
print("I would like to own " + tran)3-4嘉宾名单names = ['JairZhu','HHL','WilliamZheng']
for name in names:
print (name + " ,would you like to have a dinner with me?")3-5修改嘉宾名单names = ['JairZhu','HHL','WilliamZheng']
for name in names:
print (name + " ,would you like to have a dinner with me?")
print("Oops! HHL is busy debuging his program so he cannot attend.")
names[1]="Xuanxuan" #修改名单
for name in names:
print (name + " ,would you like to have a dinner with me?")
3-6添加嘉宾names = ['JairZhu','HHL','WilliamZheng']
for name in names:
print (name + " ,would you like to have a dinner with me?")
print("Oops! HHL is busy debuging his program so he cannot attend.")
names[1]="Xuanxuan" #修改名单
for name in names:
print (name + " ,would you like to have a dinner with me?")
print("Oh!I have found a bigger table!")
names.insert(0,'Manman') #插入元素
names.insert(2,'Miracle')
names.append('Wonder')
for name in names:
print (name + " ,would you like to have a dinner with me?")3-7缩减名单names = ['JairZhu','HHL','WilliamZheng']
for name in names:
print (name + " ,would you like to have a dinner with me?")
print("Oops! HHL is busy debuging his program so he cannot attend.")
names[1]="Xuanxuan" #修改名单
for name in names:
print (name + " ,would you like to have a dinner with me?")
print("Oh!I have found a bigger table!")
names.insert(0,'Manman') #插入元素
names.insert(2,'Miracle')
names.append('Wonder')
for name in names:
print (name + " ,would you like to have a dinner with me?")
print("Oops! I only have to invite 2 friends!")
length=len(names)
while length > 2: #判断是否删除到只剩下两位嘉宾
length-=1
print(names[length] + " ,I am so sorry!")
names.pop(-1)
for name in names: #输出两位嘉宾的信息
print(name + " ,I'm looking forward to your visit.")
del names[0]
del names[0] #删除最后两位嘉宾3-8放眼世界cities=['Paris','Chengdu','Mosco','London','Beijing']
print(cities)
print(sorted(cities)) #按字母顺序排序输出,不改变原值
print(cities)
print(sorted(cities,reverse = True)) #按字母反向排序,不改变原值
print(cities)
cities.reverse() #倒序输出
print(cities)
cities.reverse() #再倒序输出
print(cities)
sorted(cities) #按字母顺序排序修改
print(cities)
sorted(cities,reverse=True) #按字母顺序倒序排序修改
print(cities)3-9晚餐嘉宾names = ['JairZhu','HHL','WilliamZheng']
for name in names:
print (name + " ,would you like to have a dinner with me?")
print("Oops! HHL is busy debuging his program so he cannot attend.")
names[1]="Xuanxuan" #修改名单
for name in names:
print (name + " ,would you like to have a dinner with me?")
print(len(names))3-10尝试使用各个函数cities=['Paris','Chengdu','Mosco','London','Beijing']
cities.append('Shanghai') #尾部添加
print(cities)
cities.insert(0,'Guangzhou') #中间插入
print(cities)
del cities[3] #删除
print(cities)
cities.pop(-1) #特定位置删除
print(cities)
cities.remove('Beijing') #根据值删除
print(cities)
cities.sort() #排序
print(cities)
cities.reverse() #倒序排列
print(cities)
print(len(cities)) #输出列表长度
3-11有意引发错误,使操作一个越界的值即可。
有意触发:names=[]
print(names[-1])

修改:names=['Haha']
print(names[-1])

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  课程作业