Write a Python program to check multiple keys exists in a dictionary
The program demonstrates the use of the &ht;= operator in the context of dictionaries. The &ht;= operator checks whether the set on the left side is a superset of the set on the right side, which means that all elements in the set on the right side are also in the set on the left side.
- In the first line, the program checks if the keys in the "student" dictionary are a superset of the set {'age', 'name'}. Since both the keys 'age' and 'name' are present in the "student" dictionary, the output is True.
- In the second line, the program checks if the keys in the "student" dictionary are a superset of the set {'City', 'Ram'}. Here, the key 'City' has a different casing than the key 'city' present in the "student" dictionary, hence the output is False.
- In the third line, the program checks if the keys in the "student" dictionary are a superset of the set {'city', 'Salem'}. Since the keys 'city' and 'Salem' are not keys in the "student" dictionary, the output is False.
Source Code
student = {
'name': 'Ram',
'age': 23,
'city': 'Salem'
}
print(student.keys() >= {'age', 'name'})
print(student.keys() >= {'City', 'Ram'})
print(student.keys() >= {'city', 'Salem'})
Output
True
False
False