3.4. イテラブルの詳細
3.4.4 集合
同じ要素は複数入れられない
集合 変数 = {要素1, 要素2, 要素3, 要素4, 要素5}
w_set = {"dog", "cat", "mouse"}
for s in w_set:
print(s)
->
dog
cat
mouse
addメソッド 集合名.add(要素)
removeメソッド 集合名.remove(要素)
discardメソッド 集合名.discard(要素)
w_set = {"dog", "cat", "mouse"}
print(w_set)
->{"dog", "cat", "mouse"}
w_set.add("fox")
print(w_set)
->{"cat", "fox", "dog", "mouse"}
w_set.add("dog")
print(w_set)
->{"cat", "fox", "dog", "mouse"}
w_set.remove("fox")
print(w_set)
->{"cat", "dog", "mouse"}
w_set.discard("mouse")
print(w_set)
->{"cat", "dog"}