We Can Create Dictionary With Following Four Methods
We Can Create Dictionary With Following Four Methods
# Creating a Dictionary
# with Integer Keys
Dict = {1: 'Delhi', 2: 'Ajmer', 3: 'Beawar'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
# Creating a Dictionary
# with Mixed keys
Dict = {'Name': 'INFOTECH', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)
# Creating a Dictionary
# with dict() method
Dict = dict({1: 'yellow', 2: 'white', 3:'green'})
print("\nDictionary with the use of dict(): ")
print(Dict)
# Creating a Dictionary
# with each item as a Pair
Dict = dict([(1, 'Amit'), (2, 'Sumit')])
print("\nDictionary with each item as a pair: ")
print(Dict)
# Creating a Dictionary
Dict = {1: 'Computer', 'name': 'INFOTECH', 3: 'Education'}
# Deleting a Key
# using pop()
Dict.pop(5)
print("\nPopping specific element: ")
print(Dict)
# Deleting a Key
# using popitem()
Dict.popitem()
print("\nPops first element: ")
print(Dict)
Dictionary Methods
METHODS DESCRIPTION
clear() The clear() method removes all items from the dictionary.
pop() Removes and returns an element from a dictionary having the given key.
popitem() Removes the arbitrary key-value pair from the dictionary and returns it as tuple.
fromkeys() Create a new dictionary with keys from seq and values set to value.
Q1. WAP to create a phone directory and print phone numbers of all the friends?
An.s.
Pd={“ram”:9829089288,”shyam”:9938934844,”Mohan”:93849899,”Sohan”:93489349}
print (name,”:”,pd[name])