Sheet No: ……………
VNRVJIET
Name of the Experiment: Dictionary
Name of the Laboratory:
Python Programming and Practice Experiment No.: 5 Date:
a) Write a program to count the numbers of characters in the string and store them in a
dictionary data structure.
Code:
def count_characters(s):
char_count = {}
for char in s:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1
return char_count
string1 = "good morning"
char_num = count_characters(string1)
print("Character Count:", char_num)
Output:
b) Write a program combine lists into a dictionary.
Code:
def combine_lists(keys, values):
return dict(zip(keys, values))
keys = ['name', 'age', 'city']
values = ['Kate', 23, 'New York']
combined_dict = combine_lists(keys, values)
print("Combined Dictionary:", combined_dict)
Output: