Python Test 3 Chapter 10

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

dictionary; entries

In practice, a programmer first creates a __________ and then adds _______, perhaps by reading user-input or text from a file.

my_dict[key] jose_grade = my_dict['Jose']

Indexing operation - retrieves the value associated with key.

list( ); list

Instead of indexing, a valid approach is to use the list( ) built-in function to convert view object into a ____, and then perform the necessary operations.

wraps braces { } around key-value pairs of literals and/or variables

What is the first (braces) approach to create a dict.

KeyError

What type of error does set.remove(value) raise if a value is not found in the set?

runtime error

What type of error will using the index operator on a set cause?

"named" attribute

Which is simpler to read: a "named" attribute or a list of tuple referenced via index.

A Car named tuple with fields like Car.price and Car.horsepower

Which would more clearly represent a car object: A Car named tuple with fields like Car.price and Car.horsepower or a list with index positions correlating to some attributes.

dictionary comprehension (out of scope for this material)

evaluates a loop to create a new dictionary, similar to how list comprehension creates a new list.

dict[k]=v

updates the existing entry dict[k], if dict[k] already exists

NO

Do sets allow duplicates?

dict(Bobby= '805-555-2232', Johnny='951-555-0055')

Example of dict( ) with key-value pairs

named tuple

allows the programmer to define a new simple data type that consists of named attributes.

no

Can dictionaries be accessed by indexing?

yes

Can you iterate of a dictionary?

set.clear( )

Clears all elements from the set

dot notation

In a named tuple, a data object's attributes can be accessed using what?

hash value

What is ordering determined by in a dictionary?

no

Once created, can a tuple's elements be changed?

yes

Can a diction contain a list?

dict

A dictionary is represented by the ____ object type

set.add(value)

Add value into the set

my_dict[key] = value my_dict['Jose'] = B+

Adds an entry if the entry does not exist, else modifies the existing entry.

set1.update(set2)

Adds the elements in set2 to set1

dict[k]=v

