Board logo

標題: 串列 (一) [打印本頁]

作者: 鄭繼威    時間: 2022-8-18 22:21     標題: 串列 (一)

本帖最後由 鄭繼威 於 2022-8-18 23:01 編輯

• 串列,又稱為「清單」或「列表」,與其他語言的
「陣列(Array)」相同,其功能與變數相類似。
  索引值是從0 開始計數,索引值不可超出串列的範圍
• 宣告
串列名稱(變數名稱)= [元素1, 元素2, ......]
• 範例
list1 = [1, 2, 3, 4, 5]
#元素皆為整數
list2 = ["香蕉", "蘋果", "橘子"]
#元素皆為字串
list3 = [1, "香蕉", True]
#包含不同資料型態元素

• 取得串列元素
變數名稱= 串列名稱[索引值]


假設有一串列其內容如下:
list=["忠","孝","仁","愛","信","義","和","平"]

試取出串列中的第1到第5個元素

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. print(list[0])
  3. print(list[1:6]) #左邊是開始(有包含),右邊是結束(沒有包含)。
  4. print(list1[-1]) #取出最後一個元素值
複製代碼

作者: 鄭繼威    時間: 2022-8-19 11:33

  1. a=["忠","孝","仁","愛","信","義","和","平"]
  2. print(a[-3:-1])
複製代碼

作者: 陳牧謙    時間: 2022-8-19 11:38

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. print(list[0])
  3. print(list[1:6])
  4. print(list1[-1]
複製代碼

作者: 彭郁程    時間: 2022-8-19 11:38

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. print(list[4])
  3. print(list[1:5])
  4. print(list[-1])
複製代碼

作者: 吳俊頡    時間: 2022-8-19 11:38

本帖最後由 吳俊頡 於 2022-8-19 11:40 編輯
  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. print(list[1])
  3. print(list[1:5])
  4. print(list[-2])
複製代碼

作者: 姜宏叡    時間: 2022-8-19 11:39

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. print(list[4])
  3. print(list[5:7])
  4. print(list[-8])
複製代碼

作者: 簡宗漢    時間: 2022-8-19 11:40

  1. j=["忠","孝","仁","愛","信","義","和","平"]
  2. a=[1,2,3,4,5,6,7,8,9,"284","大",1.11]
  3. print(a[0:20])
  4. print(a[1]+a[4]-a[8]+a[1])
複製代碼

作者: 胡凰月    時間: 2022-8-19 11:41

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. print(list[0])
  3. print(list[1:6])
  4. print(list[-1])
複製代碼

作者: 吳鎧茵    時間: 2022-8-19 11:42

  1. a=["忠","孝","仁","愛","信","義","和","平"]
  2. print(a[1:3])
  3. print(a[2:4])
  4. print(a[5:7])
  5. print(a[6:8])
  6. print(a[5:6])
  7. print(a[4:7])
  8. print(a[2:5])
  9. print(a[3:6])
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2