找回密碼
 立即註冊
查看: 2893|回復: 11

Lists

[複製鏈接]

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

發表於 2016-10-12 15:38:56 | 顯示全部樓層 |閱讀模式
  1. zoo_animals = ["pangolin", "cassowary", "sloth", "pig"];

  2. if len(zoo_animals) > 3:
  3.     print "The first animal at the zoo is the " + zoo_animals[0]
  4.     print "The second animal at the zoo is the " + zoo_animals[1]
  5.     print "The third animal at the zoo is the " + zoo_animals[2]
  6.     print "The fourth animal at the zoo is the " + zoo_animals[3]
複製代碼


938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2016-10-12 15:42:57 | 顯示全部樓層
  1. numbers = [5, 6, 7, 8]

  2. print "Adding the numbers at indices 0 and 2..."
  3. print numbers[0] + numbers[2]
  4. print "Adding the numbers at indices 1 and 3..."
  5. print numbers[1] + numbers[3]
複製代碼

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2016-10-12 15:47:01 | 顯示全部樓層
  1. zoo_animals = ["pangolin", "cassowary", "sloth", "tiger"]
  2. # Last night our zoo's sloth brutally attacked
  3. #the poor tiger and ate it whole.

  4. # The ferocious sloth has been replaced by a friendly hyena.
  5. zoo_animals[2] = "hyena"

  6. # What shall fill the void left by our dear departed tiger?
  7. # Your code here!
  8. zoo_animals[3] = "pig"
複製代碼

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2016-10-12 15:53:48 | 顯示全部樓層
  1. suitcase = []
  2. suitcase.append("sunglasses")
  3. suitcase.append("hat")
  4. suitcase.append("shoes")
  5. suitcase.append("underwear")

  6. list_length = len(suitcase) # Set this to the length of suitcase

  7. print "There are %d items in the suitcase." % (list_length)
  8. print suitcase
複製代碼

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2016-10-12 15:58:44 | 顯示全部樓層
  1. suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]

  2. first  = suitcase[0:2]  # The first and second items (index zero and one)
  3. middle = suitcase[2:4]  # Third and fourth items (index two and three)
  4. last   = suitcase[4:]  # The last two items (index four and five)
複製代碼

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2016-10-12 16:01:51 | 顯示全部樓層
  1. animals = "catdogfrog"
  2. cat  = animals[:3]   # The first three characters of animals
  3. dog  = animals[3:6]   # The fourth through sixth characters
  4. frog = animals[6:]   # From the seventh character to the end
複製代碼

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2016-10-12 16:06:37 | 顯示全部樓層
  1. animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
  2. duck_index = animals.index("duck")   # Use index() to find "duck"

  3. animals.insert(duck_index, "cobra")

  4. print animals # Observe what prints after the insert operation
複製代碼

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2016-10-12 16:09:51 | 顯示全部樓層
  1. my_list = [1,9,3,8,5,7]

  2. for number in my_list:
  3.     print 2 * number
複製代碼

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2016-10-12 16:38:52 | 顯示全部樓層
  1. start_list = [5, 3, 1, 2, 4]
  2. square_list = []

  3. for x in start_list:
  4.     square_list.append(x ** 2)
  5. square_list.sort()
  6. print square_list
複製代碼

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2016-10-13 01:16:42 | 顯示全部樓層
  1. backpack = ['xylophone', 'dagger', 'tent', 'bread loaf']
  2. backpack.remove("dagger")
複製代碼

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2016-10-18 12:16:57 | 顯示全部樓層
  1. a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
  2. for x in a:
  3.     if x % 2 == 0:
  4.         print x
複製代碼

print 偶數

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2017-3-6 14:10:12 | 顯示全部樓層
  1. for letter in "Redd":
  2.     print letter
  3.    
  4. # Empty lines to make the output pretty
  5. print
  6. print

  7. word = "Programming is fun!"

  8. for letter in word:
  9.     # Only print out the letter i
  10.     if letter == "i":
  11.         print letter
複製代碼
您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

Archiver|手機版|小黑屋|DD論壇 維護: Redd Design

GMT+8, 2024-9-20 00:48 , Processed in 0.045811 second(s), 29 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回復 返回頂部 返回列表