用來描述多個裝備、多個愛好、多種狀態等
python
在[]裏能夠存放多個任意類型的值,逗號分隔元素
app
hobby_list = ['run','play','read'] hobby_list = list ['run','play','read'] print(f'my hobby_list:{hobby_list}') # my hobby_list:['run','play','read']
**優先掌握**
code
1.按索引取值
排序
2.切片(split)
繼承
3.長度(len)
索引
4.成員運算(is/is not)
ci
5.for循環
rem
6.刪除(del)
it
7.追加(append)
io
# list之索引取值 hobby_list = ['play', 'swimming', 'dancing', '666'] print(1, hobby_list[-1]) hobby_list[-1] = '233' # 索引修改 print(2, hobby_list[-1])
# list之切片 print(hobby_list[:]) # ['play', 'swimming', 'dancing', '666'] print(hobby_list[::-1]) # 從右到左 # ['666', 'dancing', 'swimming', 'play'] # ['play', 'swimming', 'dancing', '233'] # ['233', 'dancing', 'swimming', 'play']
# list之長度 name_list = ['nick', 'jason', 'tank', 'sean'] print(f"len(name_list): {len(name_list)}") # len(name_list): 4
# list之成員運算in和not in name_list = ['nick', 'jason', 'tank', 'sean'] print(f"'tank sb' in name_list: {'tank sb' in name_list}") print(f"'nick handsome' not in name_list: {'nick handsome' not in name_list}") # 'tank sb' in name_list: False # 'nick handsome' not in name_list: True
# list之循環 name_list = ['nick', 'jason', 'tank', 'sean'] for name in name_list: print(name) ##結果 nick jason tank sean
# list之刪除 name_list = ['nick', 'jason', 'tank', 'sean'] del name_list[2] print(f"name_list: {name_list}") # name_list: ['nick', 'jason', 'sean']
# list之追加值 name_list = ['nick', 'jason', 'tank', 'sean'] name_list.append('tank sb') print(f"name_list: {name_list}") # name_list: ['nick', 'jason', 'tank', 'sean', 'tank sb']
**須要掌握**
1.insert(插入,在列表的某一個位置插入一個元素)
2.pop(刪除元素.默認刪除列表最後一個元素)
3.remove(移除元素)
4.count(計數.u哦你過來計數列表裏面某一元素出現的次數)
5.index(索引.返回列表裏某一元素的位置)
6.claar(清空列表)
7.copy(複製列表)
8.extend(繼承,也就是兩個列表何在一塊兒)
9.reverse(反轉列表的元素,從右到左進行排序)
10.sort(排序.默認升序排)
hobby_list = ['run','play','read'] hobby_list.insert(1,'sleep') print(hobby_list) # ['run', 'sleep', 'play', 'read']
# pop默認刪除的是最後一個元素,可是也能夠指定索引的位置 hobby_list = ['run','play','read'] hobby_list.pop() print(hobby_list) # ['run', 'play'] hobby_list = ['run','play','read'] hoppy_list.pop(0) print(hobby_list) # ['play','read']
hobby_list = ['run','play','read'] hobby_list.remove('run') print(hobby_list) # ['play','read']
hobby_list = ['run','play','read'] print(hobby_list.count('run')) # 1
hobby_list = ['run','play','read'] print(hobby_list(play)) # 1
hobby_list = ['run','play','read'] hobby_list.clear() print(hobby_list) # []
hobby_list = ['run','play','read'] hobby_list2 = hobby_list.copy print(hobbby_list2) # ['run','play','read']
hobby_list = ['run','play','read'] hobby_list2 = ['sing'] hobby_list.extend(hobby_list2) print(hobby_list) # ['run', 'play', 'read', 'sing']
hobby_list = ['run','play','read'] hobby_list.reverse() print(hobby_list) # ['read', 'play', 'run']
hobby_list = ['run','play','read'] hobby_list.sort() print(hobby_list) ['play', 'read', 'run']
hobby_list = ['run','play','read'] print(id(hobby_list)) hobby_list[2] = 'sing' print(id(hobby_list)) #結果 2855383215368 2855383215368