Level 73 Level 75
Level 74

[Quiz] Dictionary


95 words 0 ignored

Ready to learn       Ready to review

Ignore words

Check the boxes below to ignore/unignore words, then click save at the bottom. Ignored words will never appear in any learning session.

All None

Ignore?
dictionary
A mapping from a set of keys to their corresponding values.
key-value pair
The representation of the mapping from a key to a value.
value
One of the basic units of data, like a number or string, that a program manipulates
implementation
A way of performing a computation.
hash function
A function used by a hashtable to compute the location for a key.
hashable
An object is hashable if it is immutable (ints, floats, tuples, strings, etc) or user-defined classes that define a __hash__ method.
lookup
A dictionary operation that takes a key and finds the corresponding value.
reverse lookup
A dictionary operation that takes a value and finds one or more keys that map to
singleton
A list (or other sequence) with a single element.
call graph
A diagram that shows every frame created during the execution of a program, with an
histogram
A set of counters.
memo
A computed value stored to avoid unnecessary future computation.
flag
A boolean variable used to indicate whether a condition is true.
declaration
A statement like global that tells the interpreter something about a variable
global variable
A variable defined outside a function. Global variables can be accessed from any
>>>
Python shell prompt
abs
Return absolute Value of a number
argument
Extra information which the computer uses to perform commands
argparse
Parser for Command-Line Options, arguments and sub-commands
assert
Used while debugging to check for conditions that ought to apply
Assignment
Giving a value to a variable
Block
one or more lines of code grouped together with the same minimum amount of indentation
break
Used to exit a for loop or a while loop
class
Template for creating user-defined Objects
compiler
A program that converts an entire program into machine code before the program is executed.
continue
Terminate the smallest enclosing loop and immediately continue with the next iteration of the loop.
conditional statement
if x == y
debugging
The process of finding and removing any of the three kinds of programming errors.
def
Defines a function or method
Lists
beatles = ["John","Paul","George","Ringo","me"]
index
str.index(sub[, start[, end]])
.sort()
takes a list and modifies it so that it is in numerical or alphabetical order
del()
allows you to remove an item from a dictionary, usually an entire entry
.remove(item)
allows you to remove an item from a dictionary, usually part of an entry
.index()
used when you need to find where an item is in a list
.insert()
used when you need to add and item into a list, not just at add it to the end
.append()
used to add items to the end of a list
By key (as opposed to by index)
How are dictionary items accessed (as opposed to list items)
Similarities between lists and dictionaries
Variable-length (can be added to and subtracted from), Heterogenous (can contain any object type), Nestable (can contain any number of lists or other dictionaries as values, both store values by reference. Also a value…
Major differences between lists and dictionaries
Dictionaries are not ordered, and don't support the same sequence operations that work on strings and lists.
Syntax of dictionary declarations
Series of key:value pairings, separated by commas, enclosed in braces --> {a:2, 'age':3, 'peter':[1, 2, 3]}
D.clear()
Removes all items from D
D.copy()
Returns shallow copy of D
D.fromkeys(S[,v])
Returns new dict with keys from S and values equal to v, which defaults to None
D.get(k[,d[)
Returns D[k] if k in D, else d, which defaults to none
D.has_key(k)
Returns True if D has a key k, else False
D.items()
List of D's key:value pairs, as 2-tuples
D.iteritems()
an iterator over the key:value items of D
D.iterkeys()
an iterator over the keys of D
D.itervalues()
an iterator over the values of D
D.keys()
Returns list of D's keys
D.pop(k[,d])
Removes specified key and returns the corresponding value. If key is not found, d is returns if given, otherwise KeyError is raised
D.popitem()
Removes and returns some key:value pair as a 2-tuple, raises KeyError if D is empty
D.setdefault(key, default)
Fetch by key, if absent set default
D.update(D2)
Merges the key:value pairings of two dictionaries, blindly overwriting value of the same key if there's a clash
D.values()
Returns list of D's values
D.viewitems()
Returns a set-like object providing a view on D's items
D.viewkeys()
Returns a set-like object providing as view on D's keys
D.viewvalues()
Returns an object providing a view on D's values
dict(name='Bob', age=40)
A dictionary with items 'name':'Bob' and 'age':40
dict.fromkeys(['a', 'b'], 0)
A dictionary with items 'a':0 and 'b':0
dict(zip(['a', 'b', 'c'], [1, 2, 3]))
A dictionary with items 'a':1, 'b':2, 'c':3, but not necessarily in that order
A dictionary, D, with items 1:1, 2:4, 3:9, 4:16
D = {x: x**2 for x in [1, 2, 3, 4]}
examples of immutable data
6, True, 'happy' - The literal cannot be changed
Dictionary rules
key must be immutable and unique.
tuple
(pronounced TUH-pul or TOO-pul) A built-in Python datatype, which is an immutable ordered sequence of values. Note that only the sequence itself is immutable. If it contains a mutable value such as a dictionary, …
Dictionary example
d = { 'joe' : 18, 'lisa': 20 , 'jerry' : 18 }
dictionaryName.keys()
How to access all of the keys? values?
Using .pop
How do you remove things from a dictionary?
comma separated values files
What does csv stand for?
d['key']
How to access items within a dictionary
Number of key value pairs
What will then len function return from a dictionary?
k in d or k not in d
Will check to see if a value is within a dictionary
What will d.get(key) return?
Will find value at that key
Difference between d[key] and d.get(key)
d[key] returns an error if not found
None
value that means nothing there null in other languages
What does d.pop(key) return?
Removes the key value pair from the dictionary.
immutable
An object with fixed value. Immutable objects include numbers, strings and tuples. Such an object cannot be altered. A new object has to be created if a different value has to be stored. They …
f(a, b=6)
Setting default cases within function
def functionName (a, b, * args)
How to make function that can accept as many parameters as needed?
def f(a,b,*lucky, c=5):
keyword only/positional parameter category example
Variable length keywords
def f(1,2,3,c=6,x=12,y=13,joe=7)
Variable length example
def g(*args, **kwargs):
Key and value.
An element in a dictionary has two parts. What are they called?
The string 'start' is the key, and the integer 1472 is the value.
Suppose 'start' : 1472 is an element in a dictionary. What is the key? What is the value?
employee['id'] = 54321
It stores the key-value pair 'id' : 54321 in the employee dictionary.
print(stuff[3])
ccc
How can you determine whether a key-value pair exists in a dictionary?
You can use the in operator to test for a specific key.
del inventory[654]
It deletes the element that has the key 654.
print(len(stuff))
3
print(k)
1
What is the difference between the dictionary methods pop and popitem?
The pop method accepts a key as an argument, returns the value that is associated with that key, and removes that key-value pair from the dictionary. The popitem method returns a randomly selected key-value p…
.
any character except newline
What does the keys method return?
It returns all the keys in a dictionary as a sequence of tuples.
What does the values method return?
It returns all the values in the dictionary as a sequence of tuples