活動地址:CSDN21天學習挑戰賽https://marketing.csdn.net/p/bdabfb52c5d56532133df2adc1a728fd
目錄
集合
Create a collection there are two ways to
元組
創建方式
列表
創建方式:
操作函數
操作方法
實例
列表的引用
字典
查找
修改和添加
修改
添加
字典的操作函數
字典的操作方法
第一種:
T = {11,111,"11"}
print(T)
# {'11', 111, 11}
第二種:
T = set("Hello Would")
print(T)
{'H', 'e', 'o', ' ', 'l', 'd', 'u', 'W'}
注意:1.If you create an empty collection must use the second method.
2.集合中元素不可重復.
3.Collection of elements can only be immutable data types.(整數、浮點數、字符串、元組等)
A collection of common operating functions and methods
t = (123,456,789)
type(t)
# <class 'tuple'># 類型
print(t)
# (123, 456, 789)# 索引
print(t[2])
# 789
print(t[0:2])# 切片
# (123, 456)
Lists and tuples are sequence type,Tuples can realize the operation of the,List all can realize,And a list of tuples flexible compared,Tuple type definition cannot change,列表是可以修改的.
定義:是包含0個或多個元素的有序序列.
T = [2,"python",[2,4,6]]
print(T)
# [2, 'python', [2, 4, 6]]
list(T[1])# 列表的索引
# ['p', 'y', 't', 'h', 'o', 'n']
print(T[0:2])# 列表的切片
# [2, 'python']
T = []
T
# []
list()
# []
List types available in parentheses to say,也可以通過list()Function converts collection or string types such as sequence list.
也可以使用 list()或者[]Production of an empty list.
max和min函數使用時,The elements in the list to make sure that you can compare,否則就會報錯.For example, list the Numbers,At the same time also have a string.
list函數中的x可以是字符串、元組,But can't be a numeric type.
T = [1, 2, 3, 4, 5, 6]
T.append(7)
print(T)
# [1, 2, 3, 4, 5, 6, 7]
G = [1, 2, 3, 1, 5, 7]
G.insert(3, 8)
print(G)
# [1, 2, 3, 8, 1, 5, 7]
N = [2, 3, 1, 6, 4, 8]
N.pop(3)
print(N)
# [2, 3, 1, 4, 8]
F = [1, 1, 2, 3, 2, 3]
F.remove(2)
print(F)
# [1, 1, 3, 2, 3]
M = [1, 2, 3, 4, 5, 6]
M.reverse()
print(M)
# [6, 5, 4, 3, 2, 1]
J = [1, 2, 3, 4, 5, 6]
J.clear()
print(J)
# []
對於列表來說,Use quotation marks can't achieve real assignment,If the listT = F,並不是拷貝F中的元素給T,But the new link a reference,意思是T和F指向的是同一個內容.如果把FThe contents of the clean up after,TThe content will disappear.可以通過copy一個新列表,Avoid to produce reference problem.
字典是通過“鍵值對”Store the data you need,Will need the data on the“值”,Colleagues associated a“鍵”.
通過“鍵”查到對應的值,這個過程叫映射.、
字典中的鍵Is the only and do not become,Each element is no order and unrepeatable.
T = {"name": "Liu Jiahao", "age": "18", "gender": "男"}
print(T["age"])
# 18
Modify and add can use“=”賦值.
T = {"name": "Liu Jiahao", "age": "18", "gender": "男"}
T['name'] = '彭於晏'
print(T)
# {'name': '彭於晏', 'age': '18', 'gender': '男'}
T = {"name": "Liu Jiahao", "age": "18", "gender": "男"}
T['book'] = 'math'
print(T)
# {'name': 'Liu Jiahao', 'age': '18', 'gender': '男', 'book': 'math'}
都看到這了,I have a look at the little cute thumb up to me .