QUESTION IMAGE
Question
what is the output of the following code?
my_set = {1, 2, 3}
my_set.add(2)
print(my_set)
{1, 2, 2, 3}
{1, 2, 3}
error
none of the above
question 11
which method is used to combine two sets?
combine()
merge()
union()
join()
Step1: Understand the properties of a set
In Python, a set is an unordered collection of unique elements.
Step2: Analyze the add() operation
When my_set.add(2) is executed, since the element 2 is already present in the set {1, 2, 3}, adding it again has no effect as sets do not allow duplicate elements.
Step3: Determine the output of print(my_set)
The set remains {1, 2, 3} as the add() operation did not change it due to the uniqueness property of sets.
for Question 11:
Step1: Recall set - related methods
- The
combine()method is not a standard set method in Python. - The
merge()method is not a standard set method in Python. - The
union()method is used to combine two sets. If we have two setsset1andset2,set1.union(set2)(orset1 | set2) returns a new set with all the elements from both sets. - The
join()method is mainly used for strings (e.g.,'-'.join(list_of_strings)to concatenate strings in a list with a delimiter), not for sets.
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
{1, 2, 3}