您的位置:首页 > 其它

第二周作业

2018-03-18 10:54 246 查看
3-3trip_modes = ['bus','car','bike','subway','plane']
for trip_mode in trip_modes:
print('I would like to own a '+ trip_mode)打印所有的出行工具,如下:
I would like to own a bus
I would like to own a car
I would like to own a bike
I would like to own a subway

I would like to own a plane

3-4 到 3-7guests = ['Alice','Julie','Ben','Jim','Nicole']
print(guests)
print(guests[0]+ ' will not attend the meet !')
guests[0] = 'Maidanna'
print(guests)
print("There is a big table !")
guests.insert(0,'Davie')
guests.insert(3,'queen')
guests.append('Amy')
print(guests)
while True:
name = guests.pop()
print('sorry '+name + ' , you can not attend the meet.')
if len(guests)<=2:
break
for guest in guests:
print(guest+' are still be invited!')
while guests:
del guests[0]
print(guests)测试代码如下:
['Alice', 'Julie', 'Ben', 'Jim', 'Nicole']
Alice will not attend the meet !
['Maidanna', 'Julie', 'Ben', 'Jim', 'Nicole']
There is a big table !
['Davie', 'Maidanna', 'Julie', 'queen', 'Ben', 'Jim', 'Nicole', 'Amy']
sorry Amy , you can not attend the meet.
sorry Nicole , you can not attend the meet.
sorry Jim , you can not attend the meet.
sorry Ben , you can not attend the meet.
sorry queen , you can not attend the meet.
sorry Julie , you can not attend the meet.
Davie are still be invited!
Maidanna are still be invited!

[]

3-8到3-9places = ['Paris','Tokyo','Beijing','London','Chicago']
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)
print(len(places))测试代码如下:
['Paris', 'Tokyo', 'Beijing', 'London', 'Chicago']
['Beijing', 'Chicago', 'London', 'Paris', 'Tokyo']
['Paris', 'Tokyo', 'Beijing', 'London', 'Chicago']
['Tokyo', 'Paris', 'London', 'Chicago', 'Beijing']
['Paris', 'Tokyo', 'Beijing', 'London', 'Chicago']
['Chicago', 'London', 'Beijing', 'Tokyo', 'Paris']
['Paris', 'Tokyo', 'Beijing', 'London', 'Chicago']
['Beijing', 'Chicago', 'London', 'Paris', 'Tokyo']
['Tokyo', 'Paris', 'London', 'Chicago', 'Beijing']

5

4-7: 3的倍数multiple_3 = list(range(3,31,3))
print(multiple_3)
for value in multiple_3:
print(value)测试代码:
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
3
6
9
12
15
18
21
24
27
30

4-9:立方解析Cube = [value**3 for value in range(1,11)]
print(Cube)测试代码:
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

4-10 切片places = ['Paris','Tokyo','Beijing','London','Chicago']
print(places[1:3])
print(places[:3])
print(places[1:])
print(places[-1:])
print(places[:-1])测试代码如下:
['Tokyo', 'Beijing']
['Paris', 'Tokyo', 'Beijing']
['Tokyo', 'Beijing', 'London', 'Chicago']
['Chicago']

['Paris', 'Tokyo', 'Beijing', 'London']
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Homework