Python Sets
kodingwindow@kw:~$ python3
...
>>> set1 = {'Sunday','Monday','Tuesday','Sunday'}
>>> set2 = {'Wednesday','Thursday','Friday','Friday','Saturday'}

>>> set1
{'Monday', 'Sunday', 'Tuesday'}
>>> set2
{'Wednesday', 'Friday', 'Thursday', 'Saturday'}

>>> len(set1)
3
>>> len(set2)
4

>>> max(set1)
'Tuesday'
>>> min(set1)
'Monday'

>>> max(set2)
'Wednesday'
>>> min(set2)
'Friday'
add() and update() methods
>>> set1
{'Monday', 'Sunday', 'Tuesday'}
>>> set2
{'Wednesday', 'Friday', 'Thursday', 'Saturday'}

>>> set1.add('Friday')    #Add one element
>>> set1
{'Monday', 'Friday', 'Sunday', 'Tuesday'}

>>> set2.update(['Sunday','Friday'])    #Add one or more element(s)
>>> set2
{'Wednesday', 'Friday', 'Sunday', 'Thursday', 'Saturday'}
remove(), discard(), and pop() methods
>>> set1
{'Monday', 'Friday', 'Sunday', 'Tuesday'}

>>> set2
{'Wednesday', 'Friday', 'Sunday', 'Thursday', 'Saturday'}

>>> set1.remove('Friday')    #Remove one element
>>> set1
{'Monday', 'Sunday', 'Tuesday'}

>>> set2.difference_update(['Friday','Sunday'])    #Remove one or more element(s)
>>> set2
{'Wednesday', 'Thursday', 'Saturday'}

>>> set2.update(['Sunday','Friday'])
>>> set2
{'Wednesday', 'Friday', 'Sunday', 'Thursday', 'Saturday'}

>>> set2 -= {'Friday','Sunday'}    #Remove one or more element(s)
>>> set2
{'Wednesday', 'Thursday', 'Saturday'}

>>> set1
{'Monday', 'Sunday', 'Tuesday'}

>>> set1.remove('Friday')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'Friday'

>>> set1.discard('Friday')    #No error, if element is not present
>>> set1
{'Monday', 'Sunday', 'Tuesday'}

>>> set1.discard('Sunday')
>>> set1
{'Monday', 'Tuesday'}

>>> set1.pop()
'Monday'

>>> set1
{'Tuesday'}

>>> set2
{'Wednesday', 'Thursday', 'Saturday'}

>>> set2.clear()
>>> set2
set()

>>> set1.clear()
>>> set1
set()
union() and intersection() methods
>>> set1 = {'Sunday','Monday','Friday','Sunday'}
>>> set2 = {'Wednesday','Thursday','Friday','Friday','Saturday'}

>>> set1
{'Monday', 'Friday', 'Sunday'} 
>>> set2
{'Wednesday', 'Friday', 'Thursday', 'Saturday'}
 
>>> set1.union(set2)
{'Wednesday', 'Monday', 'Friday', 'Sunday', 'Thursday', 'Saturday'}

>>> set1
{'Monday', 'Friday', 'Sunday'}
>>> set2
{'Wednesday', 'Friday', 'Thursday', 'Saturday'}

>>> set1.intersection(set2)
{'Friday'}
difference() and symmetric_difference() methods
>>> set1 = {'Sunday','Monday','Friday','Sunday'}
>>> set2 = {'Wednesday','Thursday','Friday','Friday','Saturday'}

>>> set1.difference(set2)
{'Monday', 'Sunday'}
>>> set2.difference(set1)
{'Thursday', 'Saturday', 'Wednesday'}

>>> set1.symmetric_difference(set2)
{'Sunday', 'Monday', 'Saturday', 'Thursday', 'Wednesday'}
>>> set1
{'Monday', 'Sunday', 'Friday'}

>>> set1.symmetric_difference_update(set2)
>>> set1
{'Sunday', 'Monday', 'Saturday', 'Thursday', 'Wednesday'}
Built-in set() function and operations
>>> s={1, 1, 2, 2, 1, 4, 3, 3, 6, 6, 5, 6}
>>> s
{1, 2, 3, 4, 5, 6}

>>> s[1:3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'set' object is not subscriptable

>>> s.update([7, 4, 0, 2])
>>> s
{0, 1, 2, 3, 4, 5, 6, 7}

>>> s.remove(10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 10

>>> s.remove(1)
>>> s
{0, 2, 3, 4, 5, 6, 7}

>>> s=set("Hello, World!")
>>> s
{'l', 'o', ' ', 'd', ',', '!', 'e', 'W', 'H', 'r'} 
Advertisement