Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Example sets
- set_a = {1, 2, 3, 4, 5}
- set_b = {4, 5, 6, 7, 8}
- # Union: Combine the elements of both sets (no duplicates)
- union_set = set_a.union(set_b)
- print("Union of A and B:", union_set)
- # Expected Output: Union of A and B: {1, 2, 3, 4, 5, 6, 7, 8}
- # Intersection: Find elements present in both sets
- intersection_set = set_a.intersection(set_b)
- print("Intersection of A and B:", intersection_set)
- # Expected Output: Intersection of A and B: {4, 5}
- # Difference: Find elements in set A that are not in set B
- difference_set = set_a.difference(set_b)
- print("Difference of A and B (elements in A but not in B):", difference_set)
- # Expected Output: Difference of A and B (elements in A but not in B): {1, 2, 3}
- # Subset: Check if set A is a subset of set B
- is_subset = set_a.issubset(set_b)
- print("Is A a subset of B?", is_subset)
- # Expected Output: Is A a subset of B? False
- # Additional example for subset
- set_c = {1, 2, 3}
- is_subset_c_in_a = set_c.issubset(set_a)
- print("Is C a subset of A?", is_subset_c_in_a)
- # Expected Output: Is C a subset of A? True
Advertisement
Add Comment
Please, Sign In to add comment