PDF Notes: python-basics-a-practical-introduction-to-python-3-1775093328-9781775093329_compress

    Master this deck with 20 terms through effective study methods.

    Generated from uploaded pdf

    Created by @usamamohsin

    What is a dictionary in Python and how is it structured?

    A dictionary in Python is a built-in data type that stores data in key-value pairs. Each key is unique and is used to access its corresponding value. Dictionaries are defined using curly braces {} with keys and values separated by a colon, e.g., {'key': 'value'}.

    How do you create an empty dictionary in Python?

    An empty dictionary can be created in Python using either the dict() constructor or by using curly braces. For example, you can create an empty dictionary with captains = {} or captains = dict().

    What method can be used to check if a key exists in a dictionary?

    You can use the 'in' keyword to check if a key exists in a dictionary. For example, 'if key in dictionary:' will return True if the key is present.

    How can you add items to a dictionary in Python?

    Items can be added to a dictionary using square bracket notation. For example, dictionary['new_key'] = 'new_value' will add a new key-value pair to the dictionary.

    What happens if you try to access a key that does not exist in a dictionary?

    If you try to access a key that does not exist in a dictionary, Python will raise a KeyError. To avoid this, you can use the get() method, which returns None or a specified default value if the key is not found.

    How can you iterate over a dictionary to display its contents?

    You can iterate over a dictionary using a for loop. For example, 'for key, value in dictionary.items():' allows you to access both keys and values during the iteration.

    What is the purpose of the del statement in relation to dictionaries?

    The del statement is used to delete a key-value pair from a dictionary. For example, 'del dictionary[key]' will remove the specified key and its associated value from the dictionary.

    How can you create a dictionary using the dict() function with initial values?

    You can create a dictionary with initial values using the dict() function by passing keyword arguments. For example, dict(Enterprise='Picard', Voyager='Janeway') creates a dictionary with two key-value pairs.

    What is the difference between a list and a dictionary in Python?

    A list is an ordered collection of items that can be accessed by their index, while a dictionary is an unordered collection of key-value pairs accessed by keys. Lists allow duplicate values, whereas dictionary keys must be unique.

    What are nested dictionaries and how are they useful?

    Nested dictionaries are dictionaries that contain other dictionaries as values. They are useful for modeling complex data structures, such as representing hierarchical data or structured data like spreadsheets.

    What is the significance of the KeyError in Python dictionaries?

    KeyError is an exception raised when trying to access a key that does not exist in a dictionary. It indicates that the requested key is not found, prompting the programmer to handle the situation appropriately.

    How can you safely retrieve a value from a dictionary without causing an error?

    You can safely retrieve a value from a dictionary using the get() method, which allows you to specify a default value to return if the key does not exist. For example, dictionary.get('key', 'default_value') will return 'default_value' if 'key' is not found.

    What is the output of iterating over a dictionary's keys?

    When you iterate over a dictionary's keys using 'for key in dictionary:', you will get each key in the dictionary one at a time, allowing you to access the corresponding values using the keys.

    How can you merge two dictionaries in Python?

    You can merge two dictionaries using the update() method, which adds key-value pairs from one dictionary to another. Alternatively, in Python 3.9 and later, you can use the merge operator '|', e.g., merged_dict = dict1 | dict2.

    What is the role of the items() method in a dictionary?

    The items() method returns a view object that displays a list of a dictionary's key-value tuple pairs. This allows for easy iteration over both keys and values in a dictionary.

    What is the difference between shallow copy and deep copy in relation to dictionaries?

    A shallow copy of a dictionary creates a new dictionary object but does not create copies of nested objects; it only copies references. A deep copy creates a new dictionary and recursively copies all objects found in the original dictionary.

    How can you remove all items from a dictionary?

    You can remove all items from a dictionary using the clear() method, which empties the dictionary but keeps the dictionary object itself intact.

    What is the purpose of the setdefault() method in dictionaries?

    The setdefault() method returns the value of a specified key if it exists; if not, it inserts the key with a specified default value. This is useful for initializing keys with default values.

    How do you find the length of a dictionary?

    You can find the length of a dictionary using the len() function, which returns the number of key-value pairs in the dictionary.

    What are some common use cases for dictionaries in Python programming?

    Common use cases for dictionaries include storing configuration settings, counting occurrences of items, representing structured data, and implementing lookup tables for fast data retrieval.