info = {"Carla", 19, False, 5.9, 19}
print(info)
{False, 19, 5.9, 'Carla'} # not in orignal set's order
Here we see that the items of set occur in random order and hence they cannot be accessed using index numbers.
set( )
s = set()
s1 = {}
print(type(s))
print(type(s1))
<class 'set'> # set
<class 'dict'> # dictionary
You can access items of set using a for loop.
info = {"Carla", 19, False, 5.9}
for item in info:
print(item)
False
Carla
19
5.9
The union() prints all items that are present in the two sets.
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities3 = cities.union(cities2)
print(cities3)
{'Tokyo', 'Madrid', 'Kabul', 'Seoul', 'Berlin', 'Delhi'}
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities.update(cities2) # Updating 'cities' set
print(cities)
{'Berlin', 'Madrid', 'Tokyo', 'Delhi', 'Kabul', 'Seoul'}
The intersection() prints only items that are similar to both the sets.
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities3 = cities.intersection(cities2)
print(cities3)
{'Madrid', 'Tokyo'}
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities.intersection_update(cities2)
print(cities)
{'Tokyo', 'Madrid'}
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities3 = cities.symmetric_difference(cities2)
print(cities3)
{'Seoul', 'Kabul', 'Berlin', 'Delhi'}
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities.symmetric_difference_update(cities2)
print(cities)
{'Kabul', 'Delhi', 'Berlin', 'Seoul'}