
Code
Unique Visitors
A set is a bag of unique values, duplicates simply vanish.
Sets have no order, so there's no [0] indexing.
seen = set(["ada", "bo", "ada"])
print(len(seen)) # 2
seen.add("cy")
print("bo" in seen) # True (and very fast!)
Sets can be combined like in maths:
| Code | Meaning |
|---|---|
a | b |
union: in a or b (or both) |
a & b |
intersection: in both |
a - b |
difference: in a but not b |
Because sets have no order, use sorted(s) when you want to print
them neatly. It returns a sorted list.
Pip tip:
{}makes an empty dict. For an empty set, writeset().
Assignment
The lighthouse logs who visited on Monday and Tuesday.
- Turn both lists into sets:
monandtue. everyone: people who visited on either day.both_days: people who came on both days.only_monday: people who came Monday but not Tuesday.
The lighthouse logs who visited on Monday and Tuesday.
- Turn both lists into sets:
monandtue. everyone: people who visited on either day.both_days: people who came on both days.only_monday: people who came Monday but not Tuesday.
main.py Python sleeping
Press Run to try your code, then Check to test it.