Chapter 11 Dictionaries Flashcards
A dictionary contains a collection of indices which are called?
Keys
The association of a key and a value is called a? Or sometimes a?
Key-value-pair or sometimes item
In a dictionary, each key “ “ a value
maps to
Dictionaries are always in alphabetical order. True or False
False
Dictionary elements are in no particular order, unlike lists.
” “ is a method that takes a key and default value. If the key appears in the dictionary, the method returns the corresponding value; otherwise, it returns the default value.
.get
To traverse the keys in alphabetical order, you can use
the built-in function
sorted
Define singleton
A list that contains a single element.
How do I tell Python that I want the variable outside of the function to be used inside the function without alterations?
Use the global statement
»> been_called = False
»> def example2():
global been_called
been_called = True
What is wrong with this code?
»> count = 0
»>def counter():
count = count + 1
print(count)
The global variable was not declared inside the function.
Corrected version:
»> def counter():
global count
count += 1
print(count)
True or False:
If a global variable refers to an immutable value, you can modify the value without declaring the variable.
False. The value must be mutable.
Example:
known = {0:0, 1:1}
def example():
known[2] = 1
4 suggestions for debugging large datasets are:
- Scale down the input
- Check summaries and types
- Write self-checks
- Format the output
Page 111-112 of Think Python
A relationship in which each element of one set corresponds to an element of
another set.
mapping
A mapping from keys to their corresponding values
dictionary
An object that appears in a dictionary as the first part of a key-value pair.
key
In a dictionary, another name for a key-value pair
item