🐍 Understanding Python Sets in Simple Way 🎴


Python set example showing add and update methods with unordered unique values
Python Sets tutorial explaining unordered property, no duplicate values, add() and update() methods with examples.


A Set in Python is like a set of cards in a card pack. It is unordered, does not allow duplicate values, and is mutable (can change) ✅.

📌 Creating a Set

sports = {"tennis", "cricket", "football"}

print(sports)
print(type(sports))

Output example:

{'cricket', 'tennis', 'football'}

The order may change because sets are unordered.

🚫 No Indexing

Sets cannot be accessed using index numbers. Because they are unordered, there is no position number.

📏 Length of a Set

print(len(sports))

len() function returns the number of items in the set.

📦 Different Data Types

Sets can store different data types.

mixed_set = {"apple", 10, True}

🔄 Mutable Property

Sets are mutable ➜ That means you can add or update items.

➕ add() Method

add() is used to add a single element.

fruits = {"apple", "banana", "orange"}

fruits.add("grapes")
print(fruits)

Output example:

{'apple', 'orange', 'grapes', 'banana'}

🔗 update() Method

update() is used to combine two sets.

fruits = {"apple", "banana", "orange"}
animals = {"dog", "cat"}

fruits.update(animals)
print(fruits)

Output example:

{'apple', 'dog', 'cat', 'banana', 'orange'}

🎯 Sets are useful when you want unique values only and do not care about order. They are commonly used in Data Science and filtering duplicate data 🚀.

📚 Related Articles

Article No Article Title & Link
1 🐍 Getting Started with Python for Data Science – Fundamentals Guide 🚀
2 🐍 Python Collections (Arrays) Simplified: List, Tuple, Set, & Dictionary 🐍
3 🐍 Understanding Python Lists in Simple Way 📋
4 🐍 Understanding Python Tuples in Simple Way 📦
5 🐍 Understanding Python Sets in Simple Way 🎴
6 🐍 Understanding Python Range & Dictionaries 📘
7 🐍Python Operators Guide: Arithmetic, Logical, & Precedence Explained 🐍
8 🐍 Python Control Statements Complete Guide 🔄
9 🐍 Python Functions Explained in Simple Way ⚙️
10 🐍 Python Map, Filter, Lambda & Modules – Simple Notes
11 🐍 Python OOP Concepts – Simple Short Notes
12 🐍  NumPy, Pandas & Web Scraping – Simple Python Notes📊

Post a Comment

Previous Post Next Post