Write a program to create a dictionary containing names of five students as keys and their age
as value. Keys and values should be entered by the user.
Answers
Answer:
Program/Source Code
Here is source code of the Python Program to create a dictionary with key as first character and value as words starting with that character. The program output is also shown below.
test_string=raw_input("Enter string:") l=test_string.split() d={} for word in l: if(word[0] not in d.keys()): d[word[0]]=[] d[word[0]].append(word) else: if(word not in d[word[0]]): d[word[0]].append(word) for k,v in d.items(): print(k,":",v)
Explanation:
Runtime Test Cases
Case 1: Enter string:Hello world this is a test string sanfoundry ('a', ':', ['a']) ('i', ':', ['is']) ('H', ':', ['Hello']) ('s', ':', ['sanfoundry', 'string']) ('t', ':', ['test', 'this']) ('w', ':', ['world']) Case 2: Enter string:python is my most favourite programming language in the entire world ('e', ':', ['entire']) ('f', ':', ['favourite']) ('i', ':', ['in', 'is']) ('m', ':', ['most', 'my']) ('l', ':', ['language']) ('p', ':', ['programming', 'python']) ('t', ':', ['the']) ('w', ':', ['world'])