File: exp5.ipynb
Experiment: Study of Dictionary in Python
Name: Ronit Mehta
PRN: 25070123094
To study and implement Python Dictionaries
A dictionary in Python is an unordered collection of data stored in key–value pairs, enclosed within curly braces {}.
- Store elements as key–value pairs
- Keys must be unique and immutable
- Values can be of any data type
- Dictionaries are mutable (elements can be added, updated, or removed)
- Accessing elements is done using keys rather than indexing
- Provide built-in methods like
get(),update(),pop(),keys(), andvalues()
Dictionaries are widely used for structured data storage, fast lookup operations, and real-world applications such as login systems, student databases, and product management.
- Create a dictionary containing car details such as brand, model, and year.
- Display the dictionary.
- Create a dictionary with car details.
- Access the value of a specific key (
brand). - Display the retrieved value.
- Create a dictionary with duplicate keys (
year). - Display the dictionary.
- Observe that the latest value overwrites the previous one.
- Use the
len()function on the dictionary. - Display the total number of key–value pairs.
- Create a dictionary.
- Use the
type()function to verify its data type. - Display the result.
- Create a dictionary named
car. - Add a new key (
color) using theupdate()method. - Display the updated dictionary.
- Retrieve and display all values using
values().
- Create a dictionary containing student details.
- Retrieve the student's name using the
get()method. - Remove an element (
branch) usingpop(). - Display the modified dictionary.
- Create a dictionary representing product prices.
- Modify the price of an existing product (
Mouse). - Display the updated dictionary.
- Create a dictionary with student names and marks.
- Accept user input to search for a student's marks.
- Use the
get()method to safely retrieve the value. - Display the result.
- Create a dictionary storing usernames and passwords.
- Accept username and password as input.
- Check whether the username exists in the dictionary.
- Validate the password and display appropriate messages.
- Create a dictionary containing student marks.
- Use
max()withkey=marks.getto find the highest scorer. - Display the topper's name along with their marks.
This experiment demonstrates the effective use of Python Dictionaries for storing and managing structured data. It highlights essential operations such as accessing values, updating records, removing elements, and performing searches efficiently.
The experiment also showcases practical applications like product updates, login validation, and student performance tracking, making dictionaries one of the most powerful and commonly used data structures in Python.