Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

1.

Python program to create a dictionary with employee details and


retrieve the values upon giving the keys.

CODE:
d = {
"Ename" : ["ABC", "DEF", "GHI"],
"Dept" : ["CS", "IT", "Elec."],
"Joindate" : ["03-04-2020", "02-08-2016", "01-12-2019"]
}

print("Dictionary: ", d)
print("Ename: ",
d["Ename"])
print("Department: ", d["Dept"])

OUTPUT:
2. Python program to retrieve keys, values, and key-value pairs from
a dictionary.

CODE:
d = {
"Ename" : ["ABC", "DEF", "GHI"],
"Dept" : ["CS", "IT", "Elec."],
"Joindate" : ["03-04-2020", "02-08-2016", "01-12-2019"]
}

print("Dictionary: ", d)
print("Keys: ",
d.keys())
print("Values: ",
d.values()) print("Key-Value
pairs: ") for key, value in
d.items():

OUTPUT:
3. Python program to understand dictionary methods.

CODE:
d = {
"Ename" : ["ABC", "DEF"],
"Dept" : ["CS", "IT"],
"Joindate" : ["03-04-2020", "01-12-2019"],
"Company" : "Google"
}

print("Dictionary: ",
d) #Adding a new item
d["Salary"] = [999999, 777777]
print("\nNew Dictionary: ", d)
#Displaying all keys
print("\nKeys: ", d.keys())
#Displaying all values
print("\nValues: ",
d.values()) #Displaying all
items print("\nItems: ",
d.items()) #Getting a specific
value
print("\nEname Value: ", d.get("Ename"))
#Updating a value
d.update({"Company":
"Microsoft"}) #Removing a key-
value pair d.pop("Company")
print("\nUpdated Dictionary", d)
#Clearing the entire dictionary
d.clear()
print("\nCleared Dictionary: ",
d) #Deleting the dictionary
del d
print("\nDictionary Deleted...")
OUTPUT:
4. Python program to create a dictionary (with cricket player’s names
and scores in the match) from keyboard and display the elements. Also
retrieves runs by entering the player’s name.

CODE:
dplay = {}
n = int(input("Enter number of players you want to add: "))

for i in range(0, n):


player = input("Enter player name: ")
score = int(input("Enter score: "))
dplay[player] = score

print("Scorecard: ")
for p, s in
dplay.items():
print(p, s, sep=" :
")

search = input("Enter player name whose score you want to search:


") if (search in dplay.keys()):
print("Score: ", dplay[search])
else:

OUTPUT:
5. Python program to find the number of occurrences of each letter in
a string using dictionary.

CODE:
string = input("Enter string:")
strlist = list(string)
c = []

for i in strlist:
x =
strlist.count(i)
c.append(x)

OUTPUT:
6. Python program to sort the elements of a dictionary based on a key
or value.

CODE:
d = {
"s" : 72,
"c" : 9,
"m" : 32,
"x" : 54,
"b" : 12
}

print("Original Dictionary: ", d)


print("Sorted Dictionary:
") print(sorted(d.items()))

OUTPUT:
7. Python program to find the sum of all items in a dictionary.

CODE:
d={
"s" : 72,
"c" : 9,
"m" : 32,
"x" : 54,
"b" : 12
}

s=0
for i in d.values(): s += i

print("Sum: ", s)

OUTPUT:
8. Python Program to Concatenate Two Dictionaries into One.

CODE:
d1 = {
"s" : 72,
"c" : 9,
"m" : 32
}
print("D1: ", d1)

d2 = {
"x" : 54,
"b" : 12
}
print("D2: ", d2)

d1.update(d2)
print("Concatenated dictionary: ", d1)

OUTPUT:
9. Python Program to Count the Frequency of Words Appearing in a
String Using a Dictionary.

CODE:
string = input("Enter string:")
strlist = string.split()
c = []

for i in strlist:
x =
strlist.count(i)
c.append(x)

OUTPUT:
10. Python Program to Convert Two Lists into a Dictionary.

CODE:
keys = ["A", "B", "C", "D", "E"]
values = [1, 2, 3, 4, 5]

print("List 1: ", keys)


print("List 2: ", values)

d = dict(zip(keys,

values))

OUTPUT:

You might also like