|
| 1 | +""" |
| 2 | +Python set operations and usage examples. |
| 3 | +
|
| 4 | +This module demonstrates the basic operations and common use cases for Python sets. |
| 5 | +Sets are unordered collections of unique elements, useful for membership testing, |
| 6 | +removing duplicates, and mathematical set operations. |
| 7 | +""" |
| 8 | + |
| 9 | + |
| 10 | +def set_basics(): |
| 11 | + """Demonstrate basic set creation and properties.""" |
| 12 | + # Creating sets |
| 13 | + empty_set = set() # Empty set |
| 14 | + numbers = {1, 2, 3, 4, 5} # Set of numbers |
| 15 | + fruits = {"apple", "banana", "orange"} # Set of strings |
| 16 | + mixed = {1, "hello", (1, 2)} # Set with different types |
| 17 | + |
| 18 | + # Sets cannot contain mutable objects like lists or dictionaries |
| 19 | + # This would raise TypeError: unhashable type: 'list' |
| 20 | + # invalid_set = {[1, 2], 3} |
| 21 | + |
| 22 | + # Sets automatically remove duplicates |
| 23 | + with_duplicates = {1, 2, 2, 3, 3, 3} |
| 24 | + print(f"Set with duplicates: {with_duplicates}") # {1, 2, 3} |
| 25 | + |
| 26 | + # Check length |
| 27 | + print(f"Number of elements: {len(fruits)}") |
| 28 | + |
| 29 | + return empty_set, numbers, fruits, mixed |
| 30 | + |
| 31 | + |
| 32 | +def set_operations(): |
| 33 | + """Demonstrate common set operations.""" |
| 34 | + a = {1, 2, 3, 4, 5} |
| 35 | + b = {4, 5, 6, 7, 8} |
| 36 | + |
| 37 | + # Membership testing |
| 38 | + print(f"Is 3 in set a? {3 in a}") |
| 39 | + print(f"Is 6 in set a? {6 in a}") |
| 40 | + |
| 41 | + # Adding elements |
| 42 | + c = a.copy() |
| 43 | + c.add(6) # Add a single element |
| 44 | + print(f"After adding 6: {c}") |
| 45 | + |
| 46 | + # Removing elements |
| 47 | + c.remove(6) # Raises KeyError if element doesn't exist |
| 48 | + print(f"After removing 6: {c}") |
| 49 | + |
| 50 | + c.discard(10) # No error if element doesn't exist |
| 51 | + |
| 52 | + # Pop - removes and returns an arbitrary element |
| 53 | + popped = c.pop() |
| 54 | + print(f"Popped element: {popped}, Set after pop: {c}") |
| 55 | + |
| 56 | + # Clear - removes all elements |
| 57 | + d = a.copy() |
| 58 | + d.clear() |
| 59 | + print(f"After clear: {d}") |
| 60 | + |
| 61 | + return a, b |
| 62 | + |
| 63 | + |
| 64 | +def set_mathematical_operations(): |
| 65 | + """Demonstrate mathematical set operations.""" |
| 66 | + a = {1, 2, 3, 4, 5} |
| 67 | + b = {4, 5, 6, 7, 8} |
| 68 | + |
| 69 | + # Union (OR): elements in either set |
| 70 | + union1 = a | b |
| 71 | + print(f"Union: {union1}") |
| 72 | + # Using method syntax (alternative) |
| 73 | + print(f"Union (using method): {a.union(b)}") |
| 74 | + |
| 75 | + # Intersection (AND): elements in both sets |
| 76 | + intersection1 = a & b |
| 77 | + print(f"Intersection: {intersection1}") |
| 78 | + # Using method syntax (alternative) |
| 79 | + print(f"Intersection (using method): {a.intersection(b)}") |
| 80 | + |
| 81 | + # Difference: elements in first set but not in second |
| 82 | + difference1 = a - b |
| 83 | + print(f"Difference (a - b): {difference1}") |
| 84 | + # Using method syntax (alternative) |
| 85 | + print(f"Difference (using method): {a.difference(b)}") |
| 86 | + |
| 87 | + # Symmetric difference: elements in either set, but not in both |
| 88 | + sym_diff1 = a ^ b |
| 89 | + print(f"Symmetric difference: {sym_diff1}") |
| 90 | + # Using method syntax (alternative) |
| 91 | + print(f"Symmetric difference (using method): {a.symmetric_difference(b)}") |
| 92 | + |
| 93 | + # Subset and superset |
| 94 | + c = {1, 2} |
| 95 | + print(f"Is c subset of a? {c.issubset(a)}") |
| 96 | + print(f"Is a superset of c? {a.issuperset(c)}") |
| 97 | + |
| 98 | + return union1, intersection1, difference1, sym_diff1 |
| 99 | + |
| 100 | + |
| 101 | +def set_comprehensions(): |
| 102 | + """Demonstrate set comprehensions.""" |
| 103 | + # Basic set comprehension |
| 104 | + squares = {x**2 for x in range(10)} |
| 105 | + print(f"Squares: {squares}") |
| 106 | + |
| 107 | + # Conditional set comprehension |
| 108 | + even_squares = {x**2 for x in range(10) if x % 2 == 0} |
| 109 | + print(f"Even squares: {even_squares}") |
| 110 | + |
| 111 | + # Using set to remove duplicates from a list |
| 112 | + numbers = [1, 2, 2, 3, 3, 3, 4, 4, 5] |
| 113 | + unique_numbers = list(set(numbers)) |
| 114 | + print(f"Original list: {numbers}") |
| 115 | + print(f"Unique numbers: {unique_numbers}") |
| 116 | + |
| 117 | + return squares, even_squares, unique_numbers |
0 commit comments