chapter 13 CSE final

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

describe/list several approaches to create a dict:

1. The first approach wraps braces { } around key-value pairs of literals and/or variables: {'Jose': 'A+', 'Gino': 'C-'} creates a dictionary with two keys 'Jose' and 'Gino' that are associated with the grades 'A+' and 'C-', respectively. 2: The second approach uses dictionary comprehension, which evaluates a loop to create a new dictionary, similar to how list comprehension creates a new list. Dictionary comprehension is out of scope for this material. 3. Other approaches use the dict() built-in function, using either keyword arguments to specify the key-value pairs or by specifying a list of tuple-pairs. The following creates equivalent dictionaries:dict(Bobby='805-555-2232', Johnny='951-555-0055')dict([('Bobby', '805-555-2232'), ('Johnny', '951-555-0055')])

Determine the output of each code segment. If the code produces an error, type None. Assume that my_dict has the following entries: my_dict = dict(bananas=1.59, fries=2.39, burger=3.50, sandwich=2.99) my_dict['burger'] = my_dict['sandwich'] val = my_dict.pop('sandwich') print(my_dict['burger'])

2.99 Removing a key only removes that key and any shared reference to a value still exists.

Determine the output of each code segment. If the code produces an error, type None. Assume that my_dict has the following entries: my_dict = dict(bananas=1.59, fries=2.39, burger=3.50, sandwich=2.99) my_dict.update(dict(soda=1.49, burger=3.69)) burger_price = my_dict.get('burger', 0) print(burger_price)

3.69 The update method overwrites old entries. The get method finds the burger entry and ignores the default value of 0, printing 3.69.

T/F Dictionaries can contain up to three levels of nesting.

False: The level of nesting is arbitrary.

T/F The variable my_dict created with the following code contains two keys, 'Bob' and 'A+'. my_dict = dict(name='Bob', grade='A+')

False: The code creates a new dict containing the keys name and grade.

T/F Nested dictionaries are a flexible way to organize data.

True: Nested dictionaries are a simple yet powerful data structure.

T/F Dictionary entries can be modified in-place - a new dictionary does not need to be created every time an element is added, changed, or removed.

True: Dictionaries are mutable, just like lists.

Nested dictionaries also serve as a simple but powerful data structure. A ________ ________ is a method of organizing data in a logical and coherent fashion. Actually, container objects like lists and dicts are already a form of a data structure, but nesting such containers provides a programmer with much more flexibility in the way that the data can be organized.

data structure

dictionary method: my_dict1.update(my_dict2) example code: my_dict = {'Ahmad': 1, 'Jane': 42}my_dict.update({'John': 50})print(my_dict)

description: Merges dictionary my_dict1 with another dictionary my_dict2. Existing entries in my_dict1 are overwritten if the same keys exist in my_dict2. output: {'Ahmad': 1, 'Jane': 42, 'John': 50}

dictionary method: my_dict.get(key, default) example code: my_dict = {'Ahmad': 1, 'Jane': 42}print(my_dict.get('Jane', 'N/A'))print(my_dict.get('Chad', 'N/A'))

description: Reads the value of the key entry from the dictionary. If the key does not exist in the dictionary, then returns default. output: 42 N/A

Dictionary method: my_dict.clear() code examples: my_dict = {'Ahmad': 1, 'Jane': 42}my_dict.clear()print(my_dict)

description: Removes all items from the dictionary. output: {}

dictionary method: my_dict.pop(key, default) example code: my_dict = {'Ahmad': 1, 'Jane': 42}val = my_dict.pop('Ahmad')print(my_dict)

description: Removes and returns the key value from the dictionary. If key does not exist, then default is returned. output: {'Jane': 42}

common dict operations: operations: my_dict[key] = value

description: Adds an entry if the entry does not exist, else modifies the existing entry. example code: my_dict['Jose'] = 'B+'

common dict operations: operation: del my_dict[key]

description: Deletes the key entry from a dict. example code: del my_dict['Jose']

common dict operations: operation: my_dict[key]

description: Indexing operation - retrieves the value associated with key. example code: jose_grade = my_dict['Jose']

common dict operations: operation: key in my_dict

description: Tests for existence of key in my_dict. example code: if 'Jose' in my_dict: # ...

The ______ type implements a dictionary in Python.

dict

the following dict() operations can be used to iterate over a dictionary.

dict.items() - returns a view object that yields (key, value) tuples. dict.keys() - returns a view object that yields dictionary keys. dict.values() - returns a view object that yields dictionary v

A ________ _________ is a function provided by the dictionary type (dict) that operates on a specific dictionary object. Dictionary methods can perform some useful operations, such as adding or removing elements, obtaining all the keys or values in the dictionary, merging dictionaries, etc.

dictionary method

iterating over a dictionary: using- dict.keys()

example code: num_calories = dict(Coke=90, Coke_zero=0, Pepsi=94) for soda in num_calories.keys(): print(soda) output: Coke Coke_zero Pepsi

iterating over a dictionary: using- dict.items()

example code: num_calories = dict(Coke=90, Coke_zero=0, Pepsi=94) for soda, calories in num_calories.items(): print('%s: %d' % (soda, calories)) output: Coke: 90 Coke_zero: 0 Pepsi: 94

iterating over a dictionary: using- dict.values()

example code: num_calories = dict(Coke=90, Coke_zero=0, Pepsi=94) for soda in num_calories.values(): print(soda) output: 90 0 94

Fill in the code, using the dict methods items(), keys(), or values() where appropriate. Change all negative values in my_dict to 0. for key, value in _________ : if value < 0: my_dict[key] =0

my_dict.items() The items method sets key and value to a new key-value pair each iteration of the loop.

Fill in the code, using the dict methods items(), keys(), or values() where appropriate: Print each key in the dictionary my_dict. for key in __________: print(key)

my_dict.keys() or my_dict The for loop iterates over a list containing all the keys in my_dict.

Fill in the code, using the dict methods items(), keys(), or values() where appropriate. Print twice the value of every value in my_dict. for v in ________: print(2 * v)

my_dict.values() The values() method returns a list of every value in a dictionary.

A dictionary may contain one or more ________ dictionaries, in which the dictionary contains another dictionary as a value

nested

T/F A statement such as my_dict.keys()[0] produces an error. Instead, a valid approach is to use the list() built-in function to convert a view object into a list, and then perform the necessary operations.

true

T/F The dict.items() method is particularly useful, as the view object that is returned produces tuples containing the key-value pairs of the dictionary. The key-value pairs can then be unpacked at each iteration, similar to the behavior of enumerate(), providing both the key and the value to the loop body statements without requiring extra code.

true

T/F When a program iterates over a view object, one result is generated for each iteration as needed, instead of generating an entire list containing all of the keys or values; since results are generated as needed, view objects do not support indexing.

true

T/F A benefit of using nested dictionaries is that the code tends to be more readable, especially if the keys are a category like 'Homeworks'. Alternatives like nested lists tend to require more code, consisting of more loops constructs and variables.

true: Dictionaries support arbitrary levels of nesting; Ex: The expression students['Jose']['Homeworks'][2]['Grade'] might be applied to a dictionary that has four levels of nesting.

T/F The expression {'D1': {'D2': 'x'}} is valid

true: The expression creates a nested dictionary with the key 'D1', containing the key D2 with the value 'x'.

The dict type also supports the useful methods items(), keys(), and values() methods, which produce a view object. A ______ object provides read-only access to dictionary keys and values.

view


संबंधित स्टडी सेट्स

CompTIA A+ Certification Practice Test 1 (Exam 220-1001), A+ 220 1001 Review Questions, CompTIA A+ 220-1001, Comptia A+ 220-1001 Objectives, A+ Foundations 220-1001, Messer 220-1001-2, A+ 220-1001, 220-1001 / 220-1002 A+ Acronyms, CompTIA 220-1001 te...

View Set

Chapter 10: Antitubercular Drugs

View Set

Ch. 16 ACCT 427 (general ledger)

View Set