Computer Science
What will be the output produced by following code ?
d1 = { 5 : "number", "a" : "string", (1, 2): "tuple" }
print("Dictionary contents")
for x in d1.keys(): # Iterate on a key list
print (x, ':' , d1[x], end = ' ')
print (d1[x] * 3)
print ( )
Python Dictionaries
7 Likes
Answer
Output
Dictionary contents
5 : number numbernumbernumber
a : string stringstringstring
(1, 2) : tuple tupletupletuple
Explanation
d1 is a dictionary containing three key-value pairs.x in d1.keys() represents that x will iterate on the keys of d1.
The iterations are summarized below:
| x | d1[x] | d1[x] * 3 |
|---|---|---|
| 5 | number | numbernumbernumber |
| a | string | stringstringstring |
| (1,2) | tuple | tupletupletuple |
Answered By
3 Likes
Related Questions
What is the output produced by the following code :
d1 = {5 : [6, 7, 8], "a" : (1, 2, 3)} print(d1.keys()) print(d1.values())Consider the following code and then answer the questions that follow :
myDict = {'a' : 27, 'b' : 43, 'c' : 25, 'd' : 30} valA ='' for i in myDict : if i > valA : valA = i valB = myDict[i] print(valA) #Line1 print(valB) #Line2 print(30 in myDict) #Line3 myLst = list(myDict.items()) myLst.sort() #Line4 print(myLst[-1]) #Line5- What output does Line 1 produce ?
- What output does Line 2 produce ?
- What output does Line 3 produce ?
- What output does Line 5 produce ?
- What is the return value from the list sort( ) function (Line 4) ?
Predict the output:
d = dict() d['left'] = '<' d['right'] = '>' print('{left} and {right} or {right} and {left}')Predict the output:
d = dict() d['left'] = '<' d['right'] = '>' d['end'] = ' ' print(d['left'] and d['right'] or d['right'] and d['left']) print(d['left'] and d['right'] or d['right'] and d['left'] and d['end']) print((d['left'] and d['right'] or d['right'] and d['left']) and d['end']) print("end")