dictionary and sets FINAL

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

This operator can be used to find the union of two sets.

|

The _________ method returns the value associated with a specified key and removes that key-value pair from the dictionary.

pop()

After the following code executes, what elements will be members of set3? set1 = set(['d', 'e', 'f']) set2 = set(['a', 'b', 'c', 'd', 'e']) set3 = set1.difference(set2)

{'f'}

After the following code executes, what elements will be members of set3? set1 = set(['o', 'p', 's', 'v']) set2 = set(['a', 'p', 'r', 's']) set3 = set1.intersection(set2)

{'p', 's'}

After the following code executes, what elements will be members of set3? set1 = set([1, 2, 3]) set2 = set([2, 3, 4]) set3 = set1.symmetric_difference(set2)

{1, 4}

After the following code executes, what elements will be members of set3? set1 = set([10, 20, 30, 40]) set2 = set([40, 50, 60]) set3 = set1.union(set2)

{10, 20, 30, 40, 50, 60}

You can use __________ to create an empty dictionary.

{}

What will the following code display? dct = {'Monday':1, 'Tuesday':2, 'Wednesday':3} print(dct.get('Monday', 'Not found'))

1

What will the following code display? dct = {'Monday':1, 'Tuesday':2, 'Wednesday':3} print(dct['Tuesday'])

2

What will the following code display? stuff = {'aaa' : 111, 'bbb' : 222, 'ccc' : 333} print(stuff['bbb'])

222

The _________ dictionary method returns the value associated with a specified key. If the key is not found, it returns a default value.

get()

Assume the variable dct references a dictionary. Write an if statement that determines whether the key 'James' exists in the dictionary. If so, display the value that is associated with that key. If the key is not in the dictionary, display a message indicating so.

if 'James' in dct: print(dct['James']) else: print('James is not in the dictionary.')

Assume the variable dct references a dictionary. Write an if statement that determines whether the key 'Jim' exists in the dictionary. If so, delete 'Jim' and its associated value.

if 'Jim' in dct: del dct['Jim'] else print('Jim is not in the dictionary.')

You can use the ____________ operator to determine whether a key exists in a dictionary.

in

The _________ method returns all of a dictionary's keys and their associated values as a sequence of tuples.

items()

The ___________ function returns the number of elements in a dictionary:

len()

The following function returns the number of elements in a set:

len()

You use _____________ to delete an element from a dictionary.

the del statement

You can add a group of elements to a set with this method.

update

After the following statement executes, what elements will be stored in the myset set? myset = set([2, 4, 4, 6, 6, 6, 6])

{ 2, 4, 6}

After the following statement executes, what elements will be stored in the myset set? myset = set('a bb ccc dddd')

{' ', 'a', 'b', 'c', 'd'}

After the following statement executes, what elements will be stored in the myset set? myset = set('Saturn')

{'S','a','t','u','r','n'}

After the following code executes, what elements will be members of set3? set1 = set(['d', 'e', 'f']) set2 = set(['a', 'b', 'c', 'd', 'e']) set3 = set2.difference(set1)

{'a', 'b', 'c'}

After the following statement executes, what elements will be stored in the myset set? myset = set(['a', 'bb', 'ccc', 'dddd'])

{'a', 'bb', 'ccc', 'dddd'}

This operator can be used to find the intersection of two sets.

&

This operator can be used to find the difference of two sets.

-

What values will the following code display? (Don't worry about the order in which they will be displayed.) dct = {1:[0, 1], 2:[2, 3], 3:[4, 5]} for k in dct: print(k)

1, 2, 3

What will the following code display? myset = set('1 2 3') print(len(myset))

4. Don't forget the blank space character

After the following statement executes, what elements will be stored in the myset set? myset = set(10)

An error will be raised. A number is not iterable (i.e. a collection) and cannot be used to create a set. myset = set([10]) would work

What is the number of the first index in a dictionary?

Dictionaries are not indexed by number

A list can be a dictionary key.

False

Sets are created using curly braces {}

False

The elements in a dictionary are stored in ascending order, by the keys of the key-value pairs

False

The following statement creates an empty set: myset = {}

False

The keys in a dictionary must be mutable objects.

False

You can store duplicate elements in a set.

False

I'm order to avoid keyerror exceptions, you can check whether a key is in a dictionary using the _______ operator.

In

What does the get method do if the specified key is not found in dictionary?

It returns a default value.

Which method would you use to get all the elements in a dictionary returned as a list of tuples?

Items

In a dictionary, you use a _______ to locate a specific value

Key

What will the following code display? Ages={ 'aaron: 5, 'Kelly' : 5} Ages['brianna']

KeyError

A dictionary can include the same value several times, but cannot include the same key several times.

True

Dictionaries are not sequences.

True

Sets store their elements in an unordered fashion.

True

The following statement creates an empty dictionary: dict = {}

True

The remove method raises an exception if the specified element is not found in the set.

True

A tuple can be a dictionary key

True. so long as all of the elements are immutable.

What will the following code display? dct = {1:[0, 1], 2:[2, 3], 3:[4, 5]} print(dct[3])

[4, 5]

What values will the following code display? Dct = { 1 : [0,1], 2:[2,3], 3:[4,5]} For k,i in dct.items(): If k == 3: Print(k,i)

[4,5]

This operator can be used to find the symmetric difference of two sets.

^

A(n) _________, sometimes called an ADT, is a logical description of how we view the data and operations that are allowed without regard to how they will be implemented. A dictionary is an example of an ADT.

abstract data type

You can add one element to a set with this method.

add

Write a statement that creates a dictionary containing the following key-value pairs: 'a' : 1 'b' : 2 'c' : 3

dct = { 'a' : 1, 'b' : 2, 'c' : 3}

Which would you use to delete an existing key-value pair from a dictionary?

del

This set method removes an element, but does NOT raise an exception if the element is not found.

discard

Write code to create a set with the following integers as members: 10, 20, 30, and 40.

myset = set([10, 20, 30, 40]) OR mylist = [10, 20, 30, 40] myset = set(mylist)

What will the following code display? dct = {'Monday':1, 'Tuesday':2, 'Wednesday':3} print(dct.get('Friday', 'Not found'))

not found

This set method removes an element and raises an exception if the element is not found.

remove


Kaugnay na mga set ng pag-aaral

Personal Psychology 1 - Unit 1 Lab Questions

View Set

People History 1 Unit test 4 ch13-16

View Set

What was the underlying cause of WW1

View Set

Mosby Chapter 4 - Image Production (Digital Image Acquisition)

View Set