Output of Python Program | Set 7 (Dictionary)
0 187
Output of Python Program | Set 7 (Dictionary)
Dictionaries in Python are powerful data structures that store data in key-value pairs. Understanding how dictionaries work, how to access, modify, and manipulate their contents is crucial for effective Python programming. In this blog, we will walk through some example programs to see the output of various dictionary operations.
Creating and Accessing Dictionaries
You can create a dictionary by enclosing key-value pairs in curly braces. Accessing values is done via their keys:
student = {
"name": "Alice",
"age": 22,
"courses": ["Math", "Physics"]
}
print(student["name"]) # Output: Alice
print(student.get("age")) # Output: 22
Both direct key access student["name"]
and the get()
method retrieve values from the dictionary.
Adding and Modifying Entries
Dictionaries are mutable, so you can add new key-value pairs or update existing ones:
student["grade"] = "A"
student["age"] = 23
print(student)
# Output: {'name': 'Alice', 'age': 23, 'courses': ['Math', 'Physics'], 'grade': 'A'}
This shows how to add a new key grade
and update the age
key.
Removing Items from a Dictionary
There are several ways to remove items, such as pop()
and del
:
removed_course = student.pop("courses")
print(removed_course) # Output: ['Math', 'Physics']
print(student) # Output: {'name': 'Alice', 'age': 23, 'grade': 'A'}
del student["grade"]
print(student) # Output: {'name': 'Alice', 'age': 23}
Using pop()
removes and returns the value for the given key, while del
simply deletes the key-value pair.
Iterating Over Dictionaries
You can loop through keys, values, or both using dictionary methods:
for key in student.keys():
print(key)
for value in student.values():
print(value)
for key, value in student.items():
print(key, ":", value)
This helps in accessing dictionary elements in a variety of ways depending on your need.
Practice Problem
Try the following exercise to test your understanding of dictionary operations:
# Practice Problem:
# Given a dictionary containing student names as keys and their scores as values,
# write a Python program to find the student with the highest score.
scores = {
"John": 88,
"Emma": 92,
"Sophia": 85,
"Michael": 95,
"Olivia": 90
}
# Your code here
# Expected output:
# Student with the highest score: Michael with 95
Solution to Practice Problem
scores = {
"John": 88,
"Emma": 92,
"Sophia": 85,
"Michael": 95,
"Olivia": 90
}
max_student = max(scores, key=scores.get)
max_score = scores[max_student]
print(f"Student with the highest score: {max_student} with {max_score}")
This code uses Python’s built-in max()
function with the key
argument to find the student with the highest score.
Summary
In this blog, we explored dictionary basics including creation, access, modification, deletion, and iteration. Dictionaries provide a flexible way to store and manage data by key-value pairs, and practicing these concepts will help you use them effectively in your Python projects.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments