ASSIGNMENT2 22000423 Visha

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

CS-194,Problem solving using python lab 22000423,Visha Jain

ASSIGNMENT-1

Practial-1
Aim: Python program to create a dictionary with employee details and retrieve the
values upon giving the keys.

Input:

employee={}

a=int(input("enter the number of employees:"))

for i in range(a):

empid=input("enter the ID of employee:")

name=input("enter the name of employee:")

salary=input("enter the salary of the employee:")

phno=input("enter the phone number:")

employee[empid]=name,salary,phno

print(employee)

b=(input("enter the key:"))

for i in employee.keys():

if i==b:

print(employee[i])

1
CS-194,Problem solving using python lab 22000423,Visha Jain

Output:

2
CS-194,Problem solving using python lab 22000423,Visha Jain

PRACTICAL-2
Aim: Python program to retrieve keys, values, and key-value pairs from a
dictionary

Input:

dict1={}

a=int(input("enter the number of key value pairs you want to create:"))

for i in range(a):

color=input("enter the color")

color_code=input("enter the color code:")

dict1[color]=color_code

keys = dict1.keys()

print("Keys:", keys)

values = dict1.values()

print("Values:", values)

key_value_pairs = dict1.items()

print("Key-value pairs:", key_value_pairs)

Output:

3
CS-194,Problem solving using python lab 22000423,Visha Jain

PRACTICAL-3

Aim: Python program to understand dictionary methods


Input:

dict1 = {"red": 104, "green":108, "blue": 100}

print(dict1["green"])

dict1["red"] =105

print(dict1)

dict1["purple"] = 202

print(dict1)

del dict1["green"]

print(dict1)

Output:

4
CS-194,Problem solving using python lab 22000423,Visha Jain

PRACTICAL-4

Aim: Python program to create a dictionary (with cricket player’s names and
scores in the match) from keyboard and display the elements. Also retrieves runs

by entering the player’s name.

Input:

s = {}

players = int(input("Enter the number of players: "))

for i in range(players):

name = input("Enter the name of player: ")

score = int(input("Enter the score of player: "))

s[name] = score

print("Cricket score with player's name:", s)

p_name = input("Enter the name of the player whose score you want to retrieve: ")

if p_name in s:

print(p_name,"'s score is ", s[p_name])

else:

print(p_name,"is not in the dictionary")

5
CS-194,Problem solving using python lab 22000423,Visha Jain

Output:

6
CS-194,Problem solving using python lab 22000423,Visha Jain

PRACTICAL-5

Aim: Python program to find the number of occurrences of each letter in a string
using dictionary

Input:

a=input("enter a string:")

dict1={}

for i in a:

if i in dict1:

dict1[i] += 1

else:

dict1[i] = 1

print ("Occurrence of all characters in",a," is ",str(dict1))

Output:

7
CS-194,Problem solving using python lab 22000423,Visha Jain

PRACTICAL-6
Aim: Python program to sort the elements of a dictionary based on a key or value.

Input:

d = {'green': 5, 'red': 2, 'blue': 4, 'purple': 1}

sortkeys = sorted(d.items())

sortvalues = sorted(d.items(), key=lambda x: x[1])

print("Sorted by keys:", sortkeys)

print("Sorted by values:", sortvalues)

Output:

8
CS-194,Problem solving using python lab 22000423,Visha Jain

PRACTICAL-7
Aim: Python program to find the sum of all items in a dictionary

Input:

dict1 = {1: 5,2:10,3:15,4:20}

svalues = sum(dict1.values())

skeys=sum(dict1.keys())

print("Sum of all values:", svalues)

print("sum of all keys:",skeys)

Output:

9
CS-194,Problem solving using python lab 22000423,Visha Jain

PRACTICAL-8

Aim: Python Program to Concatenate Two Dictionaries into One.

Input:

d1 = {'sun': 5, 'moon': 2, 'star': 4, 'pluto': 1}

d2 = {'saturn': 3, 'neptune': 6, 'earth': 2}

d3 = {**d1, **d2}

print("Concatenated dictionary:", d3)

Output:

10
CS-194,Problem solving using python lab 22000423,Visha Jain

PRACTICAL-9
Aim:Python Program to Count the Frequency of Words Appearing in a String
Using a Dictionary.

Input:

s=input("enter the string:")

words = s.split()

count = {}

for word in words:

if word in count:

count[word] += 1

else:

count[word] = 1

print("Word count:", count)

Output:

11
CS-194,Problem solving using python lab 22000423,Visha Jain

PRACTICAL-10
Aim:Python Program to Convert Two Lists into a Dictionary

Input:

keys = ['red', 'blue', 'green', 'purple']

values = [5, 2, 4, 1]

d = dict(zip(keys, values))

print("Dictionary:", d)

Output:

12

You might also like