笨方法學python 習題32

the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']

# this first kind of for-loop goes through a list
for number in the_count:
    print ("This is count %d" % number)

# same as above
for fruit in fruits:
    print ("A fruit of type: %s" % fruit)

# also we can go through mixed lists too  我們也可以通過混合列表
# notice we have to use %r since we don't know what's in it for i in change:  注意我們必須使用% r,因為我們不知道的我在改變
for i in change:
    print ("I got %r" % i)

# we can also build lists, first start with an empty one  我們也可以構(gòu)建列表,先挖空
elements = []

# then use the range function to do 0 to 5 counts  然后使用范圍0到5項功能
for i in range(0, 6):
    print ("Adding %d to the list." % i)

# append is a function that lists understand elements.append(i)  附加是一個函數(shù)列表理解elements.append(i)
    elements.append(i)

# now we can print them out too
for i in elements:
    print ("Element was: %d" % i)
This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: apricots
I got 1
I got 'pennies'
I got 2
I got 'dimes'
I got 3
I got 'quarters'
Adding 0 to the list.
Adding 1 to the list.
Adding 2 to the list.
Adding 3 to the list.
Adding 4 to the list.
Adding 5 to the list.
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容