You are on page 1of 9

Assignment – II

1. Program to find the second largest element in a list ‘Num’.


list=[]
n=int(input("Enter the number of elements you want to add"))
for i in range(0,n):
ele=input()
list.append(ele)
print("The entered list is: "+str(list))
list1=list
list1.remove(max(list1))
print("The second max element is "+str(max(list1)))

2. Write a Python code to delete all odd numbers and negative numbers from a given numeric list.
list=[]
n=int(input("Enter the number of elements you want to add "))
for i in range(0,n):
ele=int(input())
list.append(ele)
print("The entered list is: "+str(list))
list1=[]
for i in list:
if i >0:
if i%2==0:
list1.append(i)
print("Modified list is "+str(list1))

Parv Jain Page | 1 09714004422


3. Python Program to Find Element Occurring Odd Number of Times in a List
list=[]
n=int(input("Enter the number of elements you want to add "))
for i in range(0,n):
ele=int(input())
list.append(ele)
print("The entered list is: "+str(list))
for i in list:
if list.count(i)%2!=0:
print("Number of times "+str(i)+" occured is "+str(list.count(i)))

4. Python Program to Check if a String is a Palindrome or Not


str=input("Enter any string ")
if str == str[ : :-1]:
print("Entered string is palindrome")
else:
print("Not Palindrome")

5. Check if the Substring is present in a Given String


st=input("Enter any string ")
ch=input("Enter the substring you want to check ")
if (st.find(ch)!=-1):
print("Substring is present")

Parv Jain Page | 2 09714004422


else:
print("substring is not present")

6. Write a program to determine whether a given string has balanced parenthesis or not.
def bp(st):
s1=[]
for s in st:
if s in ('(', '{', '['):
s1.append(s)
else:
if s1 and ((s1[-1] == '(' and s == ')') or(s1[-1] == '{' and s == '}') or
(s1[-1] == '[' and s == ']')):
s1.pop()
else:
return False
return not s1
st=input("Enter the expression ")
if bp(st):
print("Balanced parenthesis")
else:
print("Not balanced parenthesis")

7. Display Letters which are in the First String but not in the Second
s1= input("Enter string 1")
s2 = input("Enter string 2")
result=set(s1)-set(s2)
print(result)

Parv Jain Page | 3 09714004422


8. Write a program that reads a string and then prints a string that capitalizes every other letter in
the string. E.g., corona becomes cOrOnA.
st=input("Enter any string ")
a=""
for i in range (0,len(st)):
if i%2!=0:
a=a+st[i].upper()
else:
a+=st[i]
print(a)

9. Python Program to Remove the Given Key from a Dictionary


list=[]
n=int(input("Enter the number of elements you want to add"))
for i in range(0,n):
ele=input()
list.append(ele)
print("The entered list is: "+str(list))
list1=[]
n1=int(input("Enter the number of elements you want to add"))
for i in range(0,n1):
ele=input()
list1.append(ele)
print("The entered list is: "+str(list1))

dict={}
for i in range(len(list)):
dict[list[i]]=list1[i]
print("The dictionary formed is "+str(dict))

t=input("Enter the key you want to remove")


del dict[t]
print("The altered dictionary is "+str(dict))

Parv Jain Page | 4 09714004422


10. Python Program to Count the Frequency of Words Appearing in a String Using a Dictionary
s=input("Enter a string")
w=s.split()
d={}
for i in w:
if i in d:
d[i]=d[i]+1
else:
d[i]=1
print(d)

11. WAP to store students information like admission number, roll number, name and marks in a
dictionary and display information on the basis of admission number.
dict={}

num_st=int(input("Enter the number of students "))

for i in range(num_st):
print(f"\nEnter information for student {i+1} ")
adm_no=input("Admission number ")
roll_num=input("Roll number ")
name=input("Name ")
marks=input("Marks ")

dict[adm_no]={"Roll Number":roll_num, "Name":name, "Marks":marks}

adm_no=input("\nEnter admission number of the student to display information ")


if adm_no in dict:
print("\nInformation for the student with Admission number ",adm_no)

Parv Jain Page | 5 09714004422


print("Roll Number ",dict[adm_no]["Roll Number"])
print("Name ", dict[adm_no]["Name"])
print("Marks ", dict[adm_no]["Marks"])
else:
print("Student with Admission Number ",adm_no, "not found")

12. Python Program to Find the Total Sum of a Nested List Using Recursion
def total_sum(nested_list):
total = 0
for item in nested_list:
if isinstance(item, list):
total += total_sum(item)
else:
total += item

Parv Jain Page | 6 09714004422


return total

my_list = [1, 2, [3, 4, [5, 6], 7], 8, [9, 10]]


total = total_sum(my_list)
print("Total sum:", total)

13. Python Program to Read a String from the User and Append it into a File
with open("file.txt","a") as f1:
string = input("Enter a string to append to this file: ")
f1.write(string)
f1.write("\n")
print("String Appended to the file successfully")

14. Python Program to Count the Occurrences of a Word in a Text File


fname=input("Enter the name of the file to search")
word=input("Enter the word to search for")
with open(fname,"r") as f1:
data=f1.read()
count=data.count(word)
print(f"The word '{word}' occurs {count} times in the file '{fname}'.")

Parv Jain Page | 7 09714004422


15. Write a program to compare two files and display total number of lines in a file.
file1 = input("Enter the name of first file: ")
file2 = input("Enter the name of second file: ")

with open(file1, 'r') as f1, open(file2, 'r') as f2:


lines1 = f1.readlines()
lines2 = f2.readlines()

print("Number of lines in", file1, ":", len(lines1))


print("Number of lines in", file2, ":", len(lines2))

if len(lines1) > len(lines2):


print(file1, "has more lines.")
elif len(lines2) > len(lines1):
print(file2, "has more lines.")
else:
print("Both files have the same number of lines.")

16. Accept list and key as input and find the index of the key in the list using linear search
def ls(lst,key):
for i in range(len(lst)):
if lst[i]==key:
return i
return -1

lst=list(map(int,input("Enter the list of numbers ").split()))


key=int(input("Enter the key to search for "))
index=ls(lst, key)

if index != -1:
print(f"The key {key} is found at index {index}")
else:
print(f"The key {key} is not found")

Parv Jain Page | 8 09714004422


17. Write a program to arrange a list on integer elements in ascending order using bubble sort
technique.
# 10, 51, 2, 18, 4, 31, 13, 5, 23, 64, 29

def bs(lst):
n=len(lst)
for i in range(n):
for j in range(0,n-i-1):
if lst[j] > lst[j+1]:
lst[j], lst[j+1] = lst[j+1], lst[j]
return lst

lst=[10, 51, 2, 18, 4, 31, 13, 5, 23, 64, 29]


sorted_list=bs(lst)
print("Sorted list in ascending order ",sorted_list)

Parv Jain Page | 9 09714004422

You might also like