Topic 15: Set in python?

Set Data Type in Python

Python provides several built-in data types, and Set is one of the most powerful and useful among them. A set is an unordered collection of unique elements, commonly used for operations like removing duplicates, mathematical set operations, and fast membership testing.

You will learn:
✔ What a set is
✔ How to create sets in Python
✔ Important operations on sets
✔ Built-in functions and methods
✔ Practical examples

 What is a set in Python?

A set in Python is a collection of unordered, unindexed, and unique elements.
It is defined using curly braces { } or the set() constructor.

Example

my_set = {1, 2, 3, 4}
print(my_set)      # {1, 2, 3, 4}

mixed_set = {10, "Python", 3.14}
print(mixed_set)   # Elements in random order

 Key Characteristics of Sets

Feature Description
Unordered Elements have no fixed position
Unique elements Duplicate values are automatically removed
Mutable You can add or remove elements
No indexing Elements cannot be accessed using index
Supports mathematical operations Union, Intersection, Difference, etc.

Creating Sets in Python

1. Using Curly Braces {}

s = {10, 20, 30}

2. Using set() Constructor

s = set([1, 2, 3, 4])

3. Empty Set

s = set()        # Correct
s = {}           # Creates empty dictionary, NOT set

Set Operations in Python

Python provides powerful set operations similar to mathematics.

 1. Union (| or union())

Combines all unique elements from both sets.

A = {1, 2, 3}
B = {3, 4, 5}

print(A | B)           # {1, 2, 3, 4, 5}
print(A.union(B))      # {1, 2, 3, 4, 5}

 2. Intersection (& or intersection())

Returns common elements.

A = {1, 2, 3}
B = {2, 3, 4}

print(A & B)                   # {2, 3}
print(A.intersection(B))      # {2, 3}

 3. Difference (- or difference())

Elements present in first set but not in second.

A = {1, 2, 3}
B = {2, 3, 4}

print(A - B)                   # {1}
print(A.difference(B))        # {1}

 4. Symmetric Difference (^ or symmetric_difference())

Elements that are in either set, but not in both.

A = {1, 2, 3}
B = {3, 4, 5}

print(A ^ B)                         # {1, 2, 4, 5}
print(A.symmetric_difference(B))     # {1, 2, 4, 5}

5. Membership Testing (in, not in)

A = {10, 20, 30}

print(20 in A)      # True
print(100 not in A) # True

Built-in Set Methods in Python

Below is a list of commonly used set methods:

Method Description
add() Adds a single element
update() Adds multiple elements
remove() Removes an element (Error if not found)
discard() Removes an element (No error if not found)
pop() Removes and returns a random element
clear() Removes all elements
copy() Returns a shallow copy
union() Returns union of sets
intersection() Returns intersection
difference() Returns difference
symmetric_difference() Returns symmetric difference
issubset() Checks if set is subset
issuperset() Checks if set is superset
isdisjoint() Checks if sets have no common elements

Examples of Built-in Methods

1. Adding Elements

s = {1, 2, 3}
s.add(4)
print(s)       # {1, 2, 3, 4}

2. Updating Multiple Items

s = {1, 2}
s.update([3, 4, 5])
print(s)       # {1, 2, 3, 4, 5}

3. Removing Elements

s = {10, 20, 30}
s.remove(20)
print(s)   # {10, 30}

4. Discard (No Error if Not Found)

s = {1, 2, 3}
s.discard(5)     # No error
print(s)

Practical Example: Removing Duplicates from a List

One of the most common uses of sets:

numbers = [1, 2, 2, 3, 3, 4, 4]
unique_numbers = list(set(numbers))

print(unique_numbers)     # Output will be unique elements

Flowchart: How Sets Work

Set in python

Conclusion

The set data type in Python is extremely useful when you need to work with unique values, perform mathematical operations, or quickly check membership. Because sets are optimized for fast lookups and duplicate removal, they play a key role in data processing and algorithm development.

Using the operations and built-in functions explained above, you can easily manage and manipulate sets in Python.

 ðŸ‘ˆðŸ‘ˆPrevious topic (List)                                       Quiz Next👉👉 

Comments

Popular posts from this blog

Topic1 :- What is Python Programming?

Topic2: -Why Python?

Topic7: What is Numpy?