Lesson 0: 列表 List



LESSON 0.1: 宣告一組列表 declare A LIST
LESSON 0.2: 取出列表中的單一元素
LESSON 0.3: 取出列表中的數個元素
LESSON 0.4: LIST的各種常用函數


Lesson 0.1: 產生列表 generate a list

Example Code
# -*- coding: UTF-8 -*-
list1 = ['physics', 'chemistry', 1997, 2000];
print(list1)   
list2 = [1, 2, 3, 4, 5 ];
print("list2=", list2)
list3 = ["a", "b", "c", "d"];
print(list3)
	
Note and Comments
list1 = ['physics', 'chemistry', 1997, 2000]; 產生一個一維混和列表,列表內容爲 'physics', 'chemistry', 1997, 2000, 在python中List包含的元素形態可以不均一, 但array的各個元素則必須爲同形態
list2 = [1, 2, 3, 4, 5 ];  產生一個一維純數字列表,列表內容爲1-5
list3 = ["a", "b", "c", "d"]; 產生一個一維純字串列表,列表內容爲a-d
print( list )  將List中的內容顯示在螢幕上 

	
how to run the py code with the console 如何執行程式 result



也可安裝jupyter後以瀏覽器開啟編輯器並執行python程式 result



Lesson 0.2: 取出列表中的單一元素

Example Code
list1 = [1, 2, 3, 4, 5 ];
print(list1[0])
print(list1[1])
print(list1[2])
print(list1[3])
print(list1[4])
print(list1[-4])
Note and Comments
result


Lesson 0.3: 取出列表中的數個元素

Example Code
list1 = [1, 2, 3, 4, 5, 6, 7 ];
print( "list1[0:] = ", list1[ 0: ] )
print( "list1[1:] = ", list1[ 1: ] )
print( "list1[:2] = ", list1[ :2 ] )
print( "list1[:3] = ", list1[ :3 ] )
print( "list1[2:5] = ", list1[ 2:5 ] )
Note and Comments
result




Lesson 0.4: List的各種常用函數:

cmp(list1, list2) :比較兩個list
len(list) :回傳元素個數
max(list) :回傳list中元素的最大值
min(list) :回傳list中元素的最小值
list.append(obj)在列表末端添加新元素
list.count(obj) 統計obj元素在列表中出現的次數
list.extend(seq) 在列表末端添加令一個序列
list.index(obj) 找出obj在列表中第一次出現的位置
list.insert(n, obj) 將obj插入到列表的第n+1的位置
list.remove(obj) 找出obj在列表中第一次出現的位置, 並刪去該obj
list.reverse() 顛倒列表中個元素的位置順序
list.sort([option]) 將列表重新排序