1. Home
  2. Data Science
  3. Python
  4. What is the correct way to create a dictionary in Python?

Added 27 Jan 2024

What is the correct way to create a dictionary in Python?

  1. dict = {"key1": "value1", "key2": "value2"} Correct answer
  2. dict = ("key1": "value1", "key2": "value2")
  3. dict = ["key1": "value1", "key2": "value2"]
  4. dict = "key1": "value1", "key2": "value2"

Correct answer

A. dict = {"key1": "value1", "key2": "value2"}

15 views

MCQSEXAM

Explanation

dict = {"key1": "value1", "key2": "value2"} is correct: dictionaries use curly braces, with each key separated from its value by a colon and pairs separated by commas.

Parentheses create tuples and square brackets create lists, so the same content inside them is a syntax error.

Want more practice? Try the full set of Computer Science MCQs on MCQsExam.com.

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More Python MCQs

Practise all Python MCQs

Similar MCQs