Introduction
A dictionary in Python is a collection of key-value pairs where each key is associated with a value. Dictionaries are mutable, meaning they can be modified after their creation. This tutorial will guide you through creating a Python program that creates a dictionary, allowing you to store and access data efficiently.
Example:
-
Input:
{'name': 'John', 'age': 25, 'city': 'New York'}
-
Output:
{'name': 'John', 'age': 25, 'city': 'New York'}
-
Input:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
-
Output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
Problem Statement
Create a Python program that:
- Takes key-value pairs as input from the user.
- Creates a dictionary using the provided key-value pairs.
- Displays the created dictionary.
Solution Steps
- Take Input from the User: Use the
input()
function to get key-value pairs from the user. - Create the Dictionary: Use a loop or other method to add key-value pairs to the dictionary.
- Display the Dictionary: Use the
print()
function to display the created dictionary.
Python Program
# Python Program to Create a Dictionary
# Author: https://www.rameshfadatare.com/
# Step 1: Initialize an empty dictionary
my_dict = {}
# Step 2: Get the number of key-value pairs
n = int(input("Enter the number of key-value pairs: "))
# Step 3: Add key-value pairs to the dictionary
for _ in range(n):
key = input("Enter key: ")
value = input("Enter value: ")
my_dict[key] = value
# Step 4: Display the created dictionary
print(f"The created dictionary is: {my_dict}")
Explanation
Step 1: Initialize an Empty Dictionary
- An empty dictionary
my_dict
is initialized using curly braces{}
. This will store the key-value pairs entered by the user.
Step 2: Get the Number of Key-Value Pairs
- The
input()
function is used to prompt the user to enter the number of key-value pairs they want to add to the dictionary. This input is converted to an integer and stored in the variablen
.
Step 3: Add Key-Value Pairs to the Dictionary
- A
for
loop is used to iteraten
times. In each iteration, the user is prompted to enter a key and a value, which are then added to the dictionary usingmy_dict[key] = value
.
Step 4: Display the Created Dictionary
- The
print()
function is used to display the dictionary after all key-value pairs have been added.
Output Example
Example 1:
Enter the number of key-value pairs: 3
Enter key: name
Enter value: John
Enter key: age
Enter value: 25
Enter key: city
Enter value: New York
The created dictionary is: {'name': 'John', 'age': 25, 'city': 'New York'}
Example 2:
Enter the number of key-value pairs: 2
Enter key: brand
Enter value: Ford
Enter key: model
Enter value: Mustang
The created dictionary is: {'brand': 'Ford', 'model': 'Mustang'}
Example 3:
Enter the number of key-value pairs: 4
Enter key: country
Enter value: USA
Enter key: capital
Enter value: Washington D.C.
Enter key: population
Enter value: 331 million
Enter key: continent
Enter value: North America
The created dictionary is: {'country': 'USA', 'capital': 'Washington D.C.', 'population': '331 million', 'continent': 'North America'}
Conclusion
This Python program demonstrates how to create a dictionary by prompting the user for key-value pairs. Dictionaries are versatile data structures in Python, and understanding how to create and manipulate them is essential for handling structured data efficiently.