A set is an unordered collection of unique values. Two things make it different from a list: it never holds duplicates, and it doesn't keep any order. Write one with curly braces:
ports = {80, 443, 8080}
If you put the same value in twice, the set keeps just one copy. This is the defining behavior:
ports = {80, 443, 80, 8080, 443}
print(ports) # {80, 443, 8080}
A set doesn't track position, so there's no first or last item and you can't index it. This fails:
ports[0]
# TypeError: 'set' object is not subscriptable
When you print a set the items may come out in any order. If you need order or indexing, you want a list, not a set.
Passing a list to set() builds a set from its values, dropping any
duplicates along the way. This is the most common way you'll create a
set in practice:
hosts = ["web-01", "web-02", "web-01"]
unique = set(hosts)
print(unique) # {'web-01', 'web-02'}
The in operator and len work just like they do on a list, and in
on a set is very fast even when the set is large:
print("web-01" in unique) # True
print(len(unique)) # 2
Empty braces {} make an empty dictionary, not an empty set. For an
empty set you must call set():
empty = set()