数据结构
列表 (Lists)
Section titled “列表 (Lists)”列表是可变的、有序的元素集合。
fruits = ["apple", "banana", "cherry"]print(fruits[0]) # 访问第一个元素
# 修改元素fruits[1] = "blackcurrant"
# 添加元素fruits.append("orange")
# 删除元素fruits.remove("cherry")元组 (Tuples)
Section titled “元组 (Tuples)”元组是不可变的、有序的元素集合。
thistuple = ("apple", "banana", "cherry")print(thistuple[0])
# 元组一旦创建,就不能修改(添加、删除、更改元素)# thistuple[1] = "blackcurrant" # 这会报错字典 (Dictionaries)
Section titled “字典 (Dictionaries)”字典是用于存储键值对的数据集合。Python 3.7+ 中字典是有序的。
car = { "brand": "Ford", "model": "Mustang", "year": 1964}
print(car["brand"])
# 修改值car["year"] = 2020
# 添加键值对car["color"] = "red"集合 (Sets)
Section titled “集合 (Sets)”集合是无序的、不重复的元素集合。
thisset = {"apple", "banana", "cherry"}thisset.add("orange")