In this example, you will learn to check if a key is present in a dictionary.
To understand this example, you should have the knowledge of the following Python programming topics:
Using in keyword
my_dict = {1: 'a', 2: 'b', 3: 'c'} if 2 in my_dict: print("present")
Output
present
Using if statement
and in
keyword, you can check if a key is present in a dictionary.
In the above example, 2 is present in the dictionary as a key; therefore, the output is present
.
You can use not in
if you want to check if a key is not present in the dictionary.
Related posts:
Python Program to Make a Simple Calculator
Python List remove()
Python hex()
Python Program to Remove Punctuations From a String
Python Program to Check if a Number is Odd or Even
Python @property decorator
Python String lower()
Python Program to Add Two Numbers
Python Program to Check Armstrong Number
Python List reverse()
Python RegEx
Python Program to Differentiate Between type() and isinstance()
Python Program to Make a Flattened List from Nested List
Python Program to Generate a Random Number
Python Dictionary
Python Dictionary update()
Python Data Structures and Algorithms - Benjamin Baka
Python Inheritance
Python Program to Count the Number of Each Vowel
Python Strings
Python Variables, Constants and Literals
Python Program to Compute all the Permutation of the String
Python Set difference()
Python Program to Find the Largest Among Three Numbers
Python del Statement
Intelligent Projects Using Python - Santanu Pattanayak
Python Program to Sort Words in Alphabetic Order
Python sum()
Python Program to Display the multiplication Table
Python round()
Python str()
Python Type Conversion and Type Casting