1
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)
print(bicycles[0]) ##第一个列表数据
print(bicycles[0].title())
print(bicycles[-1].title()) ##从最后开始数
messge = "my first bicycle was a "+bicycles[2].title()+"."
print (messge)
2
name = ['Laoli','zaoxu','Caocao','libai','dufu','dazhangwei']
print(name[0],name[1],name[2],name[3])
3
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'ducati' ##修改
print(motorcycles)
motorcycles.append('ducati') ##添加
print(motorcycles)
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)
motorcycles.insert(0, 'ducati') ##在0位置插入
print(motorcycles)
del motorcycles[0] ##删除
print(motorcycles)
popped_motorcycle = motorcycles.pop() ##弹出末尾元素到新变量列表
print(motorcycles)
print(popped_motorcycle)
motorcycles = ['honda', 'yamaha', 'suzuki']
last_owned = motorcycles.pop()
print("The last motorcycle I owned was a " + last_owned.title() + ".")
motorcycles = ['honda', 'yamaha', 'suzuki']
first_owned = motorcycles.pop(0) ##弹出指定元素
print('The first motorcycle I owned was a ' + first_owned.title() + '.')
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
motorcycles.remove('ducati') ##根据值删除
print(motorcycles)
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
too_expensive = 'ducati'
motorcycles.remove(too_expensive) ##将删除的值存储在too_expensive
print(motorcycles)
print("\nA " + too_expensive.title() + " is too expensive for me.")
4
1.嘉宾名单:如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请哪些人?请创建一个列表,其中包含至少 3个你想邀请的人;然后,使用这个列表打印消息,邀请这些人来与你共进晚餐。
2.修改嘉宾名单:你刚得知有位嘉宾无法赴约,因此需要另外邀请一位嘉宾。以完成练习 1时编写的程序为基础,在程序末尾添加一条 print 语句,指出哪位嘉宾无法赴约。修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。再次打印一系列消息,向名单中的每位嘉宾发出邀请。
3.添加嘉宾:你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。以完成练习 1或练习2时编写的程序为基础,在程序末尾添加一条 print 语句,指出你找到了一个更大的餐桌。使用 insert() 将一位新嘉宾添加到名单开头。使用 insert() 将另一位新嘉宾添加到名单中间。使用 append() 将最后一位新嘉宾添加到名单末尾。打印一系列消息,向名单中的每位嘉宾发出邀请。
4.缩减名单:你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。以完成练习3时编写的程序为基础,在程序末尾添加一行代码,打印一条你只
能邀请两位嘉宾共进晚餐的消息。使用 pop() 不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进晚餐。对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在受邀人之列。使用 del 将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的。
visitor=['mama','Baba','gege','jiejie','×××','didi']
visitor1=visitor[0]
visitor2=visitor[1]
visitor3=visitor[2]
visitor4=visitor[3]
visitor5=visitor[4]
visitor6=visitor[5]
print("我想邀请 "+visitor1+" "+visitor2+" "+visitor3+" "+visitor4+" "+visitor5+" "+visitor6+" "+"来参加我的party!")
print("我想邀请",visitor[0],visitor[1],visitor[2],visitor[3],visitor[4],visitor[5],"来参加我的party!")
print(visitor[0],"无法参加我的party")
visitor[0]='Libai'
print("我想邀请",visitor[0],visitor[1],visitor[2],visitor[3],visitor[4],visitor[5],"来参加我的party!")
visitor.insert(0,'Dufu')
print("我找了一个更大的餐桌,我还想邀请",visitor[0])
visitor.insert(3,'xijinping')
print("我找了一个更大的餐桌,我还想邀请",visitor[3])
visitor.append('haoren')
print("我找了一个更大的餐桌,我还想邀请",visitor[8])
print("我想邀请",visitor,"来参加我的party!")
print("餐桌没送来,只能邀请2位朋友了。。。")
while True:
if visitor.len()>2:
baoqian=visitor.pop()
print ("不好意思,"+baoqian+"不能邀请你来我家吃饭了!")
else:
print("你好,", visitor[0], "你依然可以参加我的party!")
print("你好,", visitor[1], "你依然可以参加我的party!")
break
del visitor[0]
del visitor[0]
print("还有",visitor.len(),"个人")
5
cars = ['bmw','audi','toyota','subaru']
cars.sort() ##按字母顺序排序
print(cars)
cars.sort(reverse=True) ##相反排序
print(cars)
cars = ['bmw','audi','toyota','subaru']
print(sorted(cars))
print(cars)
cars.reverse() ##倒着打印
print(cars)
print(len(cars)) ##确认列表长度
travel=['hainan','guangzhou','qingdao','xinjiang','dongbei']
print(travel)
print(sorted(travel))
print(travel)
print(sorted(travel,reverse=True)) ##travel按字母排序后,按反方向打印出来。
print(travel)
travel.reverse()
print(travel)
travel.reverse()
print(travel)
travel.sort()
print(travel)
travel.sort(reverse=True)
print(travel)