which of the following statement create a dictionary? and why
a) d={}
b) d={"JHON":40,"PETER":45}
C) d={40:"john",45:"peter"}
d) d=(40:"John", 45:"50")
Answers
Answer:
b) d=("JHON":40,"PETER":45)
d = {}
d = {"JHON":40,"PETER":45}
d = {40:"john",45:"peter"}
are the statements which can be used to create a dictionary.
a) d = {}
b) d = {"JHON":40,"PETER":45}
c) d = {40:"john",45:"peter"}
Explanation
a) d = {}
An empty dictionary will be created.
b) d = {"JHON":40,"PETER":45}
d is a dictionary that has keys JHON and PETER and its values are 40 and 45 respectively.
c) d = {40:"john",45:"peter"}
d is a dictionary that has keys 40 and 45 and its values are john and peter respectively.
d) d=(40:"John", 45:"50")
d is not a dictionary though it has key : value pair but, dictionary should also be enclosed in { }
Extra Information
Dictionary in Python is used to store data values in an unordered collection. For storing data in a dictionary keys are used. It's like a key: value pair in which data is held by unique keys.
In this Day = {1: 'monday', 2: 'tuesday', 3: 'wednesday'}
Day is a dictionary that has keys 1, 2 and 3 and its values are monday, tuesday, and wednesday respectively.
Example
d1 = {"abc":5,"def":6,"ghi":7}
d1 is a dictionary that has keys abc, def, and ghi and its values are 5, 6, and 7 respectively.
Python is created by Guido van Rossum and came into existence in 1991. It is a high-level and general programming language. It is a very lucid programming language as it has a good language construct and object-oriented approach. It is dynamically typed and garbage-collected.