Skip to the content.

3.2 Hw_ipynb_2_

# Function to add two integers
def add_numbers(a, b):
    return a + b

# Function to subtract two integers
def subtract_numbers(a, b):
    return a - b

# Main function
def main():
    # Take input from the user
    num1 = int(input("Enter the first integer: "))
    num2 = int(input("Enter the second integer: "))
    
    # Ask the user for the operation they want to perform
    operation = input("Enter '+' for addition or '-' for subtraction: ")
    
    # Perform the operation based on user input
    if operation == '+':
        result = add_numbers(num1, num2)
        print(f"The result of {num1} + {num2} is: {result}")
    elif operation == '-':
        result = subtract_numbers(num1, num2)
        print(f"The result of {num1} - {num2} is: {result}")
    else:
        print("Invalid operation! Please enter '+' or '-'.")

# Run the main function
if __name__ == "__main__":
    main()

3.2.2 Popcorn Hack

# Create a dictionary
my_dict = {
    "name": "Avika",
    "age": 15,
    "city": "San Diego"
}

# Print the original dictionary
print("Original Dictionary:", my_dict)

# Update an existing item (changing age)
my_dict["age"] = 16

# Print the dictionary after updating
print("Dictionary after update:", my_dict)

# Add a new item (e.g., country)
my_dict["country"] = "USA"

# Print the dictionary after adding new item
print("Dictionary after adding new item:", my_dict)
Original Dictionary: {'name': 'Avika', 'age': 15, 'city': 'San Diego'}
Dictionary after update: {'name': 'Avika', 'age': 16, 'city': 'San Diego'}
Dictionary after adding new item: {'name': 'Avika', 'age': 16, 'city': 'San Diego', 'country': 'USA'}

Homework Hacks

# Create a dictionary with at least 3 keys. Print the dictionary.
types_of_animals = {
    "dog" : "mammal",
    "fish" : "sea-creature",
    "frog" : "amphibian"
}

print(types_of_animals)
{'dog': 'mammal', 'fish': 'sea-creature', 'frog': 'amphibian'}
# Start with this dictionary: person = {"name": "Alice", "age": 30}
# i. Update the age to 31
# ii. print the updated dictionary.

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

person["age"] = "31"

print(person)
{'name': 'Alice', 'age': '31'}