We read your photo as:
Explain and demonstrate the Python dictionary get() and setdefault() functions.
ScanSolve AI Answer
Step-by-step solution
3 stepsHere are the solutions for your Python lab exercises:
Part i
Python Dictionary Built-in Functions
Part a
get() function
The get() method returns the value for the specified key. If the key does not exist, it returns None by default, or a specified default value if provided. This prevents a KeyError that would occur if you tried to access a non-existent key using square bracket notation (e.g., my_dict['non_existent_key']).
# Demonstration of get()
my_dict = {"apple": 1, "banana": 2, "cherry": 3}
# Get value for an existing key
value_apple = my_dict.get("apple")
print(f"Value for 'apple': {value_apple}")
# Get value for a non-existing key (returns None)
value_grape = my_dict.get("grape")
print(f"Value for 'grape' (no default): {value_grape}")
# Get value for a non-existing key with a default value
value_orange = my_dict.get("orange", 0)
print(f"Value for 'orange' (with default 0): {value_orange}")
# The dictionary remains unchanged
print(f"Dictionary after get(): {my_dict}")
Output:
Value for 'apple': 1
Value for 'grape' (no default): None
Value for 'orange' (with default 0): 0
Dictionary after get(): {'apple': 1, 'banana': 2, 'cherry': 3}
Part b(ii)· 3 steps
Exercise: Student Information Dictionary
setdefault() function
The setdefault() method returns the value of the item with the specified key. If the key does not exist, it inserts the key with the specified value (or None if no value is specified) and returns that value. This is useful for ensuring a key exists with a default value before attempting to use it.
# Demonstration of setdefault()
my_dict = {"apple": 1, "banana": 2, "cherry": 3}
# Get value for an existing key (dictionary remains unchanged)
value_banana = my_dict.setdefault("banana", 100)
print(f"Value for 'banana': {value_banana}")
print(f"Dictionary after setdefault() on existing key: {my_dict}")
# Get value for a non-existing key (key is added with default value)
value_date = my_dict.setdefault("date", 4)
print(f"Value for 'date': {value_date}")
print(f"Dictionary after setdefault() on non-existing key: {my_dict}")
# Get value for another non-existing key (key is added with None if no default is given)
value_elderberry = my_dict.setdefault("elderberry")
print(f"Value for 'elderberry': {value_elderberry}")
print(f"Dictionary after setdefault() with no default value: {my_dict}")
Output:
Value for 'banana': 2
Dictionary after setdefault() on existing key: {'apple': 1, 'banana': 2, 'cherry': 3}
Value for 'date': 4
Dictionary after setdefault() on non-existing key: {'apple': 1, 'banana': 2, 'cherry': 3, 'date': 4}
Value for 'elderberry': None
Dictionary after setdefault() with no default value: {'apple': 1, 'banana': 2, 'cherry': 3, 'date': 4, 'elderberry': None}
Create the initial student dictionary.
student_info = { "name": "Alice", "age": 16, "height": 165, # in cm "grade": "10th" } print(f"Initial student dictionary: {student_info}")Output:
Initial student dictionary: {'name': 'Alice', 'age': 16, 'height': 165, 'grade': '10th'}Add a new key-value pair for "school".
student_info["school"] = "Central High"Print the updated dictionary.
print(f"Updated student dictionary: {student_info}")Output:
Updated student dictionary: {'name': 'Alice', 'age': 16, 'height': 165, 'grade': '10th', 'school': 'Central High'}The final updated dictionary is:
Still stuck on this one?
Ask a follow-up, and the answer starts from this question.