PROGRAM 11
Q . Input a string and check whether it contains digit
or not
Code –
s=input("Enter a string:")
test=False
dig="0123456789"
for ch in s:
if ch in dig:
print("The string contains a digit.")
test=True
break
if test==False:
print("The string doesn't contain a digit.")
PROGRAM 11
Output -
Enter a string:2fgdh
The string contains a digit.
Enter a string:ghfsj
The string doesn't contain a digit
PROGRAM 12
Q2. Write a program that inputs a line of text and
prints its each word in a separate line. Also, print the
count of words in the line
Code -
s=input("Enter a string:")
count=0
for word in s.split():
print(word)
count+=1
print("Total words:",count)
PROGRAM 12
Output -
Enter a string: yash namdev mera naam
yash
namdev
mera
naam
Total words: 4
PROGRAM 13
Q3. Write a program that displays options for
inserting or deleting elements in a list.
Code –
val=[17,23,18,19]
print("The list is:",val)
while True:
print("Main menu")
print("1. Insert")
print("2. Delete")
print("3. Exit")
ch=int(input("Enter your choice 1/2/3:"))
if ch==1:
item=int(input("Enter item:"))
pos=int(input("Insert at which position"))
index=pos-1
val.insert(index,item)
print("Success! List now is:",val)
elif ch==2:
print("Deletion menu")
print("1. Delete using value")
print("2. Delete using index")
print("3. Delete a sublist")
dch=int(input("Enter choice(1/2/3):"))
if dch==1:
item=int(input("Enter item to be deleted"))
val.remove(item)
print("List now is : ", val)
elif dch == 2:
index= int(input("Enter index of item to be deleted :"))
val.pop(index)
print("List now is : ", val)
elif dch == 3:
l = int(input("Enter lower limit of list slice to be deleted:"))
h = int(input("Enter upper limit of list slice tobe deleted:"))
del val[1:h]
print("List now is : ", val)
else:
print("valid choices are 1/2/3 only.")
elif ch == 3:
break
else:
print("valid choices are 1/2/3 only.")
PROGRAM 13
Output
The list is: [17, 23, 18, 19]
Main menu
1. Insert
2. Delete
3. Exit
Enter your choice 1/2/3:1
Enter item:55
Insert at which position 4
Success! List now is: [17, 23, 18, 55, 19]
Main menu
1. Insert
2. Delete
3. Exit
Enter your choice 1/2/3:2
Deletion menu
1. Delete using value
2. Delete using index
3. Delete a sublist
Enter choice(1/2/3):1
Enter item to be deleted 17
List now is : [23, 18, 55, 19]
Main menu
1. Insert
2. Delete
3. Exit
Enter your choice 1/2/3:2
Deletion menu
1. Delete using value
2. Delete using index
3. Delete a sublist
Enter choice(1/2/3):2
Enter index of item to be deleted :1
List now is : [23, 55, 19]
Main menu
1. Insert
2. Delete
3. Exit
Enter your choice 1/2/3:2
Deletion menu
1. Delete using value
2. Delete using index
3. Delete a sublist
Enter choice(1/2/3):3
Enter lower limit of list slice to be deleted:1
Enter upper limit of list slice to be deleted:4
List now is : [23]
Main menu
PROGRAM 14
Q4. Write a program to find the minimum element
from a list of element along with its index in the list.
Code-
l=eval(input("Enter list:"))
length=len(l)
min_ele=l[0]
min_index=0
for i in range(1,length):
if l[i]<min_ele:
min_ele=l[i]
min_index=i
print("Given list is",l)
print("The minimum element of the given list is:")
print(min_ele,"at index",min_index)
PROGRAM 14
Output –
Enter list:[2,3,4,-2,6,-7,8,11,-9,11]
Given list is [2, 3, 4, -2, 6, -7, 8, 11, -9, 11]
The minimum element of the given list is:-9 at index 8
PROGRAM 15
Q5. Write a program to check if the elements in the
first half of a tuple are sorted in ascending order or
not.
Code –
tup = eval(input("Enter a tuple: "))
ln = len(tup)
mid = ln//2
if ln % 2 == 1 :
mid = mid+1
half = tup[:mid]
if sorted(half) == list(tup[:mid]):
print("First half is sorted")
else:
print("First half is not sorted")
PROGRAM 15
Output –
Enter a tuple: 11,12,13,14,10
First half is sorted
Enter a tuple: 11,22,13,14,10
First half is not sorted
PROGRAM 16
A tuple stores marks of a student in 5 subjects. Write
a program to calculate the grade of the student as
per the following:
Average grade
>= 75 ‘A’
60-74.999 ‘B’
50-59.999 ‘C‘
< 50 ‘D’
Code –
mks = eval(input("Enter marks tuple: "))
total = sum(mks)
avg = total /5
if avg >= 75:
grade = 'A'
elif avg >= 60:
grade = 'B'
elif avg >= 50:
grade = 'C'
else:
grade = 'D'
print("Total Marks: ", total, "Grade:", grade)
PROGRAM 16
Output –
Enter marks tuple: 78.5,67.8,89.9,70.5,50
Total Marks: 356.7 Grade: B
PROGRAM 17
Write a program to read a sentence and then create
a dictionary contains the frequency of letters and
digits in the sentence. Ignore other symbols, if any.
Code –
sen = input("Enter a sentence :")
sen=sen.lower()
alphabet_digits = 'abcdefghijklmnopqrstuvwxyz0123456789'
char_count = {}
print("Total characters in the sentence are : ", len (sen))
for char in sen:
if char in alphabet_digits: #ignore any punctuation etc
if char in char_count:
char_count [char] = char_count [char] + 1
else:
char_count [char] = 1
print(char_count)
PROGRAM 17
Output –
Enter a sentence : hello there! class 10 is done and
now you are in class 11.
Total characters in the sentence are : 58
{'h': 2, 'e': 5, 'l': 4, 'o': 4, 't': 1, 'r': 2, 'c': 2, 'a': 4, 's': 5,
'1': 3, ' 0': 1, 'i': 2, 'd': 2, 'n': 4, 'w': 1, 'y': 1, 'u': 1}
PROGRAM 18
Write a program to input your friends' names and
their Phone Numbers and store them in the
dictionary as key-value pair.
Code –
n = int (input("How many friends?"))
fd = {}
for i in range(n):
print("Enter details of friend", (i + 1))
name = input("Name:")
ph = int(input("Phone :"))
fd[name] = ph
print("Friends dictionary is ", fd)
ch = 0
while ch != 7:
print("\tMenu")
print("1. Display all friends")
print("2. Add new friend")
print("3. Delete a friend")
print("4. Modify a phone number")
print("5. Search for a friend")
print("6. Sort on names")
print("7. Exit")
ch = int(input("Enter your choice (1..7):"))
if ch ==1:
print(fd)
elif ch == 2:
print("Enter details of new friend")
name = input("Name:")
ph = int(input("Phone :"))
fd[name] = ph
elif ch == 3:
nm = input("Friend Name to be deleted:")
res=fd.pop(nm, -1)
if res != -1:
print (res, "deleted")
else:
print("No such friend")
elif ch == 4:
name = input("Friend Name: ")
ph = int(input("changed Phone :"))
fd[name] = ph
elif ch == 5:
name = input("Friend Name: ")
if name in fd :
print(name, "exists in the dictionary.")
else:
print(name, "does not exist in the dictionary.")
elif ch == 6:
lst=sorted(fd)
print("{", end = " ")
for a in lst :
print (a, ":", fd[a], end =" ")
print("}")
elif ch == 7:
break
else:
print("Valid choices are 1..")
PROGRAM18
Output –
How many friends?2
Enter details of friend 1
Name:Garry
Phone :8856725509
Friends dictionary is {'Garry': 8856725509}
Enter details of friend 2
Name:Rushi
Phone :7890334503
Friends dictionary is {'Garry': 8856725509, 'Rushi': 7890334503}
Menu
1. Display all friends
2. Add new friend
3. Delete a friend
4. Modify a phone number
5. Search for a friend
6. Sort on names
7. Exit
Enter your choice (1.7):1
{'Garry': 8856725509, 'Rushi': 7890334503}
Menu
1. Display all friends
2. Add new friend
3. Delete a friend
4. Modify a phone number
5. Search for a friend
6. Sort on names
7. Exit
Enter your choice (1.7):2
Enter details of new friend
Name:Manny
Phone :6785994739
Menu
1. Display all friends
2. Add new friend
3. Delete a friend
4. Modify a phone number
5. Search for a friend
6. Sort on names
7. Exit
Enter your choice (1.7):3
Friend Name to be deleted:Garry
8856725509 deleted
Menu
1. Display all friends
2. Add new friend
3. Delete a friend
4. Modify a phone number
5. Search for a friend
6. Sort on names
7. Exit
Enter your choice (1.7):4
Friend Name: Rushi changed Phone :7787938632
Menu
1. Display all friends
2. Add new friend
3. Delete a friend
4. Modify a phone number
5. Search for a friend
6. Sort on names
7. Exit
Enter your choice (1.7):5
Friend Name: Manny
Manny exists in the dictionary.
Menu
1. Display all friends
2. Add new friend
3. Delete a friend
4. Modify a phone number
5. Search for a friend
6. Sort on names
7. Exit
Enter your choice (1.7):6
{ Manny : 6785994739 Rushi : 7787938632 }
Menu
1. Display all friends
2. Add new friend
3. Delete a friend
4. Modify a phone number
5. Search for a friend
6. Sort on names
7. Exit
Enter your choice (1.7):7
PROGRAM 19
Write a program to create a dictionary with the roll
number, name and marks of n students in a class
and display the names of students who have marks
above 75.
Code –
n = int(input("How many Students?"))
stu = {}
for i in range(1, n+1):
print("Enter details of Student", (i))
rollno= int(input("Roll number :"))
name =input("Name :")
marks = float(input("Marks :"))
d = {"Roll_no": rollno, "Name": name,"Marks": marks}
key="Stu" + str(i) stu[key] = d
print("Students with marks > 75 are:")
for i in range(1, n+1):
key = "Stu" + str(i)
if stu[key]["Marks"] >= 75:
print(stu[key])
PROGRAM 19
Output –
How many Students?3
Enter details of Student 1
Roll number :10
Name :Sam Marks :65
Enter details of Student 2
Roll number :11
Name :Tom
Marks :85
Enter details of Student 3
Roll number :12
Name :Fred
Marks :91
Students with marks > 75 are:
{'Roll_no': 11, 'Name': 'Tom', 'Marks': 85.0}
{'Roll_no': 12, 'Name': 'Fred', 'Marks': 91.0}
PROGRAM 20
Write a program to input a string and print number
of upper- and lower-case letters in it.
Code –
str1 = input("Enter a string: ")
ucase, lcase = 0,0
for ch in str1:
if ch >= 'A' and ch <= 'Z':
ucase += 1
if ch>= 'a' and ch <= 'z':
lcase += 1
print("No. of uppercase letters: ", ucase)
print("No. of lowercase letters: ", lcase)
PROGRAM 20
Output –
Enter a string: METamorPHosIS
No. of uppercase letters: 7
No. of lowercase letters: 6