Python Dictionaries Quiz
44 Questions
4 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What method can be used to remove a specific key from a dictionary and return its value?

  • discard()
  • remove()
  • pop() (correct)
  • delete()
  • The clear() method removes all items from a dictionary but keeps the dictionary itself.

    True

    What method returns a view object displaying all the keys in a dictionary?

    keys()

    To merge two dictionaries, the _______ method is used.

    <p>update()</p> Signup and view all the answers

    Match the following functions with their descriptions:

    <p>keys() = Returns a view of all the keys in the dictionary values() = Returns a view of all the values in the dictionary items() = Returns a view of key-value pairs as tuples get() = Retrieves value corresponding to a given key</p> Signup and view all the answers

    Which of the following correctly prints the length of a dictionary named my_dict?

    <p>print(len(my_dict))</p> Signup and view all the answers

    Dictionaries allow for duplicate keys.

    <p>False</p> Signup and view all the answers

    What will be the result of the statement my_dict.popitem() if the dictionary is empty?

    <p>It will raise a KeyError.</p> Signup and view all the answers

    What is a key characteristic of a Python dictionary?

    <p>Keys must be unique.</p> Signup and view all the answers

    A Python dictionary can have duplicate keys.

    <p>False</p> Signup and view all the answers

    What method can be used to safely access the value of a key without raising an error if the key does not exist?

    <p>get()</p> Signup and view all the answers

    A dictionary in Python is enclosed in ________.

    <p>curly braces</p> Signup and view all the answers

    Match the following dictionary types with their descriptions:

    <p>Empty Dictionary = my_dict = {} Dictionary with Integer Elements = my_dict = {1: 'apple', 2: 'banana'} Nested Dictionary = my_dict = {'person': {'name': 'John'}} Single Element Dictionary = my_dict = {1: 'apple'}</p> Signup and view all the answers

    What will my_dict.get(4) return if my_dict = {1: 'apple', 2: 'banana', 3: 'cherry'}?

    <p>None</p> Signup and view all the answers

    You can modify the value of an existing key in a Python dictionary.

    <p>True</p> Signup and view all the answers

    How would you check if a key exists in a dictionary?

    <p>'key in my_dict'</p> Signup and view all the answers

    What is one key feature of dictionaries compared to lists?

    <p>Dictionaries are mutable and can directly change values.</p> Signup and view all the answers

    Keys in a dictionary can be of any type, including mutable types.

    <p>False</p> Signup and view all the answers

    What is the expected output of counting word occurrences in the sentence 'Hello world, hello again, world!'?

    <p>{'hello': 2, 'world,': 2, 'again,': 1}</p> Signup and view all the answers

    A dictionary can be used as a set of ________ to keep track of the frequency of elements.

    <p>counters</p> Signup and view all the answers

    Match the following dictionary operations with their descriptions:

    <p>counting occurrences = Tracking frequency of elements using keys changing values = Updating existing entry without creating a new object using immutable types = Keys must not change after creation case-insensitive counting = Counting elements without considering letter casing</p> Signup and view all the answers

    In the provided example, what will be the output of the dictionary 'count_dict' after processing the list ['apple', 'banana', 'apple', 'orange', 'banana', 'banana']?

    <p>{'apple': 2, 'banana': 3, 'orange': 1}</p> Signup and view all the answers

    What will the output be if you run the given word counting code on a file containing 'hello world hello'?

    <p>{'hello': 2, 'world': 1}</p> Signup and view all the answers

    Dictionaries can only be used with string keys.

    <p>False</p> Signup and view all the answers

    Write a function that counts how many times each character appears in the string 'hello' and returns the result in a dictionary.

    <p>{'h': 1, 'e': 1, 'l': 2, 'o': 1}</p> Signup and view all the answers

    The provided code only counts uppercase letters in the text file.

    <p>False</p> Signup and view all the answers

    What data structure is used to store word counts in the first problem?

    <p>dictionary</p> Signup and view all the answers

    The expected output for counting letters is {'h': 1, 'e': 1, 'l': __, 'o': 2, 'w': 1, 'r': 1, 'd': 1, 'p': 1, 'y': 1, 't': 1, 'n': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1}.

    <p>3</p> Signup and view all the answers

    Match the file names to their respective tasks:

    <p>textfile.txt = Count word frequencies letters.txt = Count letter frequencies input.txt = Count specific words sample.txt = Count word occurrences using loops</p> Signup and view all the answers

    In the letter counting problem, which characters are to be ignored?

    <p>Spaces and punctuation</p> Signup and view all the answers

    The output for counting specific words in 'input.txt' includes the word 'hello'.

    <p>False</p> Signup and view all the answers

    How many times does the word 'python' appear in the 'input.txt' example provided?

    <p>2</p> Signup and view all the answers

    Which method is used to remove a key from a dictionary and return its value?

    <p>pop() method</p> Signup and view all the answers

    The del statement can be used to remove an item from a dictionary without returning its value.

    <p>True</p> Signup and view all the answers

    What will be the merged dictionary when merging dict1 = {'apple': 1, 'banana': 2} and dict2 = {'cherry': 3, 'date': 4}?

    <p>{'apple': 1, 'banana': 2, 'cherry': 3, 'date': 4}</p> Signup and view all the answers

    To count the frequency of each fruit, you can create a dictionary named __________.

    <p>fruits</p> Signup and view all the answers

    Match the dictionary operations with their descriptions:

    <p>del = Removes a key from a dictionary pop() = Removes a key and returns its value update() = Merges another dictionary into the current one items() = Returns a view object displaying a list of dictionary's key-value tuple pairs</p> Signup and view all the answers

    What will the output of the following code be? my_dict = {'apple': 2, 'banana': 3, 'cherry': 1} for key, value in my_dict.items(): if value > 2: print(f'{key}: {value}')

    <p>banana: 3</p> Signup and view all the answers

    Dictionaries can be used to count word frequency in a text.

    <p>True</p> Signup and view all the answers

    What function in Python is used to extract words from a string using regular expressions?

    <p>re.findall</p> Signup and view all the answers

    In the code word_count[word] = 1, the word_count dictionary is being used to count the __ counts of words.

    <p>frequency</p> Signup and view all the answers

    What will be the output of the following code after executing? my_dict = {'apple': 2, 'banana': 3} my_dict['apple'] = 5 my_dict['orange'] = 1 print(my_dict)

    <p>{'apple': 5, 'banana': 3, 'orange': 1}</p> Signup and view all the answers

    The print(key) statement inside a loop over a dictionary will print the values of the dictionary.

    <p>False</p> Signup and view all the answers

    Given the dictionary {'apple': 2, 'banana': 3, 'cherry': 5}, which key has the maximum value?

    <p>cherry</p> Signup and view all the answers

    Study Notes

    Python Dictionaries

    • A dictionary in Python is an unordered collection of key-value pairs.
    • Keys must be unique and immutable (e.g., strings, numbers, tuples).
    • Values can be of any data type.
    • Dictionaries are mutable, meaning their contents can be changed after creation.
    • Dictionaries are enclosed in curly braces {}, with key-value pairs separated by a colon :.

    Types of Dictionaries

    • Empty Dictionary: An empty dictionary is created with empty curly braces {}.
    • Dictionary with Integer Elements: Dictionaries can have integer keys, paired with other data type values.
    • Dictionary with Mixed Data Types: Dictionaries can store various data types as keys and values.
    • Nested Dictionary: A dictionary can contain another dictionary.
    • Single Element Dictionary: A dictionary with only one key-value pair.

    Accessing Dictionary Elements

    • Using Keys: Elements are accessed by using their corresponding keys inside square brackets [].
    • Using get() Method: Retrieving a value using the get() method avoids errors if the key doesn't exist. By default, it returns None.

    Modifying Dictionaries

    • Adding Items: New key-value pairs can be added by assigning a value to a new key.
    • Changing Values: The value associated with an existing key can be modified by assigning a new value to that key.

    Removing Items

    • Using del: Removes an item by specifying its key.
    • Using pop(): Removes and returns a value associated with a key.
    • Clearing the Dictionary: Removes all items from a dictionary using the clear() method.
    • Deleting the Entire Dictionary: The del keyword can be used to delete the entire dictionary.

    Dictionary Methods

    • keys(): Returns a view object containing all the keys.
    • values(): Returns a view object containing all the values.
    • items(): Returns a view object containing all the key-value pairs as tuples.
    • get(): Returns the value associated with a given key or None if the key doesn't exist.
    • popitem(): Removes and returns an arbitrary key-value pair from the dictionary.
    • update(): Updates a dictionary with elements from another dictionary or an iterable of key-value pairs.

    Iterating Through Dictionaries

    • Iterating through keys: You can iterate through the keys of a dictionary.
    • Iterating through values: You can iterate through the values of a dictionary.
    • Iterating through key-value pairs: You can iterate through both keys and values simultaneously.

    Dictionaries and Files

    • Dictionaries facilitate tasks like processing data from files, counting word occurrences, and organizing data in key-value formats from text files.
    • Example problems involve processing words or characters from text files and storing results in dictionaries for further analysis or manipulation.

    Word Frequency Counter

    • Programs can count the occurrences of each word in text, ignoring case and punctuation.
    • Data structures like dictionaries are used to store word frequencies.

    Adding/Updating Key-Value Pairs

    • Example of creating a dictionary with initial key-value pairs.
    • Example of updating a value associated with an existing key.
    • Example of adding a new key-value pair to a dictionary.

    Finding Maximum Value in a Dictionary

    • Programs can identify the key corresponding to the maximum value.

    Iterating Through a Dictionary

    • Iterating through the keys, values, and key-value pairs of a dictionary separately and collectively.

    Removing Items from a Dictionary

    • Using del to remove items by keys.
    • Using pop() to remove and return the value of an item.
    • Modifying the dictionary after removing items.

    Simple Text Parsing: Capitalized Words

    • Programs extract and count capitalized words in a text string.
    • The extracted words are stored in a dictionary to count and display their frequency.

    Merging Two Dictionaries

    • Combining two dictionaries into a single dictionary.

    Converting List of Tuples to a Dictionary

    • Converting a list of tuples into a dictionary.

    Frequency of Elements in a Dictionary

    • Finding the count of a specific element within a dictionary.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    UNIT3: Python Dictionaries PDF

    Description

    Test your knowledge on Python dictionaries with this quiz. Questions cover methods for manipulating dictionaries, their characteristics, and behavior when accessing values. Ideal for learners looking to strengthen their understanding of Python data structures.

    More Like This

    Quiz de Python
    44 questions

    Quiz de Python

    EnrapturedStarfish9623 avatar
    EnrapturedStarfish9623
    Python Dictionaries Fundamentals
    10 questions
    Use Quizgecko on...
    Browser
    Browser