Python Sets Made Easy π₯ | Union, Intersection & Difference | Python Lesson 13
About this video
Learn Python Sets β Unique Values and Set Operations step by step with PEARL INSTITUTE BATALA.
In Lesson 13 of our Python + Machine Learning Practical Course, you will learn how Python Sets store unique values, remove duplicates automatically, and perform powerful operations such as Union, Intersection and Difference.
This lesson is explained in easy Hindi/Hinglish with practical coding examples, common mistakes and a quick practice question.
β In this lesson you will learn:
β’ What is a Set in Python?
β’ How to Create a Set
β’ Curly Braces { } in Sets
β’ Unique Values in Sets
β’ Automatic Removal of Duplicate Values
β’ Why Sets are Unordered
β’ Why Sets Do Not Support Indexing
β’ add() Method
β’ discard() Method
β’ Union using |
β’ Intersection using &
β’ Difference using -
β’ Membership Testing using in
β’ Practical Set Examples
β’ Common Set Errors
β’ Quick Practice Question
β’ Use of Sets in Machine Learning and Data Preparation
π» Basic Python Set Example
subjects = {"Python", "Java", "Python"}
Even though "Python" appears twice, the set stores only unique values.
print(len(subjects))
Output:
2
π Important Rule
Python Sets automatically remove duplicate values.
Sets are also unordered, so you should not rely on the position of items.
This is invalid:
colors[0]
A set does not support numeric indexing.
Instead, use membership testing:
print("red" in colors)
Output:
True
π₯ Important Set Operations
Union |
Combines unique values from both sets.
{1, 2, 3} | {3, 4, 5}
Result:
{1, 2, 3, 4, 5}
Intersection &
Returns values present in both sets.
{1, 2, 3} & {3, 4, 5}
Result:
{3}
Difference -
Returns values present in the first set but not in the second.
{1, 2, 3} - {3, 4, 5}
Result:
{1, 2}
π― Perfect For:
Python Beginners
Coding Beginners
BCA / B.Sc. IT / MCA Students
School & College Students
Programming Students
Machine Learning Beginners
Computer Course Students
Students Learning Python from Scratch
π€ Machine Learning Connection
Python Sets are useful during data preparation for:
β’ Removing duplicate values
β’ Comparing categories
β’ Checking membership
β’ Finding common values
β’ Finding distinct values
β’ Cleaning datasets before further processing
π Course: Python + Machine Learning Practical Course
π Lesson 13: Python Sets β Unique Values and Set Operations
π« PEARL INSTITUTE BATALA
π Near Bus Stand, Jalandhar Road, BATALA
π Call / WhatsApp: 7814976999
π www.PEARLINSTITUTE.in
π©βπ« Institute Head: Mrs Sonika Malhotra
π Subscribe to continue learning Python step by step.
π Next Lesson: Python Dictionaries β Key-Value Data