Quiz 3- Chapter’s 9-11 Flashcards
define a dictionary
an object that stores a collection of data.
elements are ordered, changeable and DO NOT allow duplicates
KEY VALUE PAIRS are elements
Is a key immutable or mutable
Key’s are immutable
are dictionaries mutable?
yes
How do you retrieve a specific value from a dictionary?
You use the key associated with it
Dictionaries are ordered. What does that mean
that the items have a defined order and that order will not change. Unordered means that the items do not have a defined order and you cannot refer to an item by using an index
dictionary format:
{key1:val1,key2:val2,key3:val3}
What will this do?
variable = {}
What built-in function will accomplish the same thing?
It will create an empty dictionary.
dict() will accomplish the same thing
what will this do?
cars = {‘’brand’’:’’Ford’’,’’model’’:’’Mustang’’,’’year’’:1964}
print(cars)
it will display the dictionary.
What will this do?
dictionary[key] = value
Add the new element to the dictionary. If the key already exists it will change the value associated with it
dictionary[key]
What will this do?
retrieve the value from the dictionary. If key is in the dictionary, otherwise KeyError exception is raised.
What’s a way to test and see if a key is in a dictionary?
using the in and not in operators.
if ‘’model’’ in cars:
print(‘’Yes, ‘model’ is a key’’)
cars = {‘’brand’’:’’Ford’’,’’model’’:’’Mustang’’,’’year’’:1964}
x = cars.values()
print(x)
What is the output?
dict_values([‘Ford’,’Mustang’,1964])
How do you retrieve elements from a dictionary?
cars = {‘’brand’’:’’Ford’’,’’model’’:’’Mustang’’,’’year’’:1964}
x = cars.items()
print(x)
it will display the key value pair
del dictionary[key]
Will do what?
delete the key-value pair in the dictionary. If key is not in dictionary, KeyError exception is raised.
What does the len function do?
Used to obtain the number of elements in a dictionary.
Key must be immutable but values can be any type of object.
One dictionary can include keys of several different immutable tuples.
Values stored in a single dictionary can be of different types
clear method
deletes all the elements in a dictionary, leaving it empty
format: dictionary.clear()
What does the get method do?
gets a value associated with a specified key from dictionary
format: dictionary.get(key,default)
default is returned if key is not found
alternative to [] operator
CANNOT RAISE KEYERROR EXCEPTION
items method:
returns all the dictionaries keys and values
format: dictionary.items()
returned as dictionary view
each element in dictionary view is a tuple which contains a key and its value
USE A FOR LOOP to iterate over the tuples in the sequence
keys method:
returns all the dictionary keys as a sequence
format: dictionary.keys()
pop method:
returns value with specified key and removes that key-value pair from the dictionary
format: dictionary.pop(key,default)
default is returned if key is not found
popitem method:
returns, as a tuple, the key value pair that was last added to the dictionary. The method also removes the pair from the dictionary.
format: dictionary.popitem()
KEY VALUE PAIR RETURNED AS TUPLE
values method:
returns all the dictionary values as a sequence
format: dictionary.values()
use for loop to iterate over values
dictionary comprehension:
an expression that reads a sequence of input elements and uses those input elements to produce a dictionary
what is this an example of:
squares = {item:item**2 for item in numbers}
dictionary comprehension.
The results expressions is item:item**2
the iteration expression is for item in numbers
names = { ‘Jeremy’, ‘Kate’,’Peg’}
variable = {item:len(item) for item in names}
variable
What will the output produce?
It will display the length of each name for each item in the list
{‘Jeremy’:6,’Kate’,:4,’Peg’:3}
dict1 = {‘A’:1,’B‘:2,’C’:3}
dict2 = {k:v for k,v in dict1.items()}
dict 2
What will this do?
produce a copy of the first dictionary
{‘A’:1,’B‘:2,’C’:3}
Can you use an if clause in dictionary comprehension statements?
Yes, to select only certain elements of the input sequence.
set:
an object that stores a collection of data in same ways as mathematical set
-all items must be unique (no duplicates)
-set is unordered
- elements can be of different data types
set function:
used to create a set
for empty set: call set()
for non empty set: call set(argument) where argument is an object that contains iterable elements. (ex: list, string, tuple, can all be argument) but if the argument is a string each character becomes a set element. For set of strings, pass them to the function as a list.
if argument contains duplicates, only one of the duplicates will appear in the list
len function for a set:
returns the number of elements in a set
Are sets mutable or immutable?
mutable
add method (for a set)
adds an element to a set
update method (for a set)
updates a group of elements to a set. (arguments must be a sequence containing iterable elements and each of the elements is added to the set)
remove and discard method both do what? (for a set)
remove the specified item from the set.
The item that should be removed is passed to both methods as an argument.
Behave differently when THE SPECIFIED ITEM IS NOT FOUND IN THE SET
what error does remove method raise when specified item is not found in a set?
KeyError exception
What error does discard method raise when the specified item is not found in the set?
IT DOES NOT RAISE AN EXCEPTION
clear method (for a set):
clears all the elements of the set.
What kind of loop can be used to iterate over each element in a set?
how many times does the loop iterate for each item in the set?
A for loop. Once.
format: for item in set:
What operator can test whether a value is in set or not?
in, not in
Union of 2 sets:
a set that contains all the elements of both sets
How do you find the union of 2 sets:
use the union methodL
format: set1.union(set2)
or
use the | operator
format: set1 | set2
BOTH RETURN A NEW SET WHICH CONTAINS THE UNION OF BOTH SETS
Intersection of 2 sets:
a set that contains only the elements found in both sets
How do you find the intersection of 2 sets?
use the intersection method:
format: set1.intersection(set2)
or
use the & operator
format: set1 & set2
BOTH TECHNIQUES RETURN A NEW SET WHICH CONTAINS THE INTERSECTION OF BOTH SETS