Output of Python Programs | Set 24 (Sets) Last Updated : 29 Dec, 2017 Summarize Comments Improve Suggest changes Share Like Article Like Report Prerequisite: Python-Sets 1. What is the output of the code shown below? Python3 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? Python3 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? Python3 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? Python3 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? Python3 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'. Comment More infoAdvertise with us A Abhishek Sharma 44 Follow Improve Article Tags : Python Python-Output python-set Practice Tags : pythonpython-set Similar Reads Output of Python program | Set 15 (Loops) Prerequisite - Loops in Python Predict the output of the following Python programs. 1) What is the output of the following program? Python x = ['ab', 'cd'] for i in x: i.upper() print(x) Output:['ab', 'cd']Explanation: The function upper() does not modify a string in place, but it returns a new stri 2 min read Output of Python program | Set 16 (Threads) 1) What is the output of the following program? Python import threading barrier = threading.Barrier(4) class thread(threading.Thread): def __init__(self, thread_ID, thread_name): threading.Thread.__init__(self) self.thread_ID = thread_ID self.thread_name = thread_name def run(self): print("Thre 3 min read Output of Python program | Set 17 Prerequisite - Tuples and Dictionaryin Python Predict the output of the following Python programs. 1.What is the output of the following program? Python numberGames = {} numberGames[(1,2,4)] = 8 numberGames[(4,2,1)] = 10 numberGames[(1,2)] = 12 sum = 0 for k in numberGames: sum += numberGames[k] pri 2 min read Output of Python Programs | Set 18 (List and Tuples) 1) What is the output of the following program? PYTHON L = list('123456') L[0] = L[5] = 0 L[3] = L[-2] print(L) a) [0, '2', '3', '4', '5', 0] b) ['6', '2', '3', '5', '5', '6'] c) ['0', '2', '3', '5', '5', '0'] d) [0, '2', '3', '5', '5', 0] Ans. (d) Explanation: L[0] is '1' and L[5] is '6', both of t 3 min read Output of Python Programs | Set 19 (Strings) 1) What is the output of the following program? PYTHON3 str1 = '{2}, {1} and {0}'.format('a', 'b', 'c') str2 = '{0}{1}{0}'.format('abra', 'cad') print(str1, str2) a) c, b and a abracad0 b) a, b and c abracadabra c) a, b and c abracadcad d) c, b and a abracadabra Ans. (d) Explanation: String function 3 min read Output of Python Programs | Set 20 (Tuples) Prerequisite: Tuples Note: The output of all these programs is tested on Python3 1. What will be the output of the following program? Python3 tuple = (1, 2, 3, 4) tuple.append( (5, 6, 7) ) print(len(tuple)) Options: 125Error Output: 4. ErrorExplanation: In this case an exception will be thrown as tu 2 min read Output of Python Programs | Set 21 (Bool) Prerequisite : Boolean Note: Output of all these programs is tested on Python31. What is the output of the code: Python3 print(bool('False')) print(bool()) False, TrueNone, NoneTrue, TrueTrue, False Output: 4. True, False Explanation: If the argument passed to the bool function does not amount to ze 2 min read Output of Python Programs | Set 22 (Loops) Prerequisite: LoopsNote: Output of all these programs is tested on Python3 1. What is the output of the following?Python mylist = ['geeks', 'forgeeks'] for i in mylist: i.upper() print(mylist) [âGEEKSâ, âFORGEEKSâ].[âgeeksâ, âforgeeksâ].[None, None].Unexpected Output: 2. [âgeeksâ, âforgeeksâ]Explana 2 min read Output of Python Programs | Set 23 (String in loops) Prerequisite: Loops and String Note: Output of all these programs is tested on Python3 1. What is the output of the following? Python3 my_string = "geeksforgeeks" i = "i" while i in my_string: print(i, end =" ") None geeksforgeeks i i i i i i ⦠g e e k s f o r g e e k s 2 min read Output of Python Programs | Set 24 (Sets) Prerequisite: Python-Sets 1. What is the output of the code shown below? Python3 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 2 min read Like