A set is a collection of items in Python that is unordered, unindexed, and does not allow duplicate values. Sets are useful when you need to store unique items.
A set stores multiple values in a single variable. Unlike lists and tuples, a set does not keep duplicate values.
numbers = {10, 20, 30, 40}
print(numbers)
You can create a set by placing values inside curly braces.
fruits = {"apple", "banana", "mango"}
print(fruits)
The order in which set elements are displayed should not be relied upon.
If you add the same value multiple times, a set keeps only one copy.
numbers = {10, 20, 10, 30, 20, 40}
print(numbers)
Sets are unordered collections. Therefore, you should not depend on the display order of their elements.
colors = {"red", "green", "blue"}
print(colors)
The elements may be displayed in an order different from the order in which they were written.
Because sets are unordered, you cannot access their elements using indexes.
numbers = {10, 20, 30}
print(numbers[0])
You can also create a set using the set() constructor.
numbers = set([10, 20, 30, 40])
print(numbers)
To create an empty set, use set().
my_set = set()
print(my_set)
{} creates an empty dictionary, not an empty set.
empty_set = set()
empty_dictionary = {}
print(type(empty_set))
print(type(empty_dictionary))
A set can contain values of different data types, provided the values are hashable.
data = {10, "Python", 20.5, True}
print(data)
The display order is not guaranteed.
Set elements must be hashable. For example, a list cannot be directly stored as an element of a set.
numbers = {[1, 2], [3, 4]}
Immutable values such as numbers, strings, and tuples can generally be used as set elements.
Use the len() function to find the number of elements in a set.
fruits = {"apple", "banana", "mango"}
print(len(fruits))
The in operator checks whether an element exists in a set.
fruits = {"apple", "banana", "mango"}
print("apple" in fruits)
print("orange" in fruits)
The not in operator checks whether an element is absent from a set.
fruits = {"apple", "banana", "mango"}
print("orange" not in fruits)
The add() method adds one element to a set.
fruits = {"apple", "banana"}
fruits.add("mango")
print(fruits)
Adding an element that already exists does not create a duplicate.
numbers = {10, 20, 30}
numbers.add(20)
print(numbers)
The remove() method removes a specified element.
numbers = {10, 20, 30, 40}
numbers.remove(30)
print(numbers)
If the specified element does not exist, remove() raises a KeyError.
The discard() method also removes an element, but unlike remove(), it does not raise an error if the element is absent.
numbers = {10, 20, 30}
numbers.discard(50)
print(numbers)
The clear() method removes all elements from a set.
numbers = {10, 20, 30}
numbers.clear()
print(numbers)
You can use a for loop to access each element of a set.
fruits = {"apple", "banana", "mango"}
for fruit in fruits:
print(fruit)
The elements may be printed in any order because sets are unordered.
One common use of sets is removing duplicate values from a list.
numbers = [10, 20, 10, 30, 20, 40]
unique_numbers = set(numbers)
print(unique_numbers)
Python sets support useful mathematical set operations such as:
These operations will be covered in detail in the next lessons.
students = {"Amit", "Rahul", "Priya", "Amit"}
print("Students:", students)
print("Total:", len(students))
if "Rahul" in students:
print("Rahul is present")
Question: Which feature is true about Python sets?