Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. # Create an empty set using the constructor method.
  2. numbers = set()
  3. print(numbers) # Output: set()
  4. # Note: {} creates a dictionary in Python.
  5. print(type({})) # Output: <class 'dict'>
  6.  
  7. # set() constructor function takes an iterable as input.
  8. numbers = set([1, 2])
  9. print(numbers) # Output: {1, 2}
  10. string_set = set("hello")
  11. print(string_set) # Output: {'o', 'e', 'l', 'h'}
  12.  
  13. # Sets with some elements can also be created using {}.
  14. numbers = {1, 2, 1, 2, 3, 5}
  15. print(numbers) # Output: {1, 2, 3, 5}
  16.  
  17. # Set contains only unique elements. But they can contain elements of different types.
  18. random_set = {'a', 'a', 1, 2, 1}
  19. print(random_set) # Output: {1, 2, 'a'}
  20.  
  21. # Length of a set.
  22. numbers = {1, 2, 3}
  23. print(len(numbers)) # Output: 3
  24.  
  25. # Check if an element is in a set using 'in'
  26. print(1 in {1, 2, 3}) # Output: True
  27. print(5 in {1, 2, 3}) # Output: False
  28. print(1 not in {1, 2, 3}) # Output: False
  29.  
  30. # Access elements in a set
  31. numbers = {1, 2, 3}
  32. for number in numbers:
  33. print(number)
  34.  
  35. # Union: Elements in either set_1, set_2 or both.
  36. set_1 = {1, 2, 3, 4}
  37. set_2 = {3, 4, 5, 6}
  38. print(set_1.union(set_2)) # Output: {1, 2, 3, 4, 5, 6}
  39. print(set_1 | set_2) # Output: {1, 2, 3, 4, 5, 6}
  40.  
  41. # Intersection: Elements in both set_1 and set_2
  42. set_1 = {1, 2, 3, 4}
  43. set_2 = {3, 4, 5, 6}
  44. print(set_1.intersection(set_2)) # Output: {3, 4}
  45. print(set_1 & set_2) # Output: {3, 4}
  46.  
  47. # Set difference: Elements in set_1 not in set_2
  48. set_1 = {1, 2, 3, 4}
  49. set_2 = {3, 4, 5, 6}
  50. print(set_1.difference(set_2)) # Output: {1, 2}
  51. print(set_1 - set_2) # Output: {1, 2}
  52.  
  53. # Symmetric difference: Elements in set_1 or set_2, but not in both.
  54. set_1 = {1, 2, 3, 4}
  55. set_2 = {3, 4, 5, 6}
  56. print(set_1.symmetric_difference(set_2)) # Output: {1, 2, 5, 6}
  57. print(set_1 ^ set_2) # Output: {1, 2, 5, 6}
  58.  
  59. # Check if a set is a subset of another.
  60. set_1 = {1, 2}
  61. set_2 = {1, 2, 3}
  62. print(set_1.issubset(set_2)) # Output: True
  63. print(set_2.issubset(set_1)) # Output: False
  64.  
  65. # Check if a set is a superset of another.
  66. set_1 = {1, 2}
  67. set_2 = {1, 2, 3}
  68. print(set_1.issuperset(set_2)) # Output: False
  69. print(set_2.issuperset(set_1)) # Output: True
  70.  
  71. # Add one element to set using add() methd.
  72. numbers = {1, 2, 3}
  73. numbers.add(4) # <- takes a single hashable element.
  74. print(numbers) # Output: {1, 2, 3, 4}
  75.  
  76. # Add multiple elements to set using update() methd.
  77. numbers = {1, 2, 3}
  78. numbers.update([4, 5]) # <- takes any iterable.
  79. print(numbers) # Output: {1, 2, 3, 4, 5}
  80.  
  81. # Remove elements using remove() method.
  82. numbers = {1, 2, 3}
  83. numbers.remove(1)
  84. print(numbers) # Output: {2, 3}
  85. numbers.remove(5) # Raises KeyError if element is not present.
  86.  
  87. # Remove elements using discard element.
  88. numbers = {1, 2, 3}
  89. numbers.discard(1)
  90. print(numbers) # Output: {2, 3}
  91. numbers.discard(5) # Does not raise any error even if element is not present.
  92.  
  93. # Remove and get the last item in a set, sets are unordered so any element could get removed!
  94. numbers = {1, 2, 3}
  95. print(numbers.pop()) # Output: 3
  96. print(numbers) # Output: {1, 2}
  97.  
  98. # Empty set using clear()
  99. numbers = {1, 2, 3}
  100. numbers.clear()
  101. print(numbers) # Output: set()
  102.  
  103. # Delete set using del keyword
  104. numbers = {1, 2}
  105. del numbers
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement