#定義一個空串列 list_demo=[] #1,向串列中插入元素 def append_demo(): #第一種使用append,可以在串列末尾添加一個函式 for i in range(2): list_demo.append(input()) #輸入one,two print(list_demo) #["one","two"] #append_demo() def extend_demo(): #第二種使用extend,可以在串列末尾添加多個元素 list_one=[input() for i in range(2)] #輸入測驗串列["one","two","three"] list_demo.extend(list_one) print(list_demo) #["one","two","three"] # extend_demo() def insert_demo(): #第三種使用insert來完成,可以插入到串列中任一位置 list_demo=["zero","one","two"] list_demo.insert(1,"text_demo") print(list_demo) #['zero', 'text_demo', 'one', 'two'] # insert_demo() #2,洗掉串列元素 def remove_demo(): #remove洗掉函式可以洗掉任一指定的元素 list_demo=["one","two","three"] list_demo.remove("one") print(list_demo) #['two', 'three'] #remove_demo() def pop_demo(): #pop洗掉串列指定位的元素 list_demo = ["one", "two", "three"] ele_one=list_demo.pop(1) #串列洗掉并回傳該元素 print(ele_one) #two print(list_demo) #['one', 'three'] # pop_demo() def del_dmeo(): #del可以直接洗掉串列中指定位置的元素 list_demo=["zero","one", "two", "three"] del list_demo[1:3] print(list_demo) #['zero', 'three'] # del_dmeo() #3,查找串列元素 def in_demo(): #通過in和not in 來判斷一個元素是否在串列中 list_demo=["zero","one", "two", "three"] if "one" in list_demo: print("yes") #yes if "good" not in list_demo: print("good") #good # in_demo() def count_demo(): #count可以回傳元素在串列中出現的次數 list_demo = ["zero", "one", "two", "three"] num=list_demo.count("three") print(num) #1 # count_demo() def index_demo(): #index可以回傳元素出現在串列中的位數 list_demo = ["zero", "one", "two", "three"] num=list_demo.index("two") print(num) #2 # index_demo() #4,對串列內的函式的修改 def change_demo(): #修改串列函式直接重新賦值即可 list_demo = ["zero", "one", "two", "three"] list_demo[0]="good" print(list_demo) #['good', 'one', 'two', 'three'] change_demo()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/13989.html
標籤:Python
上一篇:python基礎2
