Skip to content
Related Articles

Related Articles

Improve Article
Output of Python program | Set 17
  • Difficulty Level : Medium
  • Last Updated : 22 Jun, 2021

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]
  
print len(numberGames) + sum
  1. Output: 
     
33
  1. Explanation: 
    Tuples can be used for keys into dictionary. The tuples can have mixed length and the order of the items in the tuple is considered when comparing the equality of the keys. 
     
  2. What is the output of the following program? 
     

Python




my_tuple = (1, 2, 3, 4)
my_tuple.append( (5, 6, 7) )
print len(my_tuple)
  1. Output: 
     
Error !
  1. Explanation: 
    Tuples are immutable and don’t have an append method as in case of Lists.Hence an error is thrown in this case. 
     
  2. What is the output of the following program? 
     

Python




t = (1, 2)
print 2 * t
  1. Output: 
     
(1, 2, 1, 2)
  1. Explanation: 
    Asterick Operator (*) operator concatenates tuple.

     

  2. What is the output of the following program? 
     

Python3




d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
print (d1 > d2)
  1. Output: 
     
TypeError
  1. Explanation: 
    The ‘>’ operator is not supported between instances of ‘dict’ and ‘dict’. 
     
  2. What is the output of the following program? 
     

Python




my_tuple = (6, 9, 0, 0)
my_tuple1 = (5, 2, 3, 4)
print my_tuple > my_tuple1
  1. Output: 
     
True
  1. Explanation: 
    Each elements of the tuples are compared one by one and if maximum number of elements are there in tuple1 which are greater of equal to corresponding element of tuple2 then tuple1 is said to be grater than tuple2. 
     

This article is contributed by Avinash Kumar Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :