Python 添加集合项

添加集合项

要向集合中添加一个项目,请使用 add() 方法。

要向集合中添加多个项目,请使用 update() 方法。

例子 1

使用 add() 方法向集合中添加一个项目:

thisset = {"apple", "banana", "cherry"}

thisset.add("orange")

print(thisset)

亲自试一试

例子 2

使用 update() 方法向集合中添加多个项目:

thisset = {"apple", "banana", "cherry"}

thisset.update(["orange", "mango", "grapes"])

print(thisset)

亲自试一试