Open In App

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? 






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

Output: 

33

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? 




my_tuple = (1, 2, 3, 4)
my_tuple.append( (5, 6, 7) )
print len(my_tuple)

Output: 

Error !

Explanation: 
Tuples are immutable and don’t have an append method as in case of Lists.Hence an error is thrown in this case. 
 

3.What is the output of the following program? 




t = (1, 2)
print 2 * t

Output: 

(1, 2, 1, 2)

Explanation: 
Asterick Operator (*) operator concatenates tuple.
 

4.What is the output of the following program? 




d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
print (d1 > d2)

Output: 

TypeError

Explanation: 
The ‘>’ operator is not supported between instances of ‘dict’ and ‘dict’. 
 

5.What is the output of the following program? 
 




my_tuple = (6, 9, 0, 0)
my_tuple1 = (5, 2, 3, 4)
print my_tuple > my_tuple1

Output: 

True

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 greater than tuple2. 

 


Article Tags :