Jed Rembold
November 12, 2025
Let’s consider a greatly simplified Enigma machine, which only has
one rotor that is not turning. So the signal goes through the rotor then
the reflector and back through the rotor. Given the rotor and reflector
mappings shown to the right, what would the word
python encrypt to?
aicmnzhnktgerfqblszghpmyFrequently 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
digits = { 0, 1, 2, 3, 4, 6, 7, 8, 9 }
squares = { 0, 1, 4, 9 }
primary = { "red", "green", "blue" }
{ }!
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:
Looking at the same sets:
| 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 set resulting from: \[ (\text{primes} \cap \text{evens}) \cup (\text{odds}\cap\text{squares})\]
A == BA <= BA < B{ x for x in range(0,100,2) }| Function | Description |
|---|---|
len(|||set|||) |
Returns the number of elements in a set |
|||elem||| in |||set||| |
Returns True if
|||elem||| is in the set |
|||set|||.copy() |
Creates and returns a shallow copy of the set |
|||set|||.add(|||elem|||) |
Adds the specified |||elem||| to the
set |
|||set|||.remove(|||elem|||) |
Removes the element from the set, raising a
ValueError if it is missing |
|||set|||.discard(|||elem|||) |
Removes the element from the set, doing nothing if it is missing |
| Name | Class | Q1 | Mid | Q3 | Final |
|---|---|---|---|---|---|
| Sally | Python | A | B | B | A |
| Jake | Python | B | B | B | C |
| James | Astro | B | B | A | |
| Lily | Astro | A | A | B | |
| Ben | Python | C | B | B | A |
{
"Python": {
"Sally": ["A", "B", "B", "A"],
"Jake": ["B", "B", "B", "C"],
"Ben": ["C", "B", "B", "A"]
},
"Astro": {
"James": ["B", "B", "A"],
"Lily": ["A", "A", "B"]
}
}
json
json.load(|||file_handle|||)
json.dump(|||data_object|||, |||file_handle|||)
with open(|||filename|||) as |||fhandle|||:
syntaxTo read a JSON file into a variable
data:
import json
with open('file.json') as fh:
data = json.load(fh)To write a variable with complex structure out to a JSON file:
import json
with open('file.json', 'w') as fh:
json.dump(data, fh)[1, 2, 3,] is perfectly
fine in Python, but illegal in JSON