Sets: What Does This Print
What does this print? · Intermediate · about 8 min
Read each short program and type exactly what it prints, with each printed line on its own line. If a program stops with an error, type the error's name, like TypeError.
Example
print(3 in {1, 2, 3})
prints
TrueProgram 1
s = {3, 1, 3, 2, 1} print(len(s))
Program 2
s = {1, 2, 3} s.add(2) s.add(4) print(sorted(s))
Program 3
a = {1, 2, 3} b = {2, 3, 4} print(sorted(a & b), sorted(a | b))
Program 4
a = {1, 2, 3} b = {2, 3, 4} print(sorted(a - b), sorted(b - a))
Program 5
s = {} print(type(s))
Program 6
s = {1, 2} s.discard(5) s.remove(5) print(len(s))
Program 7
print({1, 2} <= {1, 2, 3}, {1, 2} == {2, 1})
Program 8
s = set("hello") print(len(s), "l" in s)