0% found this document useful (0 votes)
4 views

python prg docs

The document contains eight Python programs demonstrating various dictionary operations, including concatenation, key deletion, sorting by keys, checking key existence, finding maximum and minimum values, generating squares of numbers, summing values, and sorting by values. Each program includes sample dictionaries and expected outputs. The programs serve as practical examples for manipulating dictionaries in Python.

Uploaded by

s.aafreen2009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

python prg docs

The document contains eight Python programs demonstrating various dictionary operations, including concatenation, key deletion, sorting by keys, checking key existence, finding maximum and minimum values, generating squares of numbers, summing values, and sorting by values. Each program includes sample dictionaries and expected outputs. The programs serve as practical examples for manipulating dictionaries in Python.

Uploaded by

s.aafreen2009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1st prg:

'''3. wap to concatenate dictionaries(join) to create a new one.

Sample Dictionary :

dic1={1:10, 2:20}

dic2={3:30, 4:40}

dic3={5:50,6:60}

Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}'''

# Create three dictionaries with key-value pairs.

dic1 = {1: 10, 2: 20}

dic2 = {3: 30, 4: 40}

dic3 = {5: 50, 6: 60}

# Create empty dic. 'dic4' to store their combined key-value pairs

dic4 = {}

for d in (dic1, dic2, dic3):

dic4.update(d)

print(dic4)

2nd prg:
# wap which removes a key from a dictionary.

# Create a dictionary 'myDict' with key-value pairs.

myDict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

# Print the original dictionary 'myDict'.

print(myDict)

k=input("Enter the key whose data to be deleted from the dict.")

if k in myDict:

# If present in the dict, delete the key-value

del myDict[k]

# Print the updated dictionary 'myDict' after deleting


print(myDict)

3rd prg:
# WAP that creates a dic with colour names as keys and

#their corresponding color codes as values.

#now sort dictionary based on key, display the sorted dictionary

color_dict = {

'red': '000',

'green': '111',

'black': '222',

'white': '444'

print("Original dict is",color_dict)

for key in sorted(color_dict):

print("%s: %s" % (key, color_dict[key]))

'''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

d={1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}

The key 5 is present in the dictionary

The key 9 is not present in the dictionary'''

d = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}

print(d)#print entire dictionary

# create a function 'is_key_present' that takes an argument 'x'.

def is_key_present(x):
# Check if 'x' is a key in the dictionary 'd'.

if x in d:

# If present print the key is present.

print('The key', x,'is present in the dictionary')

else:

# If 'x' is not present print the key is not present.

print('The key', x,'is not present in the dictionary')

#Call function to check if the key 5 is present in dictionary.

is_key_present(5)

5th prg:
# wap which Creates a dictionary 'my_dict' with key-value pairs.

my_dict = {'x': 500, 'y': 5874, 'z': 560}

# Find the key with the maximum value in 'my_dict' using the 'max' function and a lambda function.

# The 'key' argument specifies how the maximum value is determined.

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('Maximum Value: ', maxv)

# Print the minimum value by using the 'key_min' to access the corresponding value in 'my_dict'.

print('Minimum Value: ', minv)

6th prg:
'''WAP to generate a dictionary that contains a number

(between 1 and n) in the form (x, x*x).

Sample Dictionary ( n = 5) :

Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}'''


n = int(input("Input a number "))

# Create an empty dictionary 'd' to store the square of numbers.

d = dict()

# Iterate through numbers from 1 to 'n' (inclusive).

for x in range(1, n + 1):

# Calc. square of each number, store it in 'd' with number as key.

d[x] = x * x

# Print the dictionary 'd' containing the squares of numbers

print(d)

7th prg:
#Write a Python program to sum all the items in a dictionary.

# Create a dictionary 'my_dict' with key-value pairs.

my_dict = {'data1': 10, 'data2': 20, 'data3': -10}

''' Use the 'sum' function to calculate the sum of all values

in the 'my_dict' dictionary.

'my_dict.values()' extracts the values from the dictionary,

and 'sum()' calculates their sum.'''

print("The dictionary is",my_dict)

result = sum(my_dict.values())

# Print the result, which is the sum of the values.

print("sum of values of the key=",result)

8th prg:
'''Q1. WAp to sort (ascending and descending)a dictionary by value'''

import operator#must for sorting etc


# Create a dictionary 'd' with key-value pairs.

d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}

# Print the original dictionary 'd'.

print('Original dictionary : ',d)

'''Sort the items (key-value pairs) in the dictionary 'd' based

on the values (1st element of each pair).

The result is a list of sorted key-value pairs.'''

sorted_d = sorted(d.items(), key=operator.itemgetter(1))

print('Dictionary in ascending order by value : ',sorted_d)

'''Convert the sorted list of key-value pairs back into a

dictionary.The 'reverse=True' argument sorts the list in descending

order by value.'''

sorted_d=dict(sorted(d.items(),key=operator.itemgetter(1),reverse=True))

# Print the dictionary 'sorted_d' in descending order by value.

print('Dictionary in descending order by value : ',sorted_d)

You might also like