【Week2.1】习题3-1-3-11

我的电脑还是容纳不了Python呀~但我还是要去排除万难,完成这一段学习历程!

【3-1】依次打印列表中的元素!

names = ['alice', 'jack', 'nancy']
for name in names:
	print(name)

【3-2】依次打印列表中的元素,并且在每个元素后面加上相同的一段话!

names = ['alice', 'jack', 'nancy']
for name in names:
	print(name.title() + ", are you free this saturday night?")

【3-3】依次打印列表中的元素,每个元素融入到一句不同的话里面!

names = ['alice', 'jack', 'nancy']
print(names[0].title() + ", are you free this saturday night?")
print(names[1].title() + ", you are so nice!")
print(names[2].title() + ", do you like this book?")

【3-4】邀请列表中的朋友共进晚餐!

names = ['alice', 'jack', 'nancy']
print(names[0].title() + ", " +names[1].title() + ", " + names[2].title() + ", welcome to my dinner party!")


【3-5】在上一题基础上,指出其中一位无法赴约,修改这个不能来的换成可以来的嘉宾,再一次重新发出邀请!

names = ['alice', 'jack', 'nancy']
print(names[0].title() + ", " +names[1].title() + ", " + names[2].title() + ", welcome to my dinner party!")
print("I am sorry, nancy is not free that night! So, i will invite Mike to come.")
names[2] = "mike"
print(names[0].title() + ", " +names[1].title() + ", " + names[2].title() + ", welcome to my dinner party!")


【3-6】先指出找到了更大的桌子;使用insert()将一个人添加到开头;使用insert将一个嘉宾添加到中间;使用append将一位添加到末尾;打印消息,发出邀请!

names = ['alice', 'jack', 'nancy']
print("Wow, i found a bigger table!")
names.insert(0, "jenny")
names.insert(2, "john")
names.append("tiffany")
print(names[0].title() + ", " +names[1].title() + ", " + names[2].title() + ", " + names[3].title() + ", " + names[4].title() + ", " 
+ names[5].title() + ", welcome to my dinner party!")

【3-7】缩减名单;

names = ['alice', 'jack', 'nancy']
print("Wow, i found a bigger table!")
names.insert(0, "jenny")
names.insert(2, "john")
names.append("tiffany")
print(names[0].title() + ", " +names[1].title() + ", " + names[2].title() + ", " + names[3].title() + ", " + names[4].title() + ", " 
+ names[5].title() + ", welcome to my dinner party!")
print('\n')
print("I am sorry, the table is not avaliable on time, i can only invite two friends.")
print(names[5].title() + ", i am sorry for not being able to have dinner with you.")
names.pop()                                                                                                                                                                                                                                                                                  
print(names[4].title() + ", i am sorry for not being able to have dinner with you.")
names.pop()
print(names[3].title() + ", i am sorry for not being able to have dinner with you.")
names.pop() 
print(names[2].title() + ", i am sorry for not being able to have dinner with you.")
names.pop()
print(names[0].title() + ", " +names[1].title() +", i am still waiting for your coming.")
del names[1]
del names[0]
print(names)

【3-8】想去旅游的地方;使用sort和reverse完成各种操作!

places = ["c", "b", "h", "e", "j", "a"]
print(places)
print(sorted(places))
print(places)
print(sorted(places, reverse = True))
print(places)
places.reverse()
print(places)
places.reverse()
print(places)
places.sort()
print(places)
places.sort(reverse = True)
print(places)


【3-9】在之前的练习中加入一句len指出邀请了多少嘉宾吃饭!

names = ['alice', 'jack', 'nancy']
print(len(names))

【3-10】创建一个列表,用本章中所有的函数,来处理一次这个表!

names = ['alice', 'jack', 'nancy']
print(len(names))
names.insert(0, "mike")
print(names)
names.append("tiffany")
print(names)
names.pop()
print(names)
print(sorted(names))
print(names)
names.sort()
print(names)
names.reverse()
print(names)

【3-11】创造索引错误,并消除索引错误

names = ['alice', 'jack', 'nancy']
print(names[3])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值