Dicitionary

 simple program with dictionary

dictionary = {
" hlo": "to the principal ",
" hy ": "good morning",
" hi ": "yo boi",

# this is child dictionary
"dictionary2": {"flew": "oh my boi"}
}

print(dictionary[" hlo"])
print(dictionary[" hi "])
print(dictionary[" hy "])

# way to access child dictionary

print(dictionary["dictionary2"]["flew"])

Output:








Program with dictionary methods

dict = {
'f': 'fruit',
'v': 'vegetable',
'd': 'drinks'
}

# print all the keys in dictionary
print(dict.keys())

# print all the values of dictionary
print(dict.values())

# print the key:values at same
print(dict.items())

# append dictionary
dict['dr'] = 'dry fruits'
print(dict.keys())
print(dict['dr'])

# updating a key in dictionary
print(dict['f'])
dict['f'] = 'fry chicken'
print(dict['f'])

# empty dictionary
dict1 ={}
print(type(dict1))

Output:







Progarm to create a word dictionary

hindi_dict = {
"tu" : "you",
"mai": "me",
"kutta": "dog"
}
a = input("enter the word to find it's meaning in english \n")
print(a, " : ", hindi_dict[a])
# if key is not present in dictionary the bellow syntax will not throw error
print(a, " : ", hindi_dict.get(a))

Output:







Program to enter user input key and values

dict1 = {}
for i in range (0,3,1):
a = input("enter your name ")
b = input("enter your favorite language ")
dict1[a] = b
print(dict1.items())

Output:







No comments:

Post a Comment