NEW
Font size
WorksheetsDay 4: Dictionary Fundamentals
Total questions: 15
Worksheet time: 8mins
Given model_config = {"learning_rate": 0.001, "epochs": 100, "batch_size": 32}, which statement best explains why accessing model_config["epochs"] is considered O(1)?
Because the dictionary sorts keys to speed up searching
Because lists store keys sequentially for fast access
Because Python caches values to avoid computation
Because hash tables enable constant-time key lookup
You need to read a configuration value that might be missing. Which code safely returns a default of 0.001 for learning_rate?
lr = model_config.get("learning_rate"); 0.001
lr = model_config.get["learning_rate"] == 0.001
lr = model_config["learning_rate"] or 0.001
lr = model_config.get("learning_rate", 0.001)
Which method both removes a key and returns its value from a Python dictionary?
del dict[key]
dict.pop(key)
dict.update({key: value})
dict.clear()
Given config = {'epochs': 50, 'lr': 0.01}, which single operation will add momentum: 0.9 without altering existing keys?
config.clear()
del config['momentum']
config.pop('momentum', 0.9)
config.update({'momentum': 0.9})
You have labels = ['cat','dog','fox']. Which comprehension correctly builds {'cat':0,'dog':1,'fox':2}?
{label: i for i, label in zip(range(len(labels)), labels)}
{idx: label for idx, label in enumerate(labels)}
{label: idx for idx, label in enumerate(labels)}
{k: v for k, v in labels}
In the diagram, model_results['experiment_1']['metrics']['accuracy'] uses direct indexing. Which approach safely reads the same nested value if any intermediate key might be missing?
model_results.setdefault('experiment_1',{}).update({'accuracy':0})
model_results.get('experiment_1',{}).get('metrics',{}).get('accuracy',0)
model_results['experiment_1']['metrics'].pop('accuracy',0)
model_results.keys()['experiment_1']['metrics'].get('accuracy',0)
You have default = {'lr': 0.001, 'epochs': 100} and user = {'lr': 0.01, 'momentum': 0.9}. Which statement produces a merged config where user overrides default?
final = default.update(user) returning merged dict
final = {**user, **default} with 'lr' from default
final = {**default, **user} with 'lr' from user
final = default | user with 'lr' unchanged
In the code snippet shown, response.json() returns which Python data type when the remote payload is a JSON object with nested arrays and numbers? (Refer to the request block image.)
A bytes object with encoded JSON
A string containing raw JSON text
A dict with lists and ints/floats
A flat tuple of key–value pairs
Which mapping correctly pairs a JSON structure with its Python equivalent for AI API parsing?
JSON number maps to Python int/float
JSON array maps to Python dict
JSON string maps to Python bytes
JSON object maps to Python list
Which file mode should you use to add a new log entry to the end of an existing file without overwriting its current contents?
"r" to read existing lines safely
"a" to append new content at end
"r+" to read only from the file
"w" to write a fresh empty file
A data pipeline processes a very large text file. Which approach best minimizes memory usage while still reading all records?
Copy file to another location first
Iterate for line in f within a with block
Use f.readlines() to store full list in RAM
Use f.read() to load entire file string
You need to save a Python dict of model metrics to config.json in a human-readable way. Which statement correctly writes the JSON file?
json.loads(metrics, indent=4) after opening file
json.dumps(metrics) and ignore file handle
open("config.json", "r") then json.load(f)
json.dump(metrics, f, indent=4) inside with open("config.json", "w")
In a Python training script that loads hyperparameters from a JSON file, which step best ensures experiment reproducibility across runs?
Increase epochs until accuracy improves
Store results using json.dump with indent
Load config.json with json.load at start
Print the model name to the console
Look at the command line snippet shown: git add . → git commit -m "Add training script" → git push origin main. What is the primary purpose of the git add . step in this workflow?
Push local commits to the remote origin
Merge the main branch into a feature branch
Create a new remote repository on GitHub
Stage all current changes in the repository
You have created a repository named ai-course-week1 and want to upload Day 1–4 notebooks. Which ordered sequence of commands correctly completes the task after editing files locally?
git add . → git push origin main → git commit -m "Add notebooks"
git push origin main → git commit -m "Add notebooks" → git add .
git add . → git commit -m "Add notebooks" → git push origin main
git commit -m "Add notebooks" → git add . → git push origin main
