Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python-set/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Sets in Python

This folder provides the code examples for the Real Python tutorial [Sets in Python](https://realpython.com/python-set/).
6 changes: 6 additions & 0 deletions python-set/add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
employees = {"Alice", "Charlie"}
employees.add("John")
employees.add("Laura")
employees.add("John")
# employees.add("Jane", "Bob")
print(employees)
3 changes: 3 additions & 0 deletions python-set/clear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
employees = {"Alice", "Charlie", "John", "Laura"}
employees.clear()
print(employees)
2 changes: 2 additions & 0 deletions python-set/comprehensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
usernames = ["Alice", " bob", "ALICE ", "Bob", "charlie", "Charlie", "JOHN"]
print({name.lower().strip() for name in usernames})
10 changes: 10 additions & 0 deletions python-set/difference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
registered_users = {"Alice", "Bob", "Charlie", "Diana", "Linda"}
checked_in_users = {"Alice", "Charlie", "Linda"}
print(registered_users - checked_in_users)
print(registered_users.difference(checked_in_users))

a = {1, 2, 3, 30, 300}
b = {10, 20, 30, 40}
c = {100, 200, 300, 400}
print(a - b - c)
print(a.difference(b, c))
9 changes: 9 additions & 0 deletions python-set/difference_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
todo_list = {
"Implement user login",
"Fix bug #123",
"Improve performance",
"Write unit tests",
}
completed_tasks = {"Fix bug #123", "Improve performance"}
todo_list.difference_update(completed_tasks)
todo_list
4 changes: 4 additions & 0 deletions python-set/discard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
employees = {"Alice", "Charlie", "John", "Laura"}
employees.discard("Alice")
employees.discard("Linda")
print(employees)
22 changes: 22 additions & 0 deletions python-set/disjoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def verify_purchase(age, selection, restricted_products):
if age < 21 and not selection.isdisjoint(restricted_products):
print("Purchase denied: selection includes age-restricted products")
else:
print("Purchase approved")


verify_purchase(
age=18,
selection={"milk", "bread", "beer"},
restricted_products={"alcohol", "beer", "cigarettes"},
)
verify_purchase(
age=18,
selection={"milk", "bread"},
restricted_products={"alcohol", "beer", "cigarettes"},
)
verify_purchase(
age=35,
selection={"milk", "bread", "beer"},
restricted_products={"alcohol", "beer", "cigarettes"},
)
13 changes: 13 additions & 0 deletions python-set/intersection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
john_friends = {"Linda", "Mathew", "Carlos", "Laura"}
jane_friends = {"Alice", "Bob", "Laura", "Mathew"}

print(john_friends & jane_friends)
print(john_friends.intersection(jane_friends))

a = {1, 2, 3, 4}
b = {2, 3, 4, 5}
c = {3, 4, 5, 6}
d = {4, 5, 6, 7}

print(a & b & c & d)
print(a.intersection(b, c, d))
10 changes: 10 additions & 0 deletions python-set/intersection_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
accepted_emails = {"Bob", "Diana", "Charlie"}
target_customers = {"Alice", "Bob", "Charlie", "Diana", "Jane"}
target_customers &= accepted_emails
print(target_customers)

# Or

target_customers = {"Alice", "Bob", "Charlie", "Diana", "Jane"}
target_customers.intersection_update(accepted_emails)
print(target_customers)
38 changes: 38 additions & 0 deletions python-set/iteration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
employees = {"Alice", "Charlie", "John", "Laura"}
for employee in employees:
print(f"Hello, {employee}!")

employees = {"Alice", "Charlie", "John", "Laura"}
uppercase = set()
for employee in employees:
uppercase.add(employee.upper())

print(uppercase)

employees = {"Alice", "Charlie", "John", "Laura"}
employees = {employee.upper() for employee in employees}
print(employees)

available_seats = {"A1", "A2", "B1", "B2", "C1"}
while available_seats:
assigned_seat = available_seats.pop()
print(f"Seat {assigned_seat} was assigned")

print(available_seats)

cities = {"Vancouver", "Berlin", "London", "Warsaw", "Vienna"}
for city in sorted(cities):
print(city)

cities = {
("Vancouver", 675000),
("Berlin", 3800000),
("London", 8980000),
("Warsaw", 1790000),
("Vienna", 1900000),
}
for city in sorted(cities, key=lambda city: city[1]):
print(city)

for city in sorted(cities, key=lambda city: city[1], reverse=True):
print(city)
27 changes: 27 additions & 0 deletions python-set/literals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
numbers = [1, 2, 2, 2, 3, 4, 5, 5]
print(set(numbers))

# Single-element set
hex_colors = {"#33FF57"}
print(hex_colors)

# Multiple-element set
hex_colors = {
"#33FF57", # Green
"#3357FF", # Blue
"#F1C40F", # Yellow
"#E74C3C", # Red
}
print(hex_colors)

rgb_colors = {
(51, 255, 87), # Green
(51, 87, 255), # Blue
(241, 196, 15), # Yellow
(231, 76, 60), # Red
}
print(rgb_colors)

print({2, 4, 6, 8, 10, 8, 2})
print({"Smith", "McArthur", "Wilson", "Johansson", "Smith"})
print({42, "Hi!", 3.14159, None, "Python"})
18 changes: 18 additions & 0 deletions python-set/membership.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import random
import time

numbers_list = list(range(1_000_000))
numbers_set = set(range(1_000_000))
number_to_check = [random.randint(0, 999_999) for _ in range(1_000)]

start = time.perf_counter()
for number in number_to_check:
_ = number in numbers_list
end = time.perf_counter()
print(f"List membership check took {end - start:.4f} seconds")

start = time.perf_counter()
for number in number_to_check:
_ = number in numbers_set
end = time.perf_counter()
print(f"Set membership check took {end - start:.4f} seconds")
6 changes: 6 additions & 0 deletions python-set/pop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
employees = {"Alice", "Charlie", "John", "Laura"}
print(employees.pop())
print(employees.pop())
employees.pop()
employees.pop()
employees.pop()
6 changes: 6 additions & 0 deletions python-set/proper_subset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
regular_plan = {"Tutorials", "Quizzes"}
premium_plan = {"Tutorials", "Video Courses", "Quizzes", "Books"}
print(regular_plan < premium_plan)

a = {1, 2, 3, 4, 5}
print(a < a)
6 changes: 6 additions & 0 deletions python-set/proper_superset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
regular_plan = {"Tutorials", "Quizzes"}
premium_plan = {"Tutorials", "Video Courses", "Quizzes", "Books"}
print(premium_plan > regular_plan)

a = {1, 2, 3, 4, 5}
print(a > a)
4 changes: 4 additions & 0 deletions python-set/remove.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
employees = {"Alice", "Charlie", "John", "Laura"}
employees.remove("Charlie")
employees.remove("Linda")
print(employees)
9 changes: 9 additions & 0 deletions python-set/subset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
required_ingredients = {"cheese", "eggs", "milk"}
available_ingredients = {"cheese", "eggs", "milk", "sugar", "salt"}

print(required_ingredients <= available_ingredients)
print(required_ingredients.issubset(available_ingredients))

a = {1, 2, 3, 4, 5}
print(a <= a)
print(a.issubset(a))
9 changes: 9 additions & 0 deletions python-set/superset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
required_ingredients = {"cheese", "eggs", "milk"}
available_ingredients = {"cheese", "eggs", "milk", "sugar", "salt"}

print(available_ingredients >= required_ingredients)
print(available_ingredients.issubset(required_ingredients))

a = {1, 2, 3, 4, 5}
print(a.issuperset(a))
print(a >= a)
10 changes: 10 additions & 0 deletions python-set/symmetric_difference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
morning_attendees = {"Alice", "Charlie", "Linda", "John", "Jane"}
afternoon_attendees = {"Charlie", "Linda", "Bob", "Jane"}

print(morning_attendees ^ afternoon_attendees)
print(morning_attendees.symmetric_difference(afternoon_attendees))

a = {1, 2, 3, 4, 5}
b = {10, 2, 3, 4, 50}
c = {1, 50, 100}
print(a ^ b ^ c)
9 changes: 9 additions & 0 deletions python-set/symmetric_difference_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
morning_attendees = {"Alice", "Charlie", "Linda", "John", "Jane"}
afternoon_attendees = {"Charlie", "Linda", "Bob", "Jane"}
whole_day_attendees = set()

whole_day_attendees ^= morning_attendees
print(whole_day_attendees)

whole_day_attendees.symmetric_difference_update(afternoon_attendees)
print(whole_day_attendees)
12 changes: 12 additions & 0 deletions python-set/union.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pet_animals = {"dog", "cat", "hamster", "parrot"}
farm_animals = {"cow", "chicken", "goat", "dog", "cat"}

print(pet_animals | farm_animals)
print(pet_animals.union(farm_animals))

a = {1, 2, 3, 4}
b = {2, 3, 4, 5}
c = {3, 4, 5, 6}
d = {4, 5, 6, 7}
a.union(b, c, d)
print(a | b | c | d)
10 changes: 10 additions & 0 deletions python-set/update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
checked_in_attendees = set()
print(id(checked_in_attendees))

checked_in_attendees |= {"Alice", "Charlie"}
print(checked_in_attendees)
print(id(checked_in_attendees))

checked_in_attendees.update({"Linda", "Bob"})
print(checked_in_attendees)
print(id(checked_in_attendees))