File: exp4.ipynb
Experiment: Study of Sets in Python
Name: Ronit Mehta
PRN: 25070123094
To study and implement Python Sets
A set in Python is an unordered collection of unique elements, written inside curly braces {}.
- Do not allow duplicate values
- Elements are unordered
- Sets are mutable (elements can be added or removed)
- Indexing is not supported
- Support mathematical operations like:
- Union
- Intersection
- Difference
- Symmetric Difference
Python also provides frozenset, which is an immutable version of a set and cannot be modified after creation.
Sets are commonly used for duplicate removal, membership testing, and solving problems involving relationships between groups.
- Create a set of elements.
- Display the set.
- Attempt to access an element using indexing to show that sets do not support indexing.
- Create a set containing different data types.
- Display the set to observe how elements are stored uniquely.
- Create a set with duplicate values.
- Display the set.
- Observe that duplicate elements are automatically removed.
- Create a set.
- Add a new element using the
add()method. - Display the updated set.
- Create a set.
- Remove an element using the
remove()method. - Display the modified set.
- Create two sets with numeric values.
- Perform:
- Union
- Intersection
- Difference
- Symmetric Difference
- Display the results using operators and methods.
- Create a
frozenset. - Display its type.
- Attempt to modify it to demonstrate immutability.
- Create a list containing duplicate elements.
- Convert the list into a set.
- Display the unique elements.
- Create multiple sets.
- Find common elements using intersection.
- Display the result.
- Create sets representing different sports clubs.
- Find:
- Students playing both sports
- Students playing only one sport
- Display the results.
- Create a set of all students.
- Create a set of present students.
- Find absent students using set difference.
- Display the result.
- Create a set of elements.
- Remove an element using
remove(). - Remove another element using
discard(). - Observe the difference in behavior.
This experiment demonstrates the effective use of Python Sets for handling unique data and performing mathematical set operations. It highlights how sets simplify tasks such as duplicate removal, comparison between groups, and real-life problem solving.
The experiment also clarifies the difference between mutable sets and immutable frozensets, making sets a powerful and efficient data structure in Python.