繼續(xù)昨天沒有打完的代碼,雖然我學(xué)的慢震蒋,學(xué)的不多,但只要我每天在學(xué)躲庄,哪就要超過大多數(shù)人了查剖。某些人看到我學(xué)的內(nèi)容可能會感覺到我很低級噪窘,但是我不怕別人的嘲笑,我所學(xué)的是為了我自己效览。
>>> [1,2,3,]+['a','b','c']? #可以用+操作符連接兩個列表
[1, 2, 3, 'a', 'b', 'c']
>>> [1,'a',3.14]*3? #可以用*操作符復(fù)制列表
[1, 'a', 3.14, 1, 'a', 3.14, 1, 'a', 3.14]
>>> spam=[1,2,3,]
>>> spam=spam+['a','b','c']? #可以賦值給同一個變量
>>> spam
[1, 2, 3, 'a', 'b', 'c']
>>> spam=['cat', 'bat', 'rat', 'elephant']
>>> del spam[2]? #可以直接刪除下標(biāo)處的值
>>> spam
['cat', 'bat', 'elephant']
>>> del spam[2]
>>> spam
['cat', 'bat']
>>> del spam? #還可以刪出一整個變量
>>> spam? #刪出后這個變量就不存在了,會出現(xiàn)NameError錯誤
Traceback (most recent call last):
? File "<pyshell#11>", line 1, in <module>
? ? spam
NameError: name 'spam' is not defined
print('Enter the name of cat 1:')
catName1 = input()
print('Enter the name of cat 2:' )
catName2 = input()
print('Enter the name of cat 3:' )
catName3 = input()
print('Enter the name of cat 4:')
catName4 = input()
print('Enter the name of cat 5:')
catName5 = input()
print('Enter the name of cat 6:' )
catName6 = input()
print('The cat names are: ' )
print(catName1+' '+catName2+' '+catName3+' '+catName4+' '+catName5 + ' ' + catName6)
通過上面這些代碼丐枉,可以輸入6只貓的名字哆键。
catNames=[]? #定義一個空列表
while True:? #一直循環(huán)
? ? ? print('Enter the name of cat ' + str(len(catNames) + 1) +
? ? ? '(Or enter nothing to stop.):' )
? ? ? name=input()
? ? ? if name == '':? #判斷跳出循環(huán)
? ? ? ? ? break
? ? ? catNames=catNames+[name]? # list concatenation,連接列表
print('The cat names are:')
for name in catNames:? #用列表長度進(jìn)行循環(huán)籍嘹,把值賦值給變量name
? ? ? print(' '+name,end=' ')? #打印貓的名字,并以空格為結(jié)束辱士,如果不加end=' ',就是默認(rèn)換行結(jié)束听绳。
今天有點頭暈颂碘,所以學(xué)的不多,就到這里吧椅挣!