Concept
Sets: Bags of Unique Elements
Sets are unordered collections with zero duplicate values, written with curly braces `{1, 2}`. Any duplicates are silently deleted.
python
tags = {"python", "ai", "python", "code"}
print(tags) # {'python', 'ai', 'code'}
print("ai" in tags) # True (instant O(1) hash check)
# 85. Sets: Bags of Unique Elements Sets are unordered collections with zero duplicate values, written with curly braces `{1, 2}`. Any duplicates are silently deleted: ```python tags = {"python", "ai", "python", "code"} print(tags) # {'python', 'ai', 'code'} print("ai" in tags) # True (instant O(1) hash check) ``` Notice: To create an empty set, you MUST type `empty = set()`. If you write `empty = {}`, Python assumes you meant an empty dictionary.
Checkpoint
What happens if you put duplicate items into a set like `{1, 1, 2}`?
Practice
Create a set named `ids` with values `101, 102, 101, 103`. Print `len(ids)` to prove duplicates were removed.
01
Tap code blocks below to assemble
Available Blocks