w3schools陣列截圖
w3schools陣列程式碼
#劉任昌w3schools練習
#for x in 'Takming gold': #迴圈逐字字元
# print("字母: %s" % x)
fruits = ['台積電', '鴻海', '聯發科']
for x in fruits: # 迴圈印出元素
print ("當前公司: %s"% x)
print ("append") #append在末尾增加
fruits.append("中華電") #append接元素
print(fruits)
print("移除所有元素clear")
fruits.clear() #Removes all the elements from the list
print(fruits)
fruits = ['台積電', '鴻海', '聯發科',"中華電"]
print(fruits)
pig = fruits.copy()
print("輸出pig用copy方法" + str(pig))
print("使用copy和直接賦予值 pig = fruits有何不同? ")
dog = fruits
print("輸出dog直接=" + str(dog))
print("fruits.extend(pig)")
fruits.extend(pig) #extend接陣列或清單
print(fruits)
fruits.extend(['台塑化','台達電','富邦金'])
print(fruits)
#print("幾個台積電? " + str(fruits.count("台積電)))
print(fruits.index("富邦金"))
fruits.insert(10,"國泰金")
print(fruits)
print(fruits.index("富邦金"))
print("fruits元素個數" + str(len(fruits)))
fruits.pop(4)
print(fruits)
fruits.reverse() #反序排列
print(fruits)
fruits.sort() #排序按照字碼
print(fruits)
#體會到只要我有耐心與興趣,我也可以當一個專業的程式開發人員
w3schools陣列方法Array Methods
陣列方法Array Methods
Python語言有一組內建的方法(built-in methods)
方法Method | 說明Description |
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the first item with the specified value |
reverse() | Reverses the order of the list |
sort() | 排序Sorts the list |
w3schools轉寫模式拷貝方法表格
Array Methods
Python has a set of built-in methods that you can use on lists/arrays.
Method | Description |
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the first item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |
教學影片
230
234
235
410.拷貝應付!
回覆刪除