Skip to content

Commit 80e5421

Browse files
authored
Merge pull request #661 from realpython/python-set
Sample code for the article on sets
2 parents ae32268 + a79521b commit 80e5421

23 files changed

+246
-0
lines changed

python-set/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Sets in Python
2+
3+
This folder provides the code examples for the Real Python tutorial [Sets in Python](https://realpython.com/python-set/).

python-set/add.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
employees = {"Alice", "Charlie"}
2+
employees.add("John")
3+
employees.add("Laura")
4+
employees.add("John")
5+
# employees.add("Jane", "Bob")
6+
print(employees)

python-set/clear.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
employees = {"Alice", "Charlie", "John", "Laura"}
2+
employees.clear()
3+
print(employees)

python-set/comprehensions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
usernames = ["Alice", " bob", "ALICE ", "Bob", "charlie", "Charlie", "JOHN"]
2+
print({name.lower().strip() for name in usernames})

python-set/difference.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
registered_users = {"Alice", "Bob", "Charlie", "Diana", "Linda"}
2+
checked_in_users = {"Alice", "Charlie", "Linda"}
3+
print(registered_users - checked_in_users)
4+
print(registered_users.difference(checked_in_users))
5+
6+
a = {1, 2, 3, 30, 300}
7+
b = {10, 20, 30, 40}
8+
c = {100, 200, 300, 400}
9+
print(a - b - c)
10+
print(a.difference(b, c))

python-set/difference_update.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
todo_list = {
2+
"Implement user login",
3+
"Fix bug #123",
4+
"Improve performance",
5+
"Write unit tests",
6+
}
7+
completed_tasks = {"Fix bug #123", "Improve performance"}
8+
todo_list.difference_update(completed_tasks)
9+
todo_list

python-set/discard.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
employees = {"Alice", "Charlie", "John", "Laura"}
2+
employees.discard("Alice")
3+
employees.discard("Linda")
4+
print(employees)

python-set/disjoint.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def verify_purchase(age, selection, restricted_products):
2+
if age < 21 and not selection.isdisjoint(restricted_products):
3+
print("Purchase denied: selection includes age-restricted products")
4+
else:
5+
print("Purchase approved")
6+
7+
8+
verify_purchase(
9+
age=18,
10+
selection={"milk", "bread", "beer"},
11+
restricted_products={"alcohol", "beer", "cigarettes"},
12+
)
13+
verify_purchase(
14+
age=18,
15+
selection={"milk", "bread"},
16+
restricted_products={"alcohol", "beer", "cigarettes"},
17+
)
18+
verify_purchase(
19+
age=35,
20+
selection={"milk", "bread", "beer"},
21+
restricted_products={"alcohol", "beer", "cigarettes"},
22+
)

python-set/intersection.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
john_friends = {"Linda", "Mathew", "Carlos", "Laura"}
2+
jane_friends = {"Alice", "Bob", "Laura", "Mathew"}
3+
4+
print(john_friends & jane_friends)
5+
print(john_friends.intersection(jane_friends))
6+
7+
a = {1, 2, 3, 4}
8+
b = {2, 3, 4, 5}
9+
c = {3, 4, 5, 6}
10+
d = {4, 5, 6, 7}
11+
12+
print(a & b & c & d)
13+
print(a.intersection(b, c, d))

python-set/intersection_update.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
accepted_emails = {"Bob", "Diana", "Charlie"}
2+
target_customers = {"Alice", "Bob", "Charlie", "Diana", "Jane"}
3+
target_customers &= accepted_emails
4+
print(target_customers)
5+
6+
# Or
7+
8+
target_customers = {"Alice", "Bob", "Charlie", "Diana", "Jane"}
9+
target_customers.intersection_update(accepted_emails)
10+
print(target_customers)

0 commit comments

Comments
 (0)