2025年3月4日上課Python 陣列array串列的函數(方法)

利用w3schools練習的python指令

animal = ["豬", "牛", "羊", '貓']
#python語言的註解comments
x = animal.copy()
print(animal)
x.reverse() #將x反轉reverse
print(x)
animal.append('狗')
print(animal)
y = animal.copy()
y.reverse() #將x反轉reverse
print(y)
  
Python 說明或範例JavaScript
len() len(list) # 串列 list 長度list.length;
append()list.append(’ 加元素’)list.push(’ 加元素’)
clear() list.clear() # 移除所有元素
copy()a = list.copy() # 拷貝至新串列 a
count() list.clear() # 移除所有元素 list.count(’ 豬頭’) #list=[’ 豬頭’,...]?
extend() list.extend([’ 豬頭’,’ 狗屎’])# 合併 (延伸) 串列 a=list.concat([’ 豬頭’,’ 狗屎’])
index()
insert() list.index(’ 豬頭’) # 元素第一次出現的位置 0, 1, 2... list.indexOf(’ 豬頭’) list.insert(k,’ 豬頭’)# 位置 k 前插入元素’ 豬頭’
pop() list.pop(k) # 移除位置 k 元素
remove() list.remove(’ 豬頭’) # 移除第一次出現的’ 豬頭’ reverse() list.reverse()# 將元素次序反轉 list.slice(k,k + 1) list.reverse()
sort() list.sort()# 將元素排序list.sort()

GOOGLE COLAB執行程式

嵌入EMBED影片與網頁

留言

這個網誌中的熱門文章

楊侑宸金融常識測驗期中考

楊侑宸python運算CSV輸出TKINTER投資風險與報酬Markowaitz

楊侑宸HTML按鈕BUTTON事件ONCLICK 4月 21, 2025