Computer Science
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())
Python
Python Dictionaries
4 Likes
Answer
dict_items([('name', 'Aman'), ('age', 27), ('address', 'Delhi')])
Working
A dictionary my_dict
with two key-value pairs, 'name': 'Aman' and 'age': 26 is initialized. Then updates the value associated with the key 'age' to 27. Then adds a new key-value pair 'address': 'Delhi' to the dictionary my_dict
. The items()
method returns all of the items in the dictionary as a sequence of (key, value) tuples. In this case, it will print [('name', 'Aman'), ('age', 27), ('address', 'Delhi')].
Answered By
3 Likes
Related Questions
Predict the output.
tuple_a = 'a', 'b' tuple_b = ('a', 'b') print (tuple_a == tuple_b)
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
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#Name the function/method required to
(i) check if a string contains only uppercase letters.
(ii) gives the total length of the list.