Sunday, 19 January 2025

Day 91: Python Program to Check if a Key Exists in a Dictionary or Not

 


def check_key_exists(dictionary, key):

    """

    Check if a key exists in the dictionary.

     Args:

        dictionary (dict): The dictionary to check.

        key: The key to search for.

     Returns:

        bool: True if the key exists, False otherwise.

    """

    return key in dictionary

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

key_to_check = input("Enter the key to check: ")


if check_key_exists(my_dict, key_to_check):

    print(f"The key '{key_to_check}' exists in the dictionary.")

else:

    print(f"The key '{key_to_check}' does not exist in the dictionary.")


#source code --> clcoding.com 

Code Explanation:

1. Function Definition
def check_key_exists(dictionary, key):
The function check_key_exists is defined with two parameters:
dictionary: This is the dictionary you want to check.
key: The specific key you want to search for within the dictionary.

2. Function Docstring
"""
Check if a key exists in the dictionary.

Args:
    dictionary (dict): The dictionary to check.
    key: The key to search for.

Returns:
    bool: True if the key exists, False otherwise.
"""
This is a docstring, which provides documentation for the function.
It explains:
What the function does: It checks if a key exists in a given dictionary.
Arguments:
dictionary: The dictionary to search in.
key: The key to search for.
Return Value: The function returns a bool (Boolean value), True if the key exists, and False if it doesn’t.

3. Logic to Check Key
return key in dictionary
The function uses the in operator, which checks if the specified key is present in the dictionary.
If the key exists, it returns True; otherwise, it returns False.

4. Dictionary Declaration
my_dict = {"name": "Max", "age": 25, "city": "Germany"}
A dictionary my_dict is created with three key-value pairs:
"name": "Max"
"age": 25
"city": "Germany"

5. User Input
key_to_check = input("Enter the key to check: ")
The program asks the user to input a key they want to check.
The input() function takes the user’s input as a string and stores it in the variable key_to_check.

6. Key Existence Check
if check_key_exists(my_dict, key_to_check):
The check_key_exists function is called with my_dict and the user-provided key_to_check as arguments.
If the function returns True (key exists), the code inside the if block executes.
Otherwise, the else block is executed.

7. Output Messages
    print(f"The key '{key_to_check}' exists in the dictionary.")
If the key exists, this line prints a success message indicating the key exists in the dictionary.

    print(f"The key '{key_to_check}' does not exist in the dictionary.")
If the key doesn’t exist, this line prints a failure message indicating the key is absent.

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