Adds the new key-values pair k-v, if dict[k[ does not already exist

set( )

An empty set can only be created using what?

yes

Are dictionaries mutable?

yes

Are sets mutable?

index operator

Because the elements of a set are unordered and have no meaningful position in the collection, the _____ ________ is not valid.

del dict[k]

Deletes the entry dict[k]

del my_dict[key] del my_dict['Jose']

Deletes the key entry from a dict.

associative

Dictionaries are typically used in place of lists when what type of relationship exists

yes

Do dictionary elements maintain their insertion order.

yes

Do sets support the len( ) function?

no

Do view objects support iterating?

yes

Does a view object reflect any updates made to a dictionary, even if the dictionary is altered after the view object is created?

yes

Does printing a tuple always display surrounding parentheses?

yes

Does the following state produce an error: my_dict.keys()[0]

key; value

Each ________ in the dictionary is associated with a ______, much like each word in an English language dictionary is associated with a definition.

in-place

Ex. the following evaluation of the statement my_dict.pop('Ahmad'), any other variables that reference the same objects as my_dict will also reflect the removal of 'Ahmad'

dict([('Bobby', '805-55-2232'), ('Johnny', '951-555-0055')])

Example of dict( ) with tuple pairs

len(set)

Find the length (number of elements) of the set

the key is specified in brackets [ ].

How does you access a dictionary entry?

by created a list of comma-separated values, such as 5, 15, 20

How is a new tuple generated?

where the name and attribute names of the named tuple are provided as arguments to the namedtuple constructor. (Note that the fields to include in the named tuple are found in a list, but may also be a single string with space or comma separated values)

How should a named tuple be created once the container is imported?

replace the named tuple with a new object

How would a programmer wishing to edit a named tuple go about it?

KeyError

If no entry with a matching key exists in the dictionary, what type of runtime error occurs when the program is terminated?

yes

Is a tuple a sequence type?

immutable

Like normal tuples, named tuples are ___________.

my_dict1.update(my_dict2)

Merges dictionary my_dict1 with another dictionary my_dict2. Existing entries in my_dict1 are overwritten if the same keys exist in my_dict2.

in-place

Modification of dictionary elements using the dictionary methods is performed ___________.

my_dict.get(key, default)

Reads the value of the key entry from the dictionary. If the key does not exist in the dictionary, then returns default.

set.pop( )

Remove a random element from the set

my_dict.clear()

Removes all items from the dictionary.

my_dict.pop(key, default)

Removes and returns the key value from the dictionary. If key does not exist, then default is returned.

dict.items()

Returns a list of dict's (key, value) tuple pairs

set.union(set_a, set_b, set_c...)

Returns a new set containing all of the unique elements in all sets.

set.intersection(set_a, set_b, set_c...)

Returns a new set containing only the elements in common between set and all provided sets.

set_a.symmetric_difference(set_b)

Returns a set containing only elements that appear in exactly one of set_a or set_b

set.difference(set_a, set_b, set_c...)

Returns a set containing only the elements of set that are not found in any of the provided sets.

1. elements are unordered 2. elements are unique

Sets have the following 2 properties:

set( )

Simply passing a list into _________ will cause any duplicates to be omitted in the created set.

key in my_dict if 'Jose' in my_dict: # . . . .

Tests for existence of key in my_dict.

dict

The ____ type implements a dictionary in Python.

namedtuple

The ________ container must be imported to create a new named tuple.

remove( ); pop( )

These two methods remove an element from the set.

value in set

To check if a specific value exists in a set, a membership test such as _____________ ______ _________ can be used.

parentheses ex. (5, 15, 20)

Typically, tuples are surrounded with _________________.

no elements in the set share the same value

What does it mean that elements are unique?

elements in the set do not have a position or index

What does it mean that elements in a set are unordered?

attempting to create a new entry with a key that already exists in the dictionary replaces the existing entry

What happens when you attempt to create a new entry of a dictionary key that already exists?

chevy_blazer.price

What is an example of dot notation in a named tuple?

tuples

________ are typically used when element position, and not just the relative ordering of elements, is important.

dictionary

a Python container used to describe associative relationships

curly braces{ }

a dict object is created using ________ to surround the key:value pairs that comprise the dictionary contentws

dictionary method

a function provided by the dictionary type (dict) that operates on a specific dictionary object

set()

a set can be created using what function?

key

a term that can be located in a dictionary, such as the word "cat" in the English dictionary.

hash

a transformation of the key into a unique value that allows the interpreter to perform very fast lookup

set()

accepts a sequence-type iterable object (list, tuple, string, etc.) whose elements are inserted into the set.

{'Jose': 'A+', 'Gino': 'C-'}

an example of a dictionary with two keys 'Jose' and 'Gino' that are associated with the grades 'A+' and 'C-', respectively

set

an unordered collection of unique elements.

dictionary

associates (or "maps") keys with values

Tuple

behaves similar to a list but is immutable

dict( )

built-in function, using either keyword arguments to specify the key-value pairs or by specifying a list of tuple-pairs

key

can be any immutable type, such as a number, string, or tuple.

set literal

can be written using curly braces { } with commas separating set elements.

value

can by any type

dictionaries

can contain objects of arbitrary type, even other containers such as lists or nested dictionaries

Dictionaries

contain references to objects as key-value pairs

values

describes some data associated with a key, such as a definition.

len( )

function that returns the number of elements in a set.

set

often used to reduce a list of items that potentially contains duplicates into a collection of unique values.

namedtuple()

only creates the new simple data type and does not create new data objects.

view object

provides read-only access to dictionary keys and values

set.remove(value)

remove the element with given value from the set. Raises KeyError if the value is not found.

dict.keys()

returns a view object that yields dictionary keys

dict.values()

returns a view object that yields dictionary values

sequence type

supports len(), indexing, and other sequence type functions

del

this keyword is used to remove entries from a dictionary

dict.items( )

this method is particularly useful, ass the view object that is returned produces tuples containing the key-value pairs of the dictionary.

add( )

this method places a new element into the set if the set does not contain an element with the provided value.


Set pelajaran terkait

TEAS English, Teas Test - Reading Section PART 2, TEAS Reading

View Set

2.1.8 Practice Questions Protection and Safety

View Set

Chapter 13 - Structure and Shape

View Set

Financial Management Chapters 3 & 4

View Set

Series 79: CH 9-- Syndication of Securities Offerings

View Set

Exam 1- Chapter 5- IS5610 Cybersecurity

View Set

Chapter 25 Normal Newborn: Needs, Care, and Feeding

View Set