Python Dictionary MCQ

SumptuousDada avatar
SumptuousDada
·
·
Download

Start Quiz

Study Flashcards

10 Questions

What separates all elements in a dictionary?

Colon (:)

In a dictionary, are both keys and values unique?

False

What is an example of a dictionary?

{ }

In the dictionary D = {1 : “One”, 2 : “Two”, 3 : “Three”}, what do 1, 2, 3 represent?

Keys

What type of error is returned by the code when trying to access a non-existent key in a dictionary?

KeyError

Which method in Python is used to delete an element from a dictionary?

b.popitem( )

In Python, can dictionaries contain duplicate values for keys?

No, each key must be unique

What is the result of running d = {1: 'one', 2: 'two', 3: 'three'}; d.clear(); print(d)?

{}

Which data type in Python is considered an unordered collection of items?

Set

If you try to access a key that does not exist in a dictionary in Python, what error will you encounter?

'KeyError'

Study Notes

Dictionary Elements

  • In a dictionary, all elements are separated by commas.
  • Each element is a key-value pair, separated by a colon.

Dictionary Uniqueness

  • In a dictionary, keys must be unique, but values can be duplicated.

Dictionary Example

  • A dictionary is a data type that stores a collection of key-value pairs, such as D = {1 : “One”, 2 : “Two”, 3 : “Three”}.

Dictionary Keys

  • In a dictionary, keys can be any immutable data type, such as integers, strings, or tuples.
  • In the example D = {1 : “One”, 2 : “Two”, 3 : “Three”}, the keys are 1, 2, and 3.

Accessing Non-Existent Keys

  • If you try to access a key that does not exist in a dictionary, a KeyError is raised.

Deleting Dictionary Elements

  • The del statement or the pop() method can be used to delete an element from a dictionary.

Duplicate Values

  • In a dictionary, duplicate values for keys are allowed, but each key must be unique.

Clearing a Dictionary

  • The clear() method is used to remove all elements from a dictionary, leaving it empty.
  • The result of running d = {1: 'one', 2: 'two', 3: 'three'}; d.clear(); print(d) is an empty dictionary {}.

Dictionary Data Type

  • A dictionary is an unordered collection of items in Python.

Accessing Non-Existent Keys Error

  • If you try to access a key that does not exist in a dictionary in Python, a KeyError is raised.

Test your knowledge on Python dictionaries with this multiple-choice quiz. Questions cover topics like keys, mutability, deletion of elements, and key repetition.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Untitled
10 questions

Untitled

SmoothestChalcedony avatar
SmoothestChalcedony
Python Program for Word Analysis
5 questions

Python Program for Word Analysis

SelfDeterminationWashington avatar
SelfDeterminationWashington
Dictionary Methods Quiz
10 questions
Use Quizgecko on...
Browser
Browser