0% found this document useful (0 votes)
7 views3 pages

1 C Set

Python offers two set data structures: Set (mutable, unordered collection of unique elements) and Frozenset (immutable version). Sets allow for adding and removing elements and support mathematical operations, while Frozensets cannot be modified after creation but can still perform set operations. Use Set for mutable collections and Frozenset for immutable needs, such as dictionary keys.

Uploaded by

kalpanapriyam213
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

1 C Set

Python offers two set data structures: Set (mutable, unordered collection of unique elements) and Frozenset (immutable version). Sets allow for adding and removing elements and support mathematical operations, while Frozensets cannot be modified after creation but can still perform set operations. Use Set for mutable collections and Frozenset for immutable needs, such as dictionary keys.

Uploaded by

kalpanapriyam213
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Set and Frozenset in Python

Python provides two types of set data structures:

1. Set (set) – A mutable (changeable), unordered collection of unique elements.


2. Frozenset (frozenset) – An immutable (unchangeable) version of a set.

Both are used to store unique items and perform mathematical operations like union, intersection, and difference.

1. Set (set)
A set is an unordered collection of unique elements enclosed in curly braces {} or created using the set() function.

Characteristics of Sets:

✅ Unordered – The elements do not follow any specific order.


✅ Unique – Duplicate values are automatically removed.
✅ Mutable – Elements can be added or removed.
✅ Supports Mathematical Operations – Union, Intersection, Difference, etc.

Creating a Set
# Creating sets
set1 = {1, 2, 3, 4, 5} # Using curly braces
set2 = set([3, 4, 5, 6, 7]) # Using set() function

print(set1) # Output: {1, 2, 3, 4, 5}


print(set2) # Output: {3, 4, 5, 6, 7}

Basic Set Operations

1. Adding and Removing Elements


numbers = {1, 2, 3}

numbers.add(4) # Adds a single element


print(numbers) # Output: {1, 2, 3, 4}

numbers.update([5, 6, 7]) # Adds multiple elements


print(numbers) # Output: {1, 2, 3, 4, 5, 6, 7}

numbers.remove(3) # Removes element (raises error if not found)


numbers.discard(10) # No error if element is not found
print(numbers) # Output: {1, 2, 4, 5, 6, 7}

numbers.clear() # Removes all elements


print(numbers) # Output: set()

2. Set Operations (Mathematical)


A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}

print("Union:", A | B) # Output: {1, 2, 3, 4, 5, 6, 7, 8}


print("Intersection:", A & B) # Output: {4, 5}
print("Difference (A - B):", A - B) # Output: {1, 2, 3}
print("Symmetric Difference:", A ^ B) # Output: {1, 2, 3, 6, 7, 8}

3. Checking Membership
fruits = {"apple", "banana", "cherry"}

print("banana" in fruits) # Output: True


print("grape" in fruits) # Output: False

2. Frozenset (frozenset)
A frozenset is an immutable version of a set. Once created, elements cannot be added or removed.

Characteristics of Frozenset:

✅ Immutable – Elements cannot be changed after creation.


✅ Supports Set Operations – Can perform union, intersection, difference, etc.
✅ Useful as Dictionary Keys – Since it is immutable, it can be used as a dictionary key.

Creating a Frozenset
# Creating a frozenset
fset = frozenset([1, 2, 3, 4, 5])
print(fset) # Output: frozenset({1, 2, 3, 4, 5})

# Frozensets support set operations


A = frozenset([1, 2, 3])
B = frozenset([3, 4, 5])

print("Union:", A | B) # Output: frozenset({1, 2, 3, 4, 5})


print("Intersection:", A & B) # Output: frozenset({3})
print("Difference:", A - B) # Output: frozenset({1, 2})
print("Symmetric Difference:", A ^ B) # Output: frozenset({1, 2, 4, 5})

Attempting to Modify a Frozenset

fset.add(6) # ❌ Error! 'frozenset' object has no attribute 'add'

Practical Example: Removing Duplicates from a List


A set can be used to remove duplicate elements from a list.

numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = set(numbers) # Removes duplicates
print(unique_numbers) # Output: {1, 2, 3, 4, 5}
Summary
Feature Set (set) Frozenset (frozenset)

Mutable? ✅ Yes ❌ No

Ordered? ❌ No ❌ No

Unique Elements? ✅ Yes ✅ Yes

Supports Set Operations? ✅ Yes ✅ Yes

Allows Adding/Removing Elements? ✅ Yes ❌ No

Usable as Dictionary Key? ❌ No ✅ Yes

✅ Use set when you need a mutable collection of unique elements.


✅ Use frozenset when you need an immutable set (e.g., as a dictionary key).

You might also like