You are on page 1of 8

#Program 1: Counting Occurrences of Characters

string = input("Enter String")

dic = {}

for i in string:

dic[i] = string.count(i)

print("Output:", dic)

#Program 2: Checking for Same Values in Dictionary

d = {}

n = int(input("Enter the limit"))

for i in range(n):

ke = input("Enter key")

va = input("Enter value")

d[ke] = va

print(d)

for i in d.values():

c=0

for j in d.values():

if i == j:

c += 1

if c == 2:

print("2 keys have the same values")

break

else:

print("No keys have the same values")

#Program 3: Calculating Commission for Salesmen


d = {}

n = int(input("Enter the limit"))

print("Enter code as key name, salamt as values")

for i in range(n):

t = ()

code = int(input("Enter code"))

name = input("Enter name")

salamt = float(input("Enter sales amount"))

t += (name, salamt)

d[code] = t

print("CODE\tNAME\tSALESAMOUNT\tCOMMISSION")

for i in d:

if d[i][1] < 10000:

c=0

elif 10000 >= d[i][1] <= 30000:

c = d[i][1] * 0.1

else:

c = d[i][1] * 0.15

print(i, '\t', d[i][0], '\t', d[i][1], '\t\t', c)

#Program 4: Modifying Student Dictionary

d = {}

n = int(input("Enter limit"))

for a in range(n):

k = int(input("Enter Enroll number(key)"))

t = []

t.append(input("Enter student name"))


t.append(input("Mobile number"))

t.append(input("Enter std"))

t.append(input("Enter section"))

d[k] = t

print("Enroll\t name\tMobile\tstd\tsection\n")

for ke, val in d.items():

print(ke, '\t', val[0], '\t', val[1], '\t', val[2], '\t', val[3])

print("Enter Enroll to search, mobile to replace")

en = int(input("Enter Enroll number"))

mno = input("Mobile number")

for ke, val in d.items():

if en == ke:

val[1] = mno

break

else:

print("Enroll not found!")

print("Modified dict", d)

#Program 5: Election Details

d = {}

while True:

print("a. Create a dictionary")

print("b. Ascending order of names")

print("c. Descending order of votes")

print("d. Exit")

cho = input("Enter the choice")


if cho.lower() == 'a':

n = int(input("Enter limit"))

for a in range(n):

k = input("Enter candidate's name (key)")

vot = int(input("Enter number of votes received"))

d[k] = vot

print("Given dictionary ", d)

elif cho.lower() == 'b':

if any(d):

print("\nAscending order of candidates' names:")

for ke in sorted(d.keys()):

print(ke, d[ke])

else:

print("Error!!! Create a dictionary using choice A")

elif cho.lower() == 'c':

if any(d):

print("\nDescending order of votes received:")

for ke in sorted(d, key=d.get, reverse=True):

print(d[ke])

else:

print("\nError!!! Create a dictionary using choice A")

elif cho.lower() == 'd':

break
else:

print("\nInvalid choice!!!")

Output : for all 5 programs

Enter Stringscroll lock

Output: {'s': 1, 'c': 2, 'r': 1, 'o': 2, 'l': 3, ' ': 1, 'k': 1}

Enter the limit5

Enter key1

Enter value10

Enter key'a'

Enter value20

Enter key1.5

Enter value30

Enter keyTrue

Enter value40

Enter key(1,2)

Enter value50

{'1': '10', "'a'": '20', '1.5': '30', 'True': '40', '(1,2)': '50'}

No keys have the same values

Enter the limit3

Enter code as key name, salamt as values

Enter code1

Enter namea

Enter sales amount12500

Enter code2

Enter nameb
Enter sales amount25700

Enter code3

Enter namec

Enter sales amount8500

CODE NAME SALESAMOUNT COMMISSION

1 a 12500.0 1875.0

2 b 25700.0 3855.0

3 c 8500.0 0

Enter limit3

Enter Enroll number(key)1

Enter student namea

Mobile number123456789

Enter std10

Enter sectionc

Enter Enroll number(key)2

Enter student nameb

Mobile number987654321

Enter std11

Enter sectiond

Enter Enroll number(key)3

Enter student namec

Mobile number56431289

Enter std11

Enter sectiont

Enroll name Mobile std section

1 a 123456789 10 c
2 b 987654321 11 d

3 c 56431289 11 t

Enter Enroll to search, mobile to replace

Enter Enroll number4

Mobile number098712

Enroll not found!

Modified dict {1: ['a', '123456789', '10', 'c'], 2: ['b', '987654321', '11', 'd'], 3: ['c', '56431289', '11', 't']}

a. Create a dictionary

b. Ascending order of names

c. Descending order of votes

d. Exit

Enter the choicea

Enter limit2

Enter candidate's name (key)a

Enter number of votes received12

Enter candidate's name (key)b

Enter number of votes received78

Given dictionary {'a': 12, 'b': 78}

a. Create a dictionary

b. Ascending order of names

c. Descending order of votes

d. Exit

Enter the choiceb

Ascending order of candidates' names:

a 12

b 78
a. Create a dictionary

b. Ascending order of names

c. Descending order of votes

d. Exit

Enter the choicec

Descending order of votes received:

78

12

a. Create a dictionary

b. Ascending order of names

c. Descending order of votes

d. Exit

Enter the choiced

You might also like