Computer Science

Python Dictionary Programs (Part 3) | Nested Dictionaries, Searching, Traversing & CBSE Practical Programs | CBSE Class 11 Computer Science

Class 11 · Computer Science

Python Dictionary Programs (Part 3)

After learning dictionary concepts and methods, the next step is to apply them in practical programming. This article contains important CBSE practical programs based on dictionaries that help develop problem-solving skills.


Program 1: Display All Keys and Values

student={
    "Roll":101,
    "Name":"Rahul",
    "Marks":95
}

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

Program 2: Search for a Key

student={
    "Name":"Rahul",
    "Marks":95,
    "City":"Jaipur"
}

key=input("Enter key: ")

if key in student:
    print("Value =",student[key])
else:
    print("Key Not Found")

Program 3: Count Total Key-Value Pairs

student={
    "Roll":101,
    "Name":"Rahul",
    "Marks":95,
    "City":"Jaipur"
}

print("Total =",len(student))

Program 4: Display Only Keys

student={
    "Roll":101,
    "Name":"Rahul",
    "Marks":95
}

for key in student.keys():
    print(key)

Program 5: Display Only Values

student={
    "Roll":101,
    "Name":"Rahul",
    "Marks":95
}

for value in student.values():
    print(value)

Program 6: Display Key-Value Pairs

student={
    "Roll":101,
    "Name":"Rahul",
    "Marks":95
}

for key,value in student.items():
    print(key,":",value)

Program 7: Find Highest Marks

marks={
    "Aman":85,
    "Riya":91,
    "Rahul":88,
    "Neha":95
}

highest=max(marks.values())

print("Highest Marks =",highest)

Program 8: Find Student with Highest Marks

marks={
    "Aman":85,
    "Riya":91,
    "Rahul":88,
    "Neha":95
}

highest=max(marks.values())

for name in marks:
    if marks[name]==highest:
        print(name)

Program 9: Nested Dictionary

students={
    101:{
        "Name":"Rahul",
        "Marks":95
    },
    102:{
        "Name":"Riya",
        "Marks":91
    }
}

print(students[101]["Name"])
print(students[102]["Marks"])

Program 10: Traverse Nested Dictionary

students={
    101:{
        "Name":"Rahul",
        "Marks":95
    },
    102:{
        "Name":"Riya",
        "Marks":91
    }
}

for roll,data in students.items():
    print(roll,data["Name"],data["Marks"])

Program 11: Count Frequency of Characters

text=input("Enter a string: ")

frequency={}

for ch in text:
    frequency[ch]=frequency.get(ch,0)+1

print(frequency)

Program 12: Count Frequency of Words

sentence=input("Enter sentence: ")

words=sentence.split()

frequency={}

for word in words:
    frequency[word]=frequency.get(word,0)+1

print(frequency)

Program 13: Student Result Dictionary

student={
    "English":85,
    "Physics":90,
    "Chemistry":88,
    "Mathematics":94
}

total=sum(student.values())

percentage=total/4

print("Total =",total)
print("Percentage =",percentage)

Program 14: Merge Two Dictionaries

d1={
    "A":10,
    "B":20
}

d2={
    "C":30,
    "D":40
}

d1.update(d2)

print(d1)

Program 15: Safe Search Using get()

student={
    "Name":"Rahul",
    "Marks":95
}

key=input("Enter key: ")

print(student.get(key,"Key Not Found"))

Real-Life Applications

  • Student database management.
  • Employee information systems.
  • Hospital patient records.
  • Library catalog management.
  • Online shopping products.
  • Word frequency analysis.
  • School ERP systems.

CBSE Important Practical Programs

  1. Create a student dictionary.
  2. Search for a key.
  3. Traverse a dictionary.
  4. Display keys and values.
  5. Count total elements.
  6. Find maximum marks.
  7. Create nested dictionaries.
  8. Traverse nested dictionaries.
  9. Count character frequency.
  10. Count word frequency.

Viva Questions

  1. What is a nested dictionary?
  2. How do you access nested dictionary values?
  3. What is the use of items()?
  4. Why is get() safer than []?
  5. How do you traverse a dictionary?
  6. Can dictionary values be lists?
  7. Can dictionary values be dictionaries?
  8. How is frequency counting performed using dictionaries?
  9. Give two applications of dictionaries.
  10. How are dictionaries different from lists?

Quick Revision

  • Dictionaries store key-value pairs.
  • Keys are unique.
  • Use items() for traversal.
  • Nested dictionaries store dictionaries inside dictionaries.
  • Dictionaries are ideal for searching and frequency counting.