Q-1. Write a python program to Generate dictionary of numbers and their squares.
Input:10
Output:{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}
Python Code:
#generate squares of numbers n=int(input()) item={} for i in range(1,n+1): item[i]=i*i print(item)
Q-2. Write a Python program to sum all the items in a dictionary.
Input:{'data_1':5,'data_2':6,'data_3':7}
Output:18
Python Code:
#sum of items in dictionary my_dict = {'data_1':5,'data_2':6,'data_3':7} result=0 for key in my_dict: result=result +my_dict[key] print(result)
Q-3. Write a Python program to multiply all the items in a dictionary.
Input:{'data_1':5,'data_2':6,'data_3':7}
Output:210
Python Code:
#multiply of items in dictionary my_dict = {'data_1':5,'data_2':6,'data_3':7} result=1 for key in my_dict: result=result *my_dict[key] print(result)
Q-4. Write a Python program to get the product of two dictionary keys.
Input:dict_1 = {'data_1' : 6, 'data_2' : 4, 'data_3' : 7} dict_2 = {'data_1' : 10, 'data_2' : 6, 'data_3' : 10}
Output:dictionary 1 : {'data_1': 6, 'data_2': 4, 'data_3': 7} dictionary 2 : {'data_1': 10, 'data_2': 6, 'data_3': 10} The product dictionary is : {'data_1': 60, 'data_2': 24, 'data_3': 70}
Python Code:
#product of two dictionary dict_1 = {'data_1' : 6, 'data_2' : 4, 'data_3' : 7} dict_2 = {'data_1' : 10, 'data_2' : 6, 'data_3' : 10} print("dictionary 1 : " + str(dict_1)) print("dictionary 2 : " + str(dict_2)) result = {key: dict_2[key] * dict_1.get(key, 0) for key in dict_2.keys()} print("The product dictionary is : " + str(result))
Q-5. Write a Python program to check a dictionary is empty or not
Input:{}
Output:Dictionary is empty
Python Code:
#check if dictionary is empty or not my_dict = {} if not bool(my_dict): print("Dictionary is empty") else: print("dictionary is not empty")
0 Comments