Sunday, 19 January 2025

Day 92: Python Program to Add a Key Value Pair to the Dictionary

 


def add_key_value(dictionary, key, value):

    """

    Adds a key-value pair to the dictionary.

    Args:

        dictionary (dict): The dictionary to update.

           key: The key to add.

        value: The value associated with the key.

  Returns:

        dict: The updated dictionary.

    """

    dictionary[key] = value

    return dictionary

my_dict = {"name": "Max", "age": 25, "city": "Delhi"}

print("Original dictionary:", my_dict)

key_to_add = input("Enter the key to add: ")

value_to_add = input("Enter the value for the key: ")

updated_dict = add_key_value(my_dict, key_to_add, value_to_add)

print("Updated dictionary:", updated_dict)

#source code --> clcoding.com 

Code Explanation: 

1. Function Definition
def add_key_value(dictionary, key, value):
This function is named add_key_value. It is designed to add a key-value pair to an existing dictionary.
Parameters:
dictionary: The dictionary to which the new key-value pair will be added.
key: The new key to add.
value: The value associated with the new key.

2. Function Docstring
"""
Adds a key-value pair to the dictionary.

Args:
    dictionary (dict): The dictionary to update.
    key: The key to add.
    value: The value associated with the key.

Returns:
    dict: The updated dictionary.
"""
This is a docstring that documents what the function does:
What it does: Adds a new key-value pair to a given dictionary.
Arguments:
dictionary: The dictionary to update.
key: The key to add.
value: The value associated with the key.
Return Value: The updated dictionary with the new key-value pair.

3. Logic to Add Key-Value Pair
dictionary[key] = value
The new key-value pair is added to the dictionary:
key is the key to add.
value is the associated value.
If the key already exists in the dictionary, this will update the value of the key.

4. Return Updated Dictionary
return dictionary
After adding or updating the key-value pair, the function returns the updated dictionary.

5. Initial Dictionary
my_dict = {"name": "Max", "age": 25, "city": "Delhi"}
A dictionary my_dict is created with the following key-value pairs:
"name": "Max"
"age": 25
"city": "Delhi"

6. Display Original Dictionary
print("Original dictionary:", my_dict)
Prints the original dictionary before any modification.

7. User Input
key_to_add = input("Enter the key to add: ")
value_to_add = input("Enter the value for the key: ")
input() Function: Prompts the user to enter the key and the value to add to the dictionary.
key_to_add: Stores the user-provided key.
value_to_add: Stores the user-provided value.

8. Call the Function
updated_dict = add_key_value(my_dict, key_to_add, value_to_add)
The add_key_value function is called with:
my_dict: The original dictionary.
key_to_add: The user-provided key.
value_to_add: The user-provided value.
The function updates my_dict by adding the new key-value pair and returns the updated dictionary.
The result is stored in the variable updated_dict.

9. Display Updated Dictionary
print("Updated dictionary:", updated_dict)
Prints the dictionary after adding the new key-value pair.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (90) AI (37) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (184) C (77) C# (12) C++ (83) Course (67) Coursera (231) Cybersecurity (24) Data Analytics (1) data management (11) Data Science (135) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Euron (19) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (5) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (62) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (959) Python Coding Challenge (402) Python Quiz (56) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses