jabela

Untitled

Sep 18th, 2024 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. # Example sets
  2. set_a = {1, 2, 3, 4, 5}
  3. set_b = {4, 5, 6, 7, 8}
  4.  
  5. # Union: Combine the elements of both sets (no duplicates)
  6. union_set = set_a.union(set_b)
  7. print("Union of A and B:", union_set)
  8. # Expected Output: Union of A and B: {1, 2, 3, 4, 5, 6, 7, 8}
  9.  
  10. # Intersection: Find elements present in both sets
  11. intersection_set = set_a.intersection(set_b)
  12. print("Intersection of A and B:", intersection_set)
  13. # Expected Output: Intersection of A and B: {4, 5}
  14.  
  15. # Difference: Find elements in set A that are not in set B
  16. difference_set = set_a.difference(set_b)
  17. print("Difference of A and B (elements in A but not in B):", difference_set)
  18. # Expected Output: Difference of A and B (elements in A but not in B): {1, 2, 3}
  19.  
  20. # Subset: Check if set A is a subset of set B
  21. is_subset = set_a.issubset(set_b)
  22. print("Is A a subset of B?", is_subset)
  23. # Expected Output: Is A a subset of B? False
  24.  
  25. # Additional example for subset
  26. set_c = {1, 2, 3}
  27. is_subset_c_in_a = set_c.issubset(set_a)
  28. print("Is C a subset of A?", is_subset_c_in_a)
  29. # Expected Output: Is C a subset of A? True
  30.  
Advertisement
Add Comment
Please, Sign In to add comment