Jed Rembold
November 10, 2025
What is the printed value of the below code?
A = [
{'name': 'Jill', 'weight':125, 'height':62},
{'name': 'Sam', 'height':68},
{'name': 'Bobby', 'height':72},
]
A.append({'weight':204, 'height':70, 'name':'Jim'})
B = A[1]
B['weight'] = 167
A.append(B)
print([d['weight'] for d in A if 'weight' in d])
[125,70,167]
[125,167,204,167]
[125,204,167]
| Year | Registration Date | Registration Day |
|---|---|---|
| 4 | 11/17 | Monday |
| 3 | 11/18 | Tuesday |
| 2 | 11/20 | Thursday |
| 1 | 11/24 | Following Monday |
If [Caesar] had anything confidential to say, he wrote it in cipher, that is, by so changing the order of the letters of the alphabet, that not a word could be made out.
|
|
Frequently we might want to iterate through a dictionary, checking either its values or its keys
Python supports iteration with the
for statement, which has the form of:
for key in |||dictionary|||:
value = |||dictionary|||[key]
|||code to work with that key and value|||You can also use the .items method to
grab both key and values together:
for key, value in |||dictionary|||.items():
|||code to work with that key and value|||Suppose we had a file of student ids and corresponding grades on a test
We want a simple program where we can enter in a target grade and have it tell us all the students who received that grade
def read_to_dict(filename):
dictionary = {}
with open(filename) as f:
for line in f:
ID, score = line.strip().split(',')
dictionary[ID] = score
return dictionary
def get_students_with_score():
scores = read_to_dict('SampleGrades.txt')
done = False
while not done:
des_grade = input('Enter a letter grade: ')
if des_grade == "":
done = True
else:
for st_id, grade in scores.items():
if grade == des_grade.strip().upper():
print(f"{st_id} got a {grade}")| Method call | Description |
|---|---|
len(|||dict|||) |
Returns the number of key-value pairs in the dictionary |
|||dict|||.get(|||key|||, |||value|||) |
Returns the value associated with the
key in the dictionary. If the key is not
found, returns the specified value, which is
None by default) |
|||dict|||.pop(|||key|||) |
Removes the key-value pair corresponding to
key and returns the associated value. Will
raise an error if the key is not found. |
|||dict|||.clear() |
Removes all key-value pairs from the dictionary, leaving it empty. |
|||dict|||.items() |
Returns an iterable object that cycles through the successive tuples consisting of a key-value pair. |
While most commonly used to indicate mappings, dictionaries have seen increased use of late as structures to store records
Looks surprisingly close to our original template of:
boss = {
'name': 'Scrooge',
'title': 'founder',
'salary': 1000
}Allows easy access of attributes without worrying about ordering
print(boss['name'])It is still using a mutable data-type to represent something that should be immutable though
Enclosed within squiggly brackets
No key-value pairs, just single values separated by commas
digits = { 0, 1, 2, 3, 4, 6, 7, 8, 9 }
squares = { 0, 1, 4, 9 }
primary = { "red", "green", "blue" }Set elements must be immutable
Sets themselves are generally mutable
Can not create an empty set just using
{ }!
set().3 in primesA.union(B)
A | BA.intersection(B)
A & BA.difference(B)
A - BA.symmetric_difference(B)
A ^ B
If we have the following sets from earlier:
| digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } |
| evens = { 0, 2, 4, 6, 8 } |
| odds = { 1, 3, 5, 7, 9 } |
| primes = { 2, 3, 5, 7 } |
| squares = { 0, 1, 4, 9 } |
What is the value of each of the following: