Sets and Dictionaries
Sets and Dictionaries
Registration no : COSC232101044
Class : BSCS 4A
Course : AI LAB
Sets
Definition
Sets are unordered collections of unique items. They are mutable but do not allow
duplicate elements.
Characteristics
Syntax
# Creating a set
Common Operations
my_set.add('banana')
if 'banana' in my_set:
another_set = {4, 5}
Dictionaries
Definition
Dictionaries are mutable collections of key-value pairs. Each key must be unique.
Characteristics
Syntax
# Creating a dictionary
Common Operations
my_dict['age'] = 26
print(my_dict) # Output: {'name': 'Alice', 'age': 26}
del my_dict['age']
if 'name' in my_dict:
Key Differences
Conclusion
Lists, tuples, sets, and dictionaries are fundamental data structures in Python. Choose lists when you need
a mutable collection of items, tuples when you need an immutable collection, sets when you need a unique
collection, and dictionaries when you need a key-value pair collection. Understanding these differences
will help you make efficient decisions in your Python programming.