What will be the output of this code?
nums = (1, 2, 3)
output = {*nums}
print(output)
Options:
{1, 2, 3}
[1, 2, 3]
(1, 2, 3)
TypeError
Step 1: Create a tuple
nums = (1, 2, 3)
Step 2: Unpack the tuple into a set
output = {*nums}
- The unpacking operator *nums extracts from the tuple.
- The {} braces convert these elements into a set, which removes duplicates (if any) and stores the elements in an unordered manner.
Step 3: Print the set
print(output)
{1, 2, 3}
Key Points to Remember:
- The unpacking operator * extracts the elements of the tuple (or other iterable).
- The {} braces create a set, which is an unordered collection of unique elements.
- Sets automatically eliminate duplicates, but there are no duplicates in this example.
Output:
{1, 2, 3}
0 Comments:
Post a Comment