|
| 1 | +#https://newdigitals.org/2024/01/23/basic-python-programming/#sets |
| 2 | +#In Python, a Set is an unordered collection of data types that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements. |
| 3 | +#Set items are unchangeable, but you can remove items and add new items. |
| 4 | +#The values True (False) and 1 (0) are considered the same value in sets, and are treated as duplicates. |
| 5 | +#Set items can be of any data type. |
| 6 | +#The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set. |
| 7 | +#Sets are written with curly brackets: |
| 8 | +thisset = {"apple", "banana", "cherry"} |
| 9 | +print(thisset) |
| 10 | +Output: |
| 11 | +{'banana', 'cherry', 'apple'} |
| 12 | +#To determine how many items a set has, use the len() function |
| 13 | +thisset = {"apple", "banana", "cherry"} |
| 14 | + |
| 15 | +print(len(thisset)) |
| 16 | + |
| 17 | +Output: |
| 18 | +3 |
| 19 | +#Sets are defined as objects with the data type ‘set’: |
| 20 | +myset = {"apple", "banana", "cherry"} |
| 21 | +print(type(myset)) |
| 22 | +Output: |
| 23 | +<class 'set'> |
| 24 | +#It is possible to use the set() constructor to make a set |
| 25 | +thisset = set(("apple", "banana", "cherry")) # note the double round-brackets |
| 26 | +print(thisset) |
| 27 | +Output: |
| 28 | +{'banana', 'cherry', 'apple'} |
| 29 | +#Creating a Set |
| 30 | +# Python program to demonstrate |
| 31 | +# Creation of Set in Python |
| 32 | + |
| 33 | +# Creating a Set |
| 34 | +set1 = set() |
| 35 | +print("Initial blank Set: ") |
| 36 | +print(set1) |
| 37 | + |
| 38 | +# Creating a Set with |
| 39 | +# the use of a String |
| 40 | +set1 = set("GeeksForGeeks") |
| 41 | +print("\nSet with the use of String: ") |
| 42 | +print(set1) |
| 43 | + |
| 44 | +# Creating a Set with |
| 45 | +# the use of Constructor |
| 46 | +# (Using object to Store String) |
| 47 | +String = 'GeeksForGeeks' |
| 48 | +set1 = set(String) |
| 49 | +print("\nSet with the use of an Object: " ) |
| 50 | +print(set1) |
| 51 | + |
| 52 | +# Creating a Set with |
| 53 | +# the use of a List |
| 54 | +set1 = set(["Geeks", "For", "Geeks"]) |
| 55 | +print("\nSet with the use of List: ") |
| 56 | +print(set1) |
| 57 | + |
| 58 | +# Creating a Set with |
| 59 | +# the use of a tuple |
| 60 | +t=("Geeks","for","Geeks") |
| 61 | +print("\nSet with the use of Tuple: ") |
| 62 | +print(set(t)) |
| 63 | + |
| 64 | +# Creating a Set with |
| 65 | +# the use of a dictionary |
| 66 | +d={"Geeks":1,"for":2,"Geeks":3} |
| 67 | +print("\nSet with the use of Dictionary: ") |
| 68 | +print(set(d)) |
| 69 | + |
| 70 | +Output: |
| 71 | +Initial blank Set: |
| 72 | +set() |
| 73 | + |
| 74 | +Set with the use of String: |
| 75 | +{'k', 'e', 'F', 'r', 'o', 'G', 's'} |
| 76 | + |
| 77 | +Set with the use of an Object: |
| 78 | +{'k', 'e', 'F', 'r', 'o', 'G', 's'} |
| 79 | + |
| 80 | +Set with the use of List: |
| 81 | +{'Geeks', 'For'} |
| 82 | + |
| 83 | +Set with the use of Tuple: |
| 84 | +{'Geeks', 'for'} |
| 85 | + |
| 86 | +Set with the use of Dictionary: |
| 87 | +{'Geeks', 'for'} |
| 88 | +#Python has a set of built-in methods that you can use on sets: add, clear, copy, difference, difference_update, discard, intersection, intersection_update, isdisjoint, isubset, etc. |
| 89 | +#Once a set is created, you cannot change its items, but you can add new items. |
| 90 | +#To add one item to a set use the add() method |
| 91 | +thisset = {"apple", "banana", "cherry"} |
| 92 | + |
| 93 | +thisset.add("orange") |
| 94 | + |
| 95 | +print(thisset) |
| 96 | +Output: |
| 97 | +{'orange', 'banana', 'cherry', 'apple'} |
| 98 | +#To add items from another set into the current set, use the update() method, e.g. add elements from tropical into thisset |
| 99 | +thisset = {"apple", "banana", "cherry"} |
| 100 | +tropical = {"pineapple", "mango", "papaya"} |
| 101 | + |
| 102 | +thisset.update(tropical) |
| 103 | + |
| 104 | +print(thisset) |
| 105 | + |
| 106 | +Output: |
| 107 | +{'banana', 'cherry', 'mango', 'papaya', 'apple', 'pineapple'} |
| 108 | +#To remove an item in a set, use the remove(), or the discard() method |
| 109 | +thisset = {"apple", "banana", "cherry"} |
| 110 | + |
| 111 | +thisset.remove("banana") |
| 112 | + |
| 113 | +print(thisset) |
| 114 | + |
| 115 | +Output: |
| 116 | +{'cherry', 'apple'} |
| 117 | +#Other options: the clear() method empties the set; the del keyword will delete the set completely |
| 118 | +#Loop through the set, and print the values |
| 119 | +thisset = {"apple", "banana", "cherry"} |
| 120 | + |
| 121 | +for x in thisset: |
| 122 | + print(x) |
| 123 | +banana |
| 124 | +cherry |
| 125 | +apple |
| 126 | + |
| 127 | +#The union() method returns a new set with all items from both sets: |
| 128 | +set1 = {"a", "b" , "c"} |
| 129 | +set2 = {1, 2, 3} |
| 130 | + |
| 131 | +set3 = set1.union(set2) |
| 132 | +print(set3) |
| 133 | +Output: |
| 134 | +{1, 2, 3, 'a', 'b', 'c'} |
| 135 | +#The intersection() method will return a new set, that only contains the items that are present in both sets. |
| 136 | +x = {"apple", "banana", "cherry"} |
| 137 | +y = {"google", "microsoft", "apple"} |
| 138 | + |
| 139 | +z = x.intersection(y) |
| 140 | + |
| 141 | +print(z) |
| 142 | + |
| 143 | +Output: |
| 144 | +{'apple'} |
| 145 | +#The symmetric_difference_update() method will keep only the elements that are NOT present in both sets |
| 146 | +x = {"apple", "banana", "cherry"} |
| 147 | +y = {"google", "microsoft", "apple"} |
| 148 | + |
| 149 | +x.symmetric_difference_update(y) |
| 150 | + |
| 151 | +print(x) |
| 152 | +Output: |
| 153 | +{'banana', 'google', 'microsoft', 'cherry'} |
0 commit comments