Output of Python Programs | Set 24 (Sets)
Prerequisite: Python-Sets
1. What is the output of the code shown below?
sets = {1, 2, 3, 4, 4} print(sets) |
Options:
- {1, 2, 3}
- {1, 2, 3, 4}
- {1, 2, 3, 4, 4}
- Error
Output:
2. {1, 2, 3, 4}
Explanation : Duplicate values are not allowed in sets. Hence, the output of the code shown above will be a set containing the duplicate value only once. Hence output will be {1, 2, 3, 4}.
2. What is the output of the code shown below?
sets = {3, 4, 5} sets.update([1, 2, 3]) print(sets) |
Options:
- {1, 2, 3, 4, 5}
- {3, 4, 5, 1, 2, 3}
- {1, 2, 3, 3, 4, 5}
- Error
Output:
1. {1, 2, 3, 4, 5}
Explanation: The method update adds elements to a set.
3. What is the output of the code shown below?
set1 = {1, 2, 3} set2 = set1.copy() set2.add(4) print(set1) |
Options:
- {1, 2, 3, 4}
- {1, 2, 3}
- Invalid Syntax
- Error
Output:
2. {1, 2, 3}
Explanation: In the above piece of code, set2 is barely a copy and not an alias of set1. Hence any change made in set2 isn’t reflected in set1.
4. What is the output of the code shown below?
set1 = {1, 2, 3} set2 = set1.add(4) print(set2) |
Options:
- {1, 2, 3, 4}
- {1, 2, 3}
- Invalid Syntax
- None
Output: 4. None
Explanation: add method doesn’t return anything. Hence there will be no output.
5. What is the output of the code shown below?
set1 = {1, 2, 3} set2 = {4, 5, 6} print(len(set1 + set2)) |
Options:
- 3
- 6
- Unexpected
- Error
Output: 4. Error
Explanation: unsupported operand type(s) for +: ‘set’ and ‘set’.
Recommended Posts:
- Output of Python programs | Set 7
- Output of Python programs | Set 8
- Output of Python programs | Set 9 (Dictionary)
- Output of Python Programs | Set 19 (Strings)
- Output of Python Programs | (Dictionary)
- Output of Python Programs | Set 20 (Tuples)
- Output of Python Programs | Set 21 (Bool)
- Output of Python Programs | Set 22 (Loops)
- Output of Python Programs | Set 23 (String in loops)
- Output of Python programs | Set 10 (Exception Handling)
- Output of Python Programs | Set 18 (List and Tuples)
- Output of C programs | Set 32
- Output of C programs | Set 34
- Output of C programs | Set 63
- Output of C programs | Set 43
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



