Gr 11 Programs to Learn.pdf
Gr 11 Programs to Learn.pdf
1 WAP to accept 10 numbers and find the maximum value entered by the user. It should
display the maximum number after accepting 10 values.
lar=0
print('Enter 10 numbers')
for i in range(1,11):
n=int(input())
if i==1:
lar=n # store the first number
elif n>lar: # compare the previous stored number with the latest entered number
lar=n
print('Largest Number =',lar)
elif i.isalpha():
s1+=i
print(s1)
print(sum)
Output:
Enter the stringabc213
abc
6
14 WAP to input a string and print each individual word of it along with its length.
strg=input(“Enter a string”)
strlst=strg.split()
for word in stlst:
print(word,”(“,len(word),”)
15 Write a program to input a line of text and print the biggest word (length wise) from it.
str = input("Enter a string: ")
words = str.split()
longWord = ''
for w in words :
if len(w) > len(longWord) :
longWord = w
num_of_students = 5
tup = ()
for i in range(num_of_students):
print("Enter the marks of student", i + 1)
m1 = int(input("Enter marks in first subject: "))
m2 = int(input("Enter marks in second subject: "))
m3 = int(input("Enter marks in third subject: "))
tup = tup + ((m1, m2, m3),)
print()
print("Nested tuple of student data is:", tup)
27 WAP to Count frequencies of all elements given in tuple and print in the form of dictionary
my_tuple = (1, 2, 3, 2, 4, 2, 5)
frequency = Counter(my_tuple)
print("Frequency of all elements:")
for element, count in frequency.items():
print("{element}: {count}")
28 WAP secondlargest(t) which takes a input as tuple and returns the second largest element in the
tuple.
t=(23,45,34,66,77,67,70)
maxvalue=max(t)
length=len(t)
secmax=0
for a in range(length):
if secmax<t[a]<maxvalue:
secmax=t[a]
print("Second largest value is:",secmax)
29 WAP to check if a tuple contains any duplicate elements.
tup=eval(input(“Enter a tuple”))
for e in tup:
if tup.count(e)>1:
print(“contains duplicate elements”)
break
Dictionary programs
30 Write a Python program to create a dictionary from a string.
#Note: Track the count of the letters from the string.
#Sample string : 'w3resource'
#Expected output : {'3': 1, 's': 1, 'r': 2, 'u': 1, 'w': 1, 'c': 1, 'e': 2, 'o': 1}
'''st = input("Enter a string: ")
dic = {}
#creates an empty dictionary
for ch in st:
if ch in dic:
#if next character is already in the dictionary
dic[ch] += 1
else:
#if ch appears for the first time
dic[ch] = 1
#Printing the count of characters in the string
print(dic)'''
31 write a program in ptyhon to remove
#the duplicate values form the dcitionary
#original dictionary={1:"aman",2:"Suman",3:"Aman"}
#new dictionary={1:"aman",2:"Suman"}
d1={1:"Aman",2:"Suman",3:"Aman"}
nd1={}
#s=d1.values() #elements giving in the list of tuples
for k,v in d1.items():
print(k,v)
if v not in nd1.values():
nd1[k]=v
print(nd1)
32 find the maximum and minimum element in the dictionary
dic = {"A":12,"B":13,"C":9,"D":89,"E":34,"F":17,"G":65,"H":36,"I":25,"J":11}
lst=list()
ic = {"A": 12, "B": 13, "C": 9, "D": 89, "E": 34,
for a in dic.values(): "F": 17, "G": 65, "H": 36, "I": 25, "J": 11}
print(a) lst = list()
for i in ic.values(): # Corrected `dic` to `ic`
lst.append(a) print(i)
lst.sort() lst.append(i)
lst.sort()
print(lst) print(lst)
print(lst[-1]) print(lst[-1]) # Largest value
print(lst[-2]) # Second largest value
print(lst[-2])
33 WAP to create the name with their salary
'''rec=int(input("enter the total no of records:"))
i=1
emp=dict()
while i<=rec:
name=input("enter the name of the employee")
salary=int(input("enter the salary"))
emp[name]=salary
i=i+1
print("\nEmployee\t Salary")
for k in emp:
print(k,':',emp[k])'''
34 WAP to show the frequency of each character in the input string "programming"
str=input("enter a string") #input
dict1={} #empty dictionary
for ch in str: #storing in ch
if ch in dict1: #checking the condition
dict1[ch]+=1 #starts counting
else:
dict1[ch]=1 #appears one time
for key in dict1: #display the output the form of dictionary
print(key,':',dict1[key])
35 A dictionary D1 has values in the form of list of numbers. Write a program to create a new
dictionary D2 having same keys as D1 but values as the sum of the list elements
ex:
D1={‘A’:[1,2,3],’B’,[4,5,6]}
then
D2={‘A’:6,’B’:15}
Ans:
D1 = eval(input("Enter a dictionary D1: "))
print("D1 =", D1)
D2 = {}
for key in D1:
num = sum(D1[key])
D2[key] = num
print(D2)