Sets and Dictionaries in Python L5
Sets and Dictionaries in Python L5
Sets and Dictionaries in Python L5
DICTIONARIES
IN PYTHON
Mr. Michael Angelo I. Dungo
Instructor I
SETS
• Sets are unordered collections of unique elements. A set is not a sequence type in
Python.
• Elements cannot be accessed by index;
• A set is a mutable object and that means it can be modified.
• A set uses curly braces and commas to separate its elements.
• s1 = {1, 4.5, 'abc'}
• You cannot access items in a set by index because there is no order.
• s1[0]
• Traceback (most recent call last):
• File "<pyshell#33>", line 1, in <module>
• s1[0]
• TypeError: 'set' object does not support indexing
• You cannot access items in a set by index because there is no order.
• There is no concept of the first element of the set. The second element, the third element and
so on.
• In real world, there are many collections that are unordered without duplicates.
• Social Security numbers, email addresses, IP addresses and so on.
• All of these are collections of unique and unordered elements.
• Duplicates are not allowed if we want to have such collections in our applications.
How to check whether a certain element is in the Set?
SET METHODS