conley.katherine37
conley.katherine37 13h ago β€’ 0 views

How to Add Key-Value Pairs to a Python Dictionary (High School Tutorial)

Hey everyone! πŸ‘‹ I'm having a bit of trouble with Python dictionaries. How do I add key-value pairs to them? It seems like such a basic thing, but I'm getting stuck. Can someone explain it in a simple way, maybe with a real-world example? Thanks! πŸ™
πŸ’» Computer Science & Technology
πŸͺ„

πŸš€ Can't Find Your Exact Topic?

Let our AI Worksheet Generator create custom study notes, online quizzes, and printable PDFs in seconds. 100% Free!

✨ Generate Custom Content

1 Answers

βœ… Best Answer
User Avatar
miranda.erik36 Jan 6, 2026

πŸ“š Understanding Python Dictionaries

In Python, a dictionary is a collection of key-value pairs. Think of it like a real-world dictionary, where you have a word (the key) and its definition (the value). Each key in a dictionary must be unique, but the values can be anything – numbers, strings, lists, or even other dictionaries!

πŸ“œ A Brief History

Dictionaries were introduced into Python to provide a way to store and retrieve data using meaningful keys rather than just numerical indexes. This was inspired by the concept of associative arrays found in other programming languages. The use of dictionaries greatly enhances code readability and efficiency in many applications.

πŸ”‘ Key Principles

  • πŸ”‘ Keys must be immutable: Keys can be strings, numbers, or tuples, but not lists.
  • πŸ“ Values can be anything: Values can be any Python data type.
  • ⏱️ Dictionaries are mutable: You can add, remove, or modify key-value pairs after the dictionary is created.

βž• Adding Key-Value Pairs

There are a couple of ways to add key-value pairs to a Python dictionary:

  • πŸ’» Using square bracket notation: This is the most common and straightforward method.
  • βš™οΈ Using the update() method: This method is useful for adding multiple key-value pairs at once or merging dictionaries.

πŸ’» Square Bracket Notation

To add a key-value pair using square bracket notation, you simply assign a value to a new key:

my_dict = {}
my_dict['name'] = 'Alice'
my_dict['age'] = 16
print(my_dict)  # Output: {'name': 'Alice', 'age': 16}

βš™οΈ The update() Method

The update() method adds key-value pairs from another dictionary or an iterable of key-value pairs:

my_dict = {}
my_dict.update({'city': 'New York', 'grade': 10})
print(my_dict)  # Output: {'city': 'New York', 'grade': 10}

# Using a list of tuples:
my_dict.update([('subject', 'Math'), ('teacher', 'Mr. Smith')])
print(my_dict) # Output: {'city': 'New York', 'grade': 10, 'subject': 'Math', 'teacher': 'Mr. Smith'}

🍎 Real-World Example: Student Database

Let's create a simple student database using a dictionary:

student = {
    'name': 'Bob',
    'age': 15,
    'grade': 9
}

# Adding more information
student['school'] = 'High School'
student['subjects'] = ['Math', 'Science', 'English']

print(student)
# Output: {'name': 'Bob', 'age': 15, 'grade': 9, 'school': 'High School', 'subjects': ['Math', 'Science', 'English']}

πŸ“ Practice Quiz

  1. ❓ Create an empty dictionary called book and add the following key-value pairs: title (The Hitchhiker's Guide to the Galaxy), author (Douglas Adams), and year (1979).
  2. βž• Add a key-value pair to the book dictionary from the previous question: genre (Science Fiction).
  3. πŸ’» Given the dictionary phone = {'brand': 'Samsung', 'model': 'S21'}, use the update() method to add {'color': 'black', 'storage': '128GB'}.
  4. πŸ’‘ Explain why keys in a Python dictionary must be immutable.
  5. πŸ“š What happens if you try to add a key that already exists in the dictionary? Provide an example.
  6. πŸ”‘ Can you use a list as a key in a Python dictionary? Explain why or why not.
  7. πŸ§ͺ Create a dictionary representing a science experiment with keys like experiment_name, date, and results. Add sample values.

βœ… Conclusion

Adding key-value pairs to Python dictionaries is a fundamental skill. Whether you use square bracket notation or the update() method, understanding how to manipulate dictionaries is crucial for many programming tasks. Keep practicing, and you'll master it in no time!

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! πŸš€