Python第二次作业

                                                                  Python第二次作业

                                                                                                 参考书目:《Python编程从入门到实践》

                                                                                                                2018/03/13

3-1姓名:将一些朋友的姓名存储在一个列表中,并将其命名为name。依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来

names = ['Mary','John','Bob']

print(names[0])

print(names[1])

print(names[2])

 

 

3-2问候语:继续使用练习3-1中的列表,但不打印每个朋友的姓名,而为每个人打印一条消息。每条消息都包含相同的问候语,但抬头为相应的朋友名字

names = ['Mary','John','Bob']

print("Hello ," + names[0])

print("Good morning ," + names[1])

print("Good afternoon ," + names[2])

 

 

3-3自己的列表:想想你喜欢的通勤方式,如骑摩托车或开汽车,并创建一个包含多种通勤方式的列表。根据该列表打印一些有关这些通勤方式的宣言

transportations =['bike','motorcycle','car']

print("I would like to ride a " +transportations[0])

print("I would like to own a Honda"+ transportations[1])

print("I would like to own a "+transportations[2])

 

 

3-4嘉宾名单:如果你可以邀请任何人一起共进晚餐,你会邀请哪些人?请创建一个列表,其中包含至少3个你想要邀请的人;然后,使用列表打印消息,邀请这些人来与你共进晚餐

customs = ['Mary','John','Bob']

for custom in customs:

  print("Wish " + custom + " would attend my party")

 

 

3-5修改嘉宾名单:你刚得知有为嘉宾无法赴约,因此需要另外邀请一位嘉宾

以完成练习3-4时编写的程序为基础,在程序末尾添加一条print语句,指出哪位嘉宾无法赴约

修改嘉宾的名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名

再次打印一系列消息,向名单中的美味嘉宾发出邀请

customs = ['Mary','John','Bob']

print(customs[2] + " can't attend theparty")

customs[2] = 'July'

for custom in customs:

  print("Wish " + custom + " would attend my party")

 

 

3-6添加嘉宾:你刚找到一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪几位嘉宾

在程序末尾添加一条print语句,指出你找到了一个更大的餐桌

使用insert()将一位新嘉宾添加到名单的开头

使用insert()将另一位新嘉宾添加到名单的中间

使用append()将最后一位新嘉宾添加到名单末尾

打印一系列消息向名单中的每位嘉宾发出邀请

customs = ['Mary','John','Bob']

customs[2] = 'July'

print("I found a bigger table")

customs.insert(0 ,'Andy')

customs.insert(int(len(customs) / 2) ,'Jane')

customs.append('David')

for custom in customs:

  print("Wish " + custom + " would attend my party")

 

 

3-7缩减名单:你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾

在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息

使用pop()不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾获悉你很抱歉,无法邀请他参加

对于余下的两位嘉宾中的每一位,都打印一条消息指出他依然在邀请之列

使用del将最后两位嘉宾从名单上删除,让名单变为空,打印该名单,合适程序结束时名单确实为空

customs = ['Mary','John','Bob']

customs[2] = 'July'

customs.insert(0 ,'Andy')

customs.insert(int(len(customs) / 2) ,'Jane')

customs.append('David')

print("I can only invite two customsto the party")

while len(customs) != 2 :

       custom= customs.pop()

       print(custom+ ". I'm sorry.You can't attend this party")

del customs[:]

print(customs)

 

 

3-8放眼世界:想出至少5个渴望去旅游的地方。

将这些地方存储在一个列表中,并确保其中的元素不是按字母顺序排列的

按原始排列打印列表。不要考虑输出是否整洁的问题,只管打印原始Python列表

使用sorted()按字母顺序打印这个列表,同时不要修改它

再次打印列表,核实排列顺序未变

使用sorted()按与字母顺序相反的顺序打印这个列表,同时不要修改它

再次打印该列表,核实排列顺序未变

使用reverse()修改列表元素的排列顺序,打印该列表,核实排列顺序确实变了

使用reverse()再次修改列表元素的排列顺序,打印该列表,核实已恢复到原来的排列顺序

使用sort()修改该列表,使其元素按字母顺序排列,打印该列表,核实排列顺序确实变了

使用sort()修改该列表,使其元素按与字母顺序相反的顺序排列,打印该列表,核实排列顺序确实变了

places =['Japan','America','Australia','England','Poland']

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)

 

 

晚餐嘉宾:使用len()打印一条消息,指出你邀请了多少为嘉宾来与你共进晚餐

customs = ['Mary','John','Bob']

customs[2] = 'July'

print("I found a bigger table")

customs.insert(0 ,'Andy')

customs.insert(int(len(customs) / 2) ,'Jane')

customs.append('David')

print(len(customs))

 

 

3-10对于本章的函数,至少使用一次

customs = ['Mary','John','Bob']

customs[2] = 'July'

customs.insert(0 ,'Andy')

customs.insert(int(len(customs) / 2) ,'Jane')

customs.append('David')

print(len(customs))

print(sorted(customs))

customs.sort()

print(customs)

customs.reverse()

print(customs)

print(customs.pop())

customs.remove('John')

print(customs)

del customs[:]

print(customs)

 

 

 

 

 

  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值