Fall 2024 - P2
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.2 Lesson Period 2 - Dictionaries Data Abstraction
Student led teaching on Abstraction. Teaching how various data types can use abstraction for copmutational efficiency.
Dictionaries - 3.2.6
mydictionary = {
#key : value
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
# Dictionaries are abstractions because they allow for efficient storage and retrieval of key-value pairs without exposing the underlying workings
print("\nThe whole dictionary is:")
print(mydictionary)
print("\nThe value of the key 'brand' is:")
#dictionaryname[key]
print(mydictionary["brand"])
print("\nThe value of the key 'model' is:")
print(mydictionary["model"])
print("\nThe value of the key 'year' is:")
print(mydictionary["year"])
The whole dictionary is:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
The value of the key 'brand' is:
Ford
The value of the key 'model' is:
Mustang
The value of the key 'year' is:
1964
Javascript Version
%%js
var mydictionary = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
};
json_object = JSON.stringify(mydictionary, null, 2)
console.log(json_object)
console.log(mydictionary["brand"])
console.log(mydictionary["model"])
console.log(mydictionary["year"])
<IPython.core.display.Javascript object>
Serialization and Deserialization
The process of serialization is to “convert an object’s state into a format that can be transported” Pretty simply: converting object types from one another
The process we will show is python dictionaries to JSON strings!
import json #importing the json library
#let's use our dictionary from before!
jsondict = json.dumps(mydictionary) #converts the dictionary to a json string
print("\nOur dictionary as a json string:", jsondict)
Our dictionary as a json string: {"brand": "Ford", "model": "Mustang", "year": 1964}