Python - Sets
Why should I learn to solve Python: Sets technical interview questions?
Learn and practise solving Python: Sets technical interview questions and answers to enhance your skills for clearing technical interviews, HR interviews, campus interviews, and placement tests.
Where can I get technical Python: Sets technical interview questions and answers with explanations?
IndiaBIX provides you with lots of fully solved Python: Sets technical interview questions and answers with a short answer description. You can download Python: Sets technical interview questions and answers as PDF files or e-books.
How do I answer Python: Sets technical interview questions from various companies?
You can answer all kinds of Python: Sets technical interview questions by practising the given exercises (short answer type). You can also find the frequently asked Python: Sets technical interview questions with answers from various companies, such as TCS, Wipro, Infosys, CTS, IBM, etc.
set
in Python?In Python, a set
is an unordered collection of unique elements. Sets are commonly used for membership testing, removing duplicates from other collections, and performing mathematical set operations. Here's an example program illustrating the use of sets:
# Creating a set
my_set = {1, 2, 3, 4, 5}
# Displaying the set
print("Set:", my_set)
# Adding elements to the set
my_set.add(6)
my_set.update({7, 8})
# Displaying the modified set
print("Modified Set:", my_set)
# Removing elements from the set
my_set.remove(3)
# Displaying the final set
print("Final Set:", my_set)
Output:
Set: {1, 2, 3, 4, 5} Modified Set: {1, 2, 3, 4, 5, 6, 7, 8} Final Set: {1, 2, 4, 5, 6, 7, 8}
In this example, a set named my_set
is created with elements 1 to 5. Elements are added using add()
and update()
methods, and an element is removed using the remove()
method. The resulting set contains unique elements and is unordered.
In Python, an empty set can be created using the set()
constructor. Here's an example program demonstrating how to create an empty set:
# Creating an empty set
empty_set = set()
# Displaying the empty set
print("Empty Set:", empty_set)
Output:
Empty Set: set()
In this example, the set()
constructor is used to create an empty set named empty_set
. The resulting set is then displayed, showing that it is indeed an empty set.
Sets in Python are unordered collections of unique elements. Here are key characteristics of sets along with an example program:
# Creating a set with unique elements
my_set = {1, 2, 3, 4, 5, 1, 2}
# Displaying the set
print("Set:", my_set)
# Adding an element to the set
my_set.add(6)
# Displaying the updated set
print("Updated Set:", my_set)
# Removing an element from the set
my_set.remove(3)
# Displaying the set after removal
print("Set after removal:", my_set)
Output:
Set: {1, 2, 3, 4, 5} Updated Set: {1, 2, 3, 4, 5, 6} Set after removal: {1, 2, 4, 5, 6}
Key characteristics:
- Sets do not allow duplicate elements.
- Sets are unordered, so the elements have no specific order.
- Elements can be added to a set using the
add()
method. - Elements can be removed from a set using the
remove()
method.
You can initialize a set with values in Python using curly braces. Here's an example program:
# Initializing a set with values
my_set = {1, 2, 3, 4, 5}
# Displaying the set
print("Initialized Set:", my_set)
Output:
Initialized Set: {1, 2, 3, 4, 5}
In this example, we've created a set my_set
with the values 1, 2, 3, 4, and 5. Sets automatically eliminate duplicate values, so even if you include duplicates in the initialization, they will be removed.