曾銘祥HTML_CSS_Javascript_Java_Python
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執行程式https://www.youtube.com/watch?v=2kI1F_OQMxA&embeds_referring_euri=https%3A%2F%2Fyangyoyo0125.blogspot.com%2Fhttps://zh.wikipedia.org/wiki/File:IPython-notebook.png
留言
張貼留言