活動地址:CSDN21天學習挑戰賽
Let's start:
Whether it is for future study needs or for work needs after three years,都需要用到python的工具,Also follow the opportunity of this event,把pythonLearn it systematically.跟著大佬,讓pythonKnowledge from entry to mastery.
列表——list–容器(書包) : 有序、可變、支持索引
列表:存儲數據、存儲字符串、數字、布爾型、列表、集合、元組、字典
- list[1:3]左閉右開
定義一個列表:
lst=["alex",123,True,"憨憨","alex"] #Separate elements with commas,The address is stored
# print(lst)
# print(id(lst[0]))#id獲取對象的內存地址
# #['alex', 123, True, '憨憨'],輸出有序
列表是可變的:
lst[0]="alexxx" #Strings are not mutable variables
print(lst)
If you continue to add list content:
lst.append("烤鴨") #Add at the end of the list
print(lst)
lst.insert(2,"黑哥") #Added after the second element.But try not to use it in the future,若數據量太大,影響效率
print(lst)
lst.extend("今天是個好日子") #迭代添加,Add today's good one by one,Can't put numbers,Because numbers cannot be looped
#['alex', 123, True, '憨憨', '今', '天', '是', '個', '好', '日', '子']
print(lst)
列表刪除元素:
lst=["alex",123,True,"憨憨","alex1"]
lst.pop() #彈,默認刪除最後一個
print(lst)
lst.pop(1)
print(lst) # Delete by the specified index
lst.remove("憨憨") #Delete by element name,If there are multiple elements with the same name,刪除第一個,It will not be deleted thereafter
print(lst)
del lst #Force destroy the list
del lst[0:3] #刪除0-3,切片刪除
print(lst)
del lst[0:4:3] #刪除0-4,0開始之後,Interrupt two numbers in the middle and delete them once,步長刪除
print(lst)
lst.clear() #清空
print(lst)
修改列表內容:
lst=["alex",123,True,"憨憨","alex1"]
lst[3]="Dahanhan"
print(lst)
lst[1:2]="12345" #切片修改
#['alex', '1', '2', '3', '4', '5', True, '憨憨', 'alex1']
print(lst)
lst[1:4]=1,"daaa",3
print(lst)
lst[1:4:2]="12" #步長不為1的,必須一一對應,多一個或者少一個都不行
print(lst)
列表的嵌套:
lst=[1,"春生","小東北","渣弟",["大黑哥",["孫悟空","弼馬溫","齊天大聖"],"馮強","海峰",["太白金星","女神","吳超",["蕭峰"]]]]
print(lst[-1])
# ['大黑哥', '馮強', '海峰', ['太白金星', '女神', '吳超', ['蕭峰']]]
a=lst[-1]
print(a[-1])
#輸出:['太白金星', '女神', '吳超', ['蕭峰']]
# No matter what type of slice,Both are source data types
#想要獲取'弼馬溫'
a=lst[-1][1][1]
print(a)
首先舉一個例子:
t=123,'abc',["come","here"]
print(t)
元組是用圓括號括起來的,元素之間用逗號隔開.如上輸出:
(123, 'abc', ['come', 'here'])
其中,A tuple is also a sequence,與字符串、列表類似.
Operations are similar to lists:
t=1,'23',[123,'abc'],["come","here"]
print(t[2])
print(t[1:])
print(t[2][0])
[123, 'abc']
('23', [123, 'abc'], ['come', 'here'])
123
需要注意的是,If a tuple has only one element,A comma should be added after the element.如下:
a=(3)
print(type(a))
b=(3,)
print(type(b))
<class 'int'>
<class 'tuple'>
Conversion between lists and tuples
t=1,'23',[123,'abc'],["come","here"]
print(t)
tls=list(t)
print(tls)
#輸出:
#(1, '23', [123, 'abc'], ['come', 'here'])
#[1, '23', [123, 'abc'], ['come', 'here']]
tu_tuple=tuple(tls)
print(tu_tuple)
#輸出:
#(1, '23', [123, 'abc'], ['come', 'here'])
python3 中的range是一個迭代對象,How to write and how to print
print(list(range(0,10)))
# 輸出:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #顧頭不顧尾(左閉右開)
python2 中的range返回的是一個列表
print(range(0,10))
# # 輸出:range(0, 10)
示例展示:
print(list(range(10,1,-1))) #[10:1:-1]
#輸出:[10, 9, 8, 7, 6, 5, 4, 3, 2]
#使用for循環和range打印100~1
for i in range(100,0,-1):
print(i,end=" ")
#輸出:100 99 98 97 96 95 94 93 92 91 ..... 1