Computer Science

Python Dictionaries (Part 1) | Introduction, Creating Dictionaries, Accessing Elements & Basic Operations | CBSE Class 11 Computer Science

Class 11 · Computer Science

Python Dictionaries (Part 1)

A dictionary is one of the most useful built-in data structures in Python. It stores data in the form of key-value pairs. Unlike lists, dictionaries access data using unique keys instead of index positions. Dictionaries are widely used to represent real-world data such as student records, employee information, product details, and configuration settings.


Learning Objectives

  • Understand the concept of dictionaries.
  • Create dictionaries using different methods.
  • Access values using keys.
  • Add, update, and delete dictionary elements.
  • Traverse dictionaries using loops.

What is a Dictionary?

A dictionary is a collection of key-value pairs enclosed within curly braces { }. Every key is unique and is used to access its corresponding value.

Syntax

dictionary={
    key1:value1,
    key2:value2,
    key3:value3
}

Examples

Student Dictionary

student={
    "roll":101,
    "name":"Rahul",
    "class":"XI",
    "marks":92
}

Product Dictionary

product={
    "id":501,
    "name":"Laptop",
    "price":55000
}

Characteristics of Dictionaries

  • Store data as key-value pairs.
  • Keys must be unique.
  • Values can be duplicated.
  • Dictionaries are mutable.
  • Elements are accessed using keys.
  • A dictionary can contain different data types.

Creating a Dictionary

Using Curly Braces

employee={
    "id":101,
    "name":"Aman",
    "salary":45000
}

Empty Dictionary

data={}

Accessing Dictionary Values

Dictionary values are accessed using their keys.

student={
    "name":"Rahul",
    "marks":95
}

print(student["name"])
print(student["marks"])

Output

Rahul
95

Adding New Elements

A new key-value pair can be added by assigning a value to a new key.

student={
    "name":"Rahul",
    "marks":95
}

student["city"]="Jaipur"

print(student)

Output

{'name':'Rahul','marks':95,'city':'Jaipur'}

Updating Existing Values

student={
    "name":"Rahul",
    "marks":95
}

student["marks"]=98

print(student)

Output

{'name':'Rahul','marks':98}

Deleting Elements

student={
    "name":"Rahul",
    "marks":95,
    "city":"Jaipur"
}

del student["city"]

print(student)

Output

{'name':'Rahul','marks':95}

Membership Operators

The in and not in operators check whether a key exists in a dictionary.

student={
    "name":"Rahul",
    "marks":95
}

print("name" in student)
print("city" in student)

Output

True
False

Traversing a Dictionary

Using for Loop

student={
    "name":"Rahul",
    "marks":95,
    "city":"Jaipur"
}

for key in student:
    print(key,student[key])

Output

name Rahul
marks 95
city Jaipur

Real-Life Applications

  • Student information systems.
  • Employee records.
  • Library management systems.
  • Online shopping applications.
  • Hospital patient records.
  • Configuration settings.

Common Programming Mistakes

1. Accessing a Non-Existing Key

student={
    "name":"Rahul"
}

print(student["city"])

Result: KeyError


2. Using Duplicate Keys

student={
    "name":"Rahul",
    "name":"Aman"
}

Explanation: Only the last value is retained because dictionary keys must be unique.


3. Assuming Dictionaries Use Index Numbers

Dictionary elements are accessed using keys, not numeric indexes.


CBSE Important Programs

  1. Create a dictionary of student details.
  2. Display all key-value pairs.
  3. Add a new key-value pair.
  4. Update the value of an existing key.
  5. Delete a key-value pair.
  6. Check whether a key exists.
  7. Display all student details using a loop.
  8. Create a product dictionary.
  9. Create an employee dictionary.
  10. Display values using keys.

Viva Questions

  1. What is a dictionary?
  2. How is a dictionary different from a list?
  3. Why must dictionary keys be unique?
  4. Can dictionary values be duplicated?
  5. How are dictionary values accessed?
  6. Are dictionaries mutable?
  7. How do you add a new key-value pair?
  8. How do you update an existing value?
  9. How do you delete a dictionary element?
  10. Give two real-life applications of dictionaries.

Exam Tips

  • Remember that dictionaries store data as key-value pairs.
  • Dictionary keys must be unique.
  • Use keys instead of indexes to access values.
  • Dictionaries are mutable.
  • Practice traversing dictionaries using a for loop.

Quick Revision

  • A dictionary stores data as key-value pairs.
  • Keys are unique, but values may be repeated.
  • Dictionaries are mutable.
  • Values are accessed using keys.
  • Use del to remove a key-value pair.
  • Membership operators check for the existence of keys.