python编程实战2

3-3

trip_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-7

guests = ['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-9

places = ['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']

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值