set is unordered collection of unique and immutable objects.set is mutable data type but does not support indexing/ slicing.set¶set1 = {1, 2, 3, 4, 5}
set2 = {'string'}
print(set1)
print(set2)
{1, 2, 3, 4, 5}
{'string'}
week_days = set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
set3 = set('string')
set4 = set(['string'])
print(week_days)
print(set3)
print(set4)
{'Tue', 'Mon', 'Sat', 'Thu', 'Fri', 'Wed', 'Sun'}
{'n', 's', 't', 'i', 'g', 'r'}
{'string'}
set1 = set([1,2,3,4,5])
print(set1)
{1, 2, 3, 4, 5}
set2 = set1
print(set2)
{1, 2, 3, 4, 5}
set2.add(10)
print(set1)
{1, 2, 3, 4, 5, 10}
set3 = set1.copy()
set3.add(20)
print(set3, set1)
{1, 2, 3, 4, 5, 20, 10} {1, 2, 3, 4, 5, 10}
# Empty sets must be created with the set built-in, and print in the same way
empty_set = set()
print(empty_set)
set()
week_days = set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
for day in week_days:
print(day, end=' ')
Tue Mon Sat Thu Fri Wed Sun
week_days = set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
print(week_days)
{'Tue', 'Mon', 'Sat', 'Thu', 'Fri', 'Wed', 'Sun'}
week_days.discard("Sun")
print(week_days)
{'Tue', 'Mon', 'Sat', 'Thu', 'Fri', 'Wed'}
set operations¶s1 = set(i for i in range(1,7))
print(s1)
{1, 2, 3, 4, 5, 6}
s2 = set(range(4,11))
print(s2)
{4, 5, 6, 7, 8, 9, 10}
1 in s1
True
5 in s2
True
34 in s2
False
|¶s_union = s1 | s2
print('using pipe symbol'.ljust(22), s_union)
print("using union methods".ljust(22), s1.union(s2))
print(s1)
print(s2)
using pipe symbol {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
using union methods {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{1, 2, 3, 4, 5, 6}
{4, 5, 6, 7, 8, 9, 10}
&¶s_intersection = s1 & s2
print(s_intersection)
{4, 5, 6}
s_intersection = s1.intersection(s2)
print(s_intersection)
print(s1, s2)
s1.intersection_update(s2)
print(s1)
{4, 5, 6}
{1, 2, 3, 4, 5, 6} {4, 5, 6, 7, 8, 9, 10}
{4, 5, 6}
# Set Difference
s1 = {1, 2, 3, 4, 5}
s2 = {4, 5, 6}
s_diff = s1 - s2 # set difference. Elements in s1 but not in s2
print(s_diff)
{1, 2, 3}
# Symmetric Difference
s1 = {1, 2, 3, 4, 5}
s2 = {4, 5, 6, 7, 8}
# Elements in s1 and s2 but not in both (excluding the intersection)
sym_diff = s1.symmetric_difference(s2)
print(sym_diff)
{1, 2, 3, 6, 7, 8}