# Python Examples
# Popcorn Hack in Python
import json
honda_civic = {
"name": "Honda Civic",
"performance": {
"engine": "2.0-liter 4-cylinder",
"horsepower": 158,
"top_speed": "200 km/h",
"acceleration": "0 to 100 km/h in 8.2 seconds",
},
"features": {
"driving_assistance": "Honda Sensing suite with adaptive cruise control and lane-keeping assist",
"exterior": "modern, aerodynamic design",
"interior": "spacious with durable cloth or optional leather seats",
},
"pricing": {
"original_price": "$24,000",
"production_run": "ongoing",
"rarity": "widely available"
}
}
jsonstring = json.dumps(honda_civic)
print(jsonstring)
# Python Homework
import requests # import requests library
url = "https://jsonplaceholder.typicode.com/posts"
response = requests.get(url) #make a get request to url
if response.status_code == 200:
data = response.json() # Convert the response to a JSON object
print("Original Data:", data) # printing original data
data[0]["title"] = "yay" #Modifying title of first post (String)
data[0]["likes"] = 0 # Adding likes field (Integer)
data[0]["rating"] = 3.5 # Adding likes field (float)
data[0]["tags"] = ["fun", "more fun", "most fun"] # Adding tags field (list)
data[0]["is_active"] = True # Adding activity field (Boolean)
print("\nModified Data for Post ID 1:") # Printing modified data
print(data[0])
else:
print("Failed to retrieve data")
// Javascript Examples
// Popcorn Hack in JS
let response1 = true;
console.log("You have plans this weekend: " + response1);
// Output: You have plans this weekend: true
let response2 = false;
console.log("You finished all your homework already: " + response2);
// Output: You finished all your homework already: false
// Homework in JS
// Function to fetch and modify data
async function fetchAndModifyData() {
const url = "https://jsonplaceholder.typicode.com/posts"; // URL for fetching data
try {
const response = await fetch(url); // Fetch data from the URL
if (response.ok) {
const data = await response.json(); // Convert response to JSON
console.log("Original Data:", data); // Print original data
// Modifying the first post
data[0].title = "My Weekend Plans"; // Modifying title of first post (String)
data[0].likes = 42; // Adding likes field (Integer)
data[0].rating = 4.7; // Adding rating field (Float)
data[0].tags = ["weekend", "plans", "relaxation"]; // Adding tags field (Array)
data[0].is_active = true; // Adding is_active field (Boolean)
console.log("\nModified Data for Post ID 1:"); // Print modified data
console.log(data[0]); // Stop after logging modified data
} else {
console.error("Failed to retrieve data.");
}
} catch (error) {
console.error("Error fetching data:", error);
}
}
// Calling the function
fetchAndModifyData();