Which of the following is the correct form of using dict()?
(a) >>>dict([(‘a’, 45),(‘b’,76)])
(b) dict({‘a’: 45,‘b’:76})
(c) dict(‘a’= 45,‘b’=76)
(d) All of these
Answer:
-lock-
The dict() function can take different types of arguments to create a dictionary object in Python.
The dict() function can take one of the following types of arguments:
- A sequence of tuples , where each tuple contains a key and a value. For example, dict([(‘a’, 45), (‘b’, 76)]) means that the key ‘a’ has the value 45 and the key ‘b’ has the value 76. The function returns a dictionary object with these key-value pairs.
- A mapping object , such as another dictionary , where each key-value pair is copied to the new dictionary object. For example, dict({‘a’: 45, ‘b’: 76}) means that the key ‘a’ has the value 45 and the key ‘b’ has the value 76. The function returns a dictionary object with these key-value pairs.
- Keyword arguments , where each argument is a key followed by an equal sign and a value . For example, dict(‘a’= 45,‘b’=76) means that the key ‘a’ has the value 45 and the key ‘b’ has the value 76. The function returns a dictionary object with these key-value pairs.
Therefore , all of these forms are valid ways of using the dict() function in Python.
So, the correct answer is (d) All of these.
-endlock-