Ahaan

3.2 Homework

Homework 3.2

# Task 1: Create a dictionary with at least 3 keys and print it
# Explanation: This dictionary represents a person with a name, age, and city of residence.
my_dict = {
    "name": "John",          # The person's name
    "age": 25,               # The person's age
    "city": "New York"       # The person's city
}
print("Initial dictionary:", my_dict)  # Printing the dictionary to see its contents

# Task 2: Start with a given dictionary, update the age, and print it
# Explanation: We start with the given dictionary, then update the age and print the updated dictionary.
person = {
    "name": "Alice",        # The person's name
    "age": 30               # The person's current age
}

# Update age to 31
person["age"] = 31         # Modifying the 'age' key's value from 30 to 31

# Print the updated dictionary
print("Updated dictionary:", person)  # Printing the updated dictionary to confirm the change

Initial dictionary: {'name': 'John', 'age': 25, 'city': 'New York'}
Updated dictionary: {'name': 'Alice', 'age': 31}

Popcorn Hack: Simple Python script to count the length of a string

# Get user input
user_string = input("Please enter a string: ")

# Count the length of the string
string_length = len(user_string)

# Print the length of the string
print(f"The length of the entered string is: {string_length}")
The length of the entered string is: 5

Popcorn Hack: create a dictionary, update an item, and add an item

# Creating dictionary 
person = {
    "name": "Ahaan",
    "age": 15,
    "is_student": False
}

person["age"] = 25    # Update item

# Adding an item
person["Thomas"] = {
    "name": "Thomas",   
    "age": 23,
    "is_student": True
} 
print(person)
{'name': 'Ahaan', 'age': 25, 'is_student': False, 'Thomas': {'name': 'Thomas', 'age': 23, 'is_student': True}}
Scroll to top