Write a code in Python to create a dictionary having names of your friends as values associated with suitable keys. Display the names of the friends whose names contain more than three characters.
import json
d={} #define an empty dictionary
n = int(input("Enter the number of friends:"))
for i in range(n):
name=input("Enter your friend's name")
key=input("Enter suitable nick name")
d[key]=name
print("The dictionary is\n",json.dumps(d,indent=4))
print("The names of the friends having more than 3 characters")
for i in d:
print(d[i] if len(d[i])>3 else None)
Write a Python code to create a dictionary containing three key-value pair such that each value contains a list of integers. Find
import json
d={} #empty dictionary
for i in range(3):
print(f"Enter the numbers for {i+1} key-value pair")
L=[] #empty list
while True:
n = int(input("Enter a number:"))
L.append(n)
k = input("Do you to continue: Y/N")
if k.lower() == "n":
break
m=i+1
key = "Key"+str(m)
d[key]=L
print("The dictionary is:")
print(json.dumps(d,indent=4))
print("The maximum value of each key is")
for i in d:
print(f"Maximum value in {i} key is {max(d[i])}")
Output: PS E:\class11\dictionary_APC> py .\03_max.py Enter the numbers for 1 key-value pair Enter a number:5 Do you to continue: Y/N Enter a number:7 Do you to continue: Y/N Enter a number:9 Do you to continue: Y/N Enter a number:2 Do you to continue: Y/N Enter a number:3 Do you to continue: Y/Nn Enter the numbers for 2 key-value pair Enter a number:19 Do you to continue: Y/N Enter a number:15 Do you to continue: Y/N Enter a number:12 Do you to continue: Y/N Enter a number:22 Do you to continue: Y/Nn Enter the numbers for 3 key-value pair Enter a number:14 Do you to continue: Y/Nn The dictionary is: { "Key1": [ 5, 7, 9, 2, 3 ], "Key2": [ 19, 15, 12, 22 ], "Key3": [ 14 ] } The maximum value of each key is Maximum value in Key1 key is 9 Maximum value in Key2 key is 22 Maximum value in Key3 key is 14 PS E:\class11\dictionary_APC>
Write a python code to create a dictionary containing some values as numbers by using suitable keys. Display all the values which are perfect numbers. [Hint: A number is said to be perfect, if the sum of the factors (including 1 and excluding the number itself) is equal to the same number)
import json
d={} #empty dictionary
n=int(input("Enter the number of pairs:"))
for i in range(n):
m=i+1
k = "key"+str(m)
p = int(input("Enter a number:"))
d[k]=p
print(f"Dictionary is \n {json.dumps(d,indent=4)}")
print("The perfect numbers are:")
for i in d:
n=d[i] #number value
s=0 #sum
for k in range(1,n):
if n%k==0:
s=s+k #sum of each factor
if s==n: #check if the sum is equal to the factor
print(f"{n}")