Chapter 9 Key Terms Flashcards
Dictionary
Object that stores a collection of data.
key
Identifier for a value. They are used to access, modify, or add values.
value
The data associated with or within a key, are typically numbers, lists, strings, and can even be other dictionaries.
Syntax for Creating a Dictionary
dictionary =
{key1:val1, key2:val2}
Syntax for Retrieving a Value from a Dictionary
dictionary[key]
in
Is used to check if a key exists in a dictionary.
not in
Is used to check if a key does not exist in a dictionary.
Syntax for Adding Elements in a Dictionary
dictionary[key] = value
del
Is used to remove an element.
len
Is used to obtain a number of elements.
Data Types in a Dictionary
Data types such as integers, strings, lists, other dictionaries, etc.
Syntax for Creating an Empty Dictionary
empty_dict = {}
clear
Is used to remove all items from a dictionary.
get
Gets a value associated with a specified key.
items
Returns all the dictionary keys and associated values.
keys
Returns all the dictionary keys as a sequence.
pop
Returns value associated with the specified key and removes that key-value pair from the dictionary.
popitem
Removes and returns a random key-value pair from the dictionary.
values
Returns a list of values in the dictionary.
Dictionary Comprehension
An expression that reads a sequence of input elements and uses those input elements to produce a dictionary.
if clause with Dictionary Comprehension
Used to select only certain elements of the input sequence.
Set
An object that stores a collection of data in the same way as a mathematical set.
Syntax for Creating a Set
my_set = {1,2,3}
len and Set
len is used to get the number of elements in a set.
Syntax for Adding and Removing Elements in a Set
my_set.add ()
my_set.remove ()
in, not in, and Sets
in and not in are used to check if an element is present within a set.
union
Is used to combine two sets; and contains elements of both sets.
|
An operator is used to help find the union of two sets.
intersection
A set that contains only the elements in both sets.
difference
A set that contains the elements that appear in the first set but do not appear in the second set
symmetric_difference
A set that contains elements not shared by the two sets.
issubset
Checks if one set is a subset of another.
issuperset
Checks if one set is a superset of another.
set2 <= set1
Checking if set2 is a subset of set1.
set1 >= set2
Checking if set1 is a superset of set2.
Set Comprehension
A concise expression that creates a new set by iterating over the elements of a sequence.
Serializing Objects
Convert the object to a stream of bytes that can easily be stored in a file.
open
Is used to open files for reading and writing.
dump
Is used to serialize an object and save it to a file using the pickle module.
close
Is used to close a file.
load
Is used to deserialize an object from a file using the pickle module.