EX38

 1 ten_things = "Apples Oranges Crows Telephone Light Sugar"
 2 
 3 print "Wait there's not 10 things in that list, let's fix that."
 4 
 5 stuff = ten_things.split(' ')
 6 more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]
 7 
 8 while len(stuff) != 10:
 9     next_one = more_stuff.pop()
10     print "Adding: ", next_one
11     stuff.append(next_one)
12     print "There's %d items now." % len(stuff)
13     
14 print "There we go: ", stuff
15 
16 print "Let's do some things with stuff."
17 
18 print stuff[1]
19 print stuff[-1] # whoa! fancy
20 print stuff.pop()
21 print ' '.join(stuff) # what? cool!
22 print '#'.join(stuff[3:5]) # super stellar!

行5:用split对ten_things进行(' ')切片

str.split(str="", num=string.count(str)).

行8:利用while一直循环,直到出现flase原理,stuff字串数量不等于10

行9:pop()对more_stuff移除列表中一个元素(默认最后一个元素),并打印出来

list.pop(obj=list[-1])

行11:在列表末尾添加新的对象

list.append(obj)

行21:join():将序列中的元素以指定的字符连接生成一个新的字符串

str.join(sequence)

sequence -- 要连接的元素序列

TIPE:了解行5,8-12的运行逻辑

 # 去阅读“面向对象编程”(Object Oriented Programming, OOP)的资料

 

 

转载于:https://www.cnblogs.com/LevenLau/p/6390743.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值