You cannot access items in a set by referring to an index or a key.
But you can loop through the set items using a for
loop, or ask if a specified value is present in a set, by using the in
keyword.
Loop through the set, and print the values:
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
Try it Yourself »
Check if "banana" is present in the set:
thisset = {"apple", "banana", "cherry"}
print("banana" in thisset)
Try it Yourself »
Once a set is created, you cannot change its items, but you can add new items.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!