All technological notes.
Sets are used to store multiple items in a single variable.
Unordered
Unchangeable
Duplicates Not Allowed
True and 1 are considered the same value in sets, and are treated as duplicates.xset = {"apple", "banana", "cherry", True, 1, 2}
print(xset) # {'apple', True, 2, 'banana', 'cherry'}
Data Types
xset = {"abc", 34, True, 40, "male"}
print(xset) # {True, 34, 'abc', 40, 'male'}
| Method | Description |
|---|---|
set() |
return a set |
| Method | Description |
|---|---|
set() |
return a set |
copy() |
Returns a copy of the set |
| Method | Description |
|---|---|
add() |
Adds an element to the set |
| Method | Description |
|---|---|
update() |
Update the set with the union of this set and others |
difference_update() |
Removes the items in this set that are also included in another, specified set |
intersection_update() |
Removes the items in this set that are not present in other, specified set(s) |
symmetric_difference_update() |
inserts the symmetric differences from this set and another |
| Method | Description |
|---|---|
pop() |
Removes an element from the set |
remove() |
Removes the specified element |
discard() |
Remove the specified item |
clear() |
Removes all the elements from the set |
| Method | Description |
|---|---|
difference() |
Returns a set containing the difference between two or more sets |
intersection() |
Returns a set, that is the intersection of two other sets |
isdisjoint() |
Returns whether two sets have a intersection or not |
issubset() |
Returns whether another set contains this set or not |
issuperset() |
Returns whether this set contains another set or not |
symmetric_difference() |
Returns a set with the symmetric differences of two sets |
union() |
Return a set containing the union of sets |
set() Constructor# create a set
print("\n--------Create a Set--------\n")
# using curly brackets
xset = {"apple", "banana", "cherry"}
print(xset) # {'apple', 'banana', 'cherry'}
# using set() constructor
xset = set(("apple", "banana", "cherry")) # note the double round-brackets
print(xset) # {'cherry', 'banana', 'apple'}
add(): adds an elementadd(): add one item to a setprint("\n--------Add a Item:add()--------\n")
xlist = {"apple", "banana", "cherry"}
xlist.add("orange")
print(xlist) # {'cherry', 'orange', 'apple', 'banana'}
update(): adding items from another iterable.update(): add items from another set into the current set.
print("\n---------Add items: update()--------\n")
xset = {"apple", "banana", "cherry"}
yset = {"pineapple", "mango", "papaya"}
xset.update(yset)
print(xset) # {'papaya', 'banana', 'apple', 'cherry', 'pineapple', 'mango'}
zset = xset.update(yset)
print(zset) # None
# iterable object
xset = {"apple", "banana", "cherry"}
yset = ["pineapple", "mango", "papaya"]
xset.update(yset)
print(xset) # {'papaya', 'banana', 'apple', 'cherry', 'pineapple', 'mango'}
discard(value)remove an item in a set
discard() will NOT raise an error.# .discard()
xset = {"apple", "banana", "cherry"}
xset.discard("banana")
print(xset)
xset.discard("banana")
xset.discard("banana")
pop()remove a random item
# .pop()
xset = {"apple", "banana", "cherry"}
x = xset.pop() #
# x = xset.pop(1) # TypeError: set.pop() takes no arguments (1 given)
# x = xset.pop("banana") # TypeError: set.pop() takes no arguments (1 given)
print(xset)
print(x) # a random item removed from set
remove(item)remove an item in a set
# .remove()
xset = {"apple", "banana", "cherry"}
xset.remove("banana")
print(xset)
# xset.remove("banana") # KeyError: 'banana'
clear()# clear()
xset = {"apple", "banana", "cherry"}
xset.clear()
print(xset) # set()
del Operator# del
xset = {"apple", "banana", "cherry"}
del xset
# print(xset) # NameError: name 'xset' is not defined
copy(): copies the setx_set = {"apple", "banana", "cherry"}
y_set = x_set.copy()
print(y_set) # {'banana', 'apple', 'cherry'}
for in loop
print("\n--------for in loop--------\n")
# for in loop
xset = {"apple", "banana", "cherry"}
for x in xset:
print(x)
in Operator: Check if Item Existsxlist = {"apple", "banana", "cherry"}
if "apple" in xlist:
print("Yes, 'apple' is in the fruits list")