Spirit0327 / aarushg_2025

https://spirit0327.github.io/aarushg_2025/
Apache License 2.0
0 stars 0 forks source link

3.2 HW Aarush Gowda #6

Open NeonMist27 opened 1 month ago

NeonMist27 commented 1 month ago

3.2.1 Popcorn create a simple python script to add two integers:

Function to add two integers

def add_integers(a, b): return a + b

Get user input for the two integers

num1 = int(input("Enter the first integer: ")) num2 = int(input("Enter the second integer: "))

Call the function and display the result

result = add_integers(num1, num2) print(f"The sum of {num1} and {num2} is {result}")

3.2.1 POPCORN HACK:

Create a dictionary with some initial key-value pairs

city_population = { "New York": 8419600, "Los Angeles": 3980400, "Chicago": 2716000 }

Update the value for an existing key

city_population["New York"] = 8500000 # Updating New York's population

Add a new key-value pair

city_population["Houston"] = 2328000 # Adding a new city's population

Print the updated dictionary

print(city_population)

POPCORN HACK 3.2.3: import json

Create a dictionary and convert it to JSON format

person = { "name": "John", "age": 30, "city": "New York" }

Convert the dictionary to a JSON string

person_json = json.dumps(person) print("Original JSON:", person_json)

Modify the dictionary (update age and add a new field for profession)

person["age"] = 31 person["profession"] = "Engineer"

Update the changes by converting back to JSON

updated_person_json = json.dumps(person) print("Updated JSON:", updated_person_json)

HOMEWORK:

Create a dictionary with 3 keys

fruit = { "name": "Banana", "color": "Yellow", "weight": 120 # in grams }

Print the dictionary

print("Original dictionary:", fruit)

Start with the given dictionary

person = {"name": "Alice", "age": 30}

Update the age to 31

person["age"] = 31

Print the updated dictionary

print("Updated dictionary:", person)