]
1
II. Long answer questions: [3x3m=9m]
3
Q4. Consider the following dictionary
statecapital={"Haryana":"Delhi", "Tamil Nadu":"Chennai”, “Rajasthan":"Jaipur"}
write the output of the following statements:
a) print(statescapital.keys())
W
b) del statecapital[“Assam")
print(statecapital)
c) print(len(statecapital))
Answers
Correct question:
Consider the following dictionary stateCapital:
stateCapital = {"Assam" : "Guwahati", "Bihar" : "Patna", "Maharashtra" : "Mumbai", "Rajasthan" : "Jaipur"}
Find the output of the following statements:
a) print(stateCapital.get("Bihar"))
b) print(stateCapital.keys())
c) print(stateCapital.values())
d) print(stateCapital.items())
e) print(len(stateCapital))
f) print("Maharashtra" in stateCapital)
g) print(stateCapital.get("Assam"))
h) del stateCapital["Assam"]
print(stateCapital)
Answers:
a) Patna
b) dict_keys(['Assam', 'Bihar', 'Maharashtra', 'Rajasthan'])
c) dict_values(['Guwahati', 'Patna', 'Mumbai', 'Jaipur'])
d) dict_items([('Assam', 'Guwahati'), ('Bihar', 'Patna'), ('Maharashtra', 'Mumbai'), ('Rajasthan', 'Jaipur')])
e) 4
f) True
g) Guwahati
h) {'Bihar': 'Patna', 'Maharashtra': 'Mumbai', 'Rajasthan': 'Jaipur'}
- get() is a method that retrieves the value of the given key.
- keys() is a method that retrieves the keys in the dictionary.
- values() is a method that retrieves the values in the dictionary.
- items() is a method that retrieves both the keys and the values in the dictionary in a tuple format.
- del is a command that deletes the key-value pair from the dictionary.