Python Sets
Riya Jacob K
Assistant Professor on Contract
Dept of Computer Applications
Set
• A set is a collection which is unordered and
unindexed.
• In Python sets are written with curly brackets.
• # Note: the set list is unordered, meaning: the
items will appear in a random order.
Example
Create a Set:
thisset = {"apple", "banana", "cherry"}
print(thisset)
Output
{'cherry', 'banana', 'apple'}
• Output
apple
banana
cherry
• Output
True
• Output
{'orange', 'apple', 'banana', 'cherry'}
• Output
{'apple', 'grapes', 'mango', 'banana',
'cherry', 'orange'}
• Output
3
• Output
{'cherry', 'apple'}
• Output
{'cherry', 'apple'}
• Output
banana
{'apple', 'cherry'}
• Output
set()
• Output
Traceback (most recent call last):
File "demo_set_del.py", line 5, in <module>
print(thisset) #this will raise an error because the set no
longer exists
NameError: name 'thisset' is not defined