python prg docs
python prg docs
Sample Dictionary :
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
dic4 = {}
dic4.update(d)
print(dic4)
2nd prg:
# wap which removes a key from a dictionary.
print(myDict)
if k in myDict:
del myDict[k]
3rd prg:
# WAP that creates a dic with colour names as keys and
color_dict = {
'red': '000',
'green': '111',
'black': '222',
'white': '444'
'''O/p
black: 222
green: 111
red: 000
white: 444'''
4th prg:
#WAP to check whether a given key already exists in a dictionary.
'''Example
def is_key_present(x):
# Check if 'x' is a key in the dictionary 'd'.
if x in d:
else:
is_key_present(5)
5th prg:
# wap which Creates a dictionary 'my_dict' with key-value pairs.
# Find the key with the maximum value in 'my_dict' using the 'max' function and a lambda function.
maxv = max(my_dict.values())
minv = min(my_dict.values())
# Print the maximum value by using the 'key_max' to access the corresponding value in 'my_dict'.
# Print the minimum value by using the 'key_min' to access the corresponding value in 'my_dict'.
6th prg:
'''WAP to generate a dictionary that contains a number
Sample Dictionary ( n = 5) :
d = dict()
d[x] = x * x
print(d)
7th prg:
#Write a Python program to sum all the items in a dictionary.
''' Use the 'sum' function to calculate the sum of all values
result = sum(my_dict.values())
8th prg:
'''Q1. WAp to sort (ascending and descending)a dictionary by value'''
d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
order by value.'''
sorted_d=dict(sorted(d.items(),key=operator.itemgetter(1),reverse=True))