Computer Science
What will be the output of the following code snippet ?
rec = {"Name" : "Python", "Age" : "20", "Addr" : "NJ", "Country" : "USA"}
id1 = id(rec)
del rec
rec = {"Name" : "Python", "Age" : "20", "Addr" : "NJ", "Country" : "USA"}
id2 = id(rec)
print(id1 == id2)
- True
- False
- 1
- Exception
Python
Python Dictionaries
7 Likes
Answer
True
Working
In the given python code snippet, id1 and id2 will point to two different objects in memory as del rec deleted the original dictionary whose id is stored in id1 and created a new dictionary with the same contents storing its id in id2. However, id1 == id2 will compare the contents of the two dictionaries pointed to by id1 and id2. As contents of both the dictionaries are same hence it returns True. If in this code we add another line print(id1 is id2) then this line will print False as id1 and id2 point to two different dictionary objects in memory.
Answered By
3 Likes
Related Questions
What would be the output of following code if
ntpl = ("Hello", "Nita", "How's", "life?") (a, b, c, d) = ntpl print ("a is:", a) print ("b is:", b) print ("c is:", c) print ("d is:", d) ntpl = (a, b, c, d) print(ntpl[0][0]+ntpl[1][1], ntpl[1])Predict the output.
tuple_a = 'a', 'b' tuple_b = ('a', 'b') print (tuple_a == tuple_b)Write the output of the code given below :
my_dict = {"name" : "Aman", "age" : 26} my_dict['age'] = 27 my_dict['address'] = "Delhi" print(my_dict.items())Write a method in python to display the elements of list thrice if it is a number and display the element terminated with '#' if it is not number.
For example, if the content of list is as follows :
List = ['41', 'DROND', 'GIRIRAJ', '13', 'ZARA']The output should be
414141
DROND#
GIRIRAJ#
131313
ZAR#