Skip to the content.

3.2 Hacks and Homework

Completing the hacks and homework for lesson 3.2


3.2


Hack 1


Python


import json

# Definining JSON object of Hellcat
hellcat_dict = {
    "make": "Dodge",
    "model": "Charger Hellcat",
    "year": 2023,
    "specs": {
        "engine": "6.2L Supercharged V8",
        "fuel_type": "Gasoline",
        "horsepower": 707,
        "torque": 650,
        "transmission": "Automatic"
    },
    "features": [
        "Bluetooth",
        "Backup Camera"
    ],
    "price": 69995.50,
    "is_new": True
}

# Dumping JSON data to a string and outputting
json_string = json.dumps(hellcat_dict)
print(json_string)
{"make": "Dodge", "model": "Charger Hellcat", "year": 2023, "specs": {"engine": "6.2L Supercharged V8", "fuel_type": "Gasoline", "horsepower": 707, "torque": 650, "transmission": "Automatic"}, "features": ["Bluetooth", "Backup Camera"], "price": 69995.5, "is_new": true}


JavaScript


%%javascript

// Defining booleans with values of true and false
let yes = true;
let no = false;

// You WILL buy a brand new car!
console.log("You will buy a brand new car..." + yes);

// You WON'T get stinky socks for Christmas!
console.log("You will get stinky socks for Christmas..." + no);
<IPython.core.display.Javascript object>


Homework


Python


import json

# Definining JSON object of Hellcat
hellcat_dict = {
    "make": "Dodge",
    "model": "Charger Hellcat",
    "year": 2023,
    "specs": {
        "engine": "6.2L Supercharged V8",
        "fuel_type": "Gasoline",
        "horsepower": 707,
        "torque": 650,
        "transmission": "Automatic"
    },
    "features": [
        "Bluetooth",
        "Backup Camera"
    ],
    "price": 69995.50,
    "is_new": True
}

# Printing the original dictionary
print(hellcat_dict)

# Increasing price
hellcat_dict["price"] = 9999999.99
print(hellcat_dict["price"])

# Making it not new anymore
hellcat_dict["is_new"] = False
print(hellcat_dict["is_new"])

# Adding a feature
hellcat_dict["features"] = ["Bluetooth", "Backup Camera", "Blind Spot Monitoring"]
print(hellcat_dict["features"])

# Newer model (setting to 2020)
hellcat_dict["year"] = 2020
print(hellcat_dict["year"])

# Changing the model
hellcat_dict["model"] = "Challenger Hellcat"
print(hellcat_dict["model"])

# Printing the final dictionary as a JSON string
print(json.dumps(hellcat_dict, indent=4))
{'make': 'Dodge', 'model': 'Charger Hellcat', 'year': 2023, 'specs': {'engine': '6.2L Supercharged V8', 'fuel_type': 'Gasoline', 'horsepower': 707, 'torque': 650, 'transmission': 'Automatic'}, 'features': ['Bluetooth', 'Backup Camera'], 'price': 69995.5, 'is_new': True}
9999999.99
False
['Bluetooth', 'Backup Camera', 'Blind Spot Monitoring']
2020
Challenger Hellcat
{
    "make": "Dodge",
    "model": "Challenger Hellcat",
    "year": 2020,
    "specs": {
        "engine": "6.2L Supercharged V8",
        "fuel_type": "Gasoline",
        "horsepower": 707,
        "torque": 650,
        "transmission": "Automatic"
    },
    "features": [
        "Bluetooth",
        "Backup Camera",
        "Blind Spot Monitoring"
    ],
    "price": 9999999.99,
    "is_new": false
}


JavaScript


%%javascript

// Definining JSON object of Hellcat
let hellcat_json = 
{
  "make": "Dodge",
  "model": "Charger Hellcat",
  "year": 2023,
  "specs": {
    "engine": "6.2L Supercharged V8",
    "fuel_type": "Gasoline",
    "horsepower": 707,
    "torque": 650,
    "transmission": "Automatic"
  },
  "features": [
    "Bluetooth",
    "Backup Camera"
  ],
  "price": 69995.50,
  "is_new": true,
};


console.log(hellcat_json);

// Increasing price
hellcat_json.price = 9999999.99;
console.log(hellcat_json.price);

// Making it not new anymore
hellcat_json.is_new = false;
console.log(hellcat_json.false);

// Adding a feature
hellcat_json.features = ["Bluetooth", "Backup Camera", "Blind Spot Monitoring"];
console.log(hellcat_json.features);

// Newer model
hellcat_json.year = 2020;
console.log(hellcat_json.year);

// Changing the model
hellcat_json.model ="Challenger Hellcat";
console.log(hellcat_json.model);

// Output the final object as a JSON string
console.log(JSON.stringify(hellcat_json));
<IPython.core.display.Javascript object>