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

Python Lab

Assignment – 2
Name – ABHINAY SHARMA
Enrollement No. - 04150404421
MCA

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

2. list1 =[2,4,5,6,5,9,8]
3.

4. # list2= list(set(list1))
5. # list2.sort()
6.

7. # print("The second higest in


list",list1[-1])
8.

9. list2=set(list1)
10. list2.remove(max(list2))

11. print(max(list2))

O/P : -
2. Write a Python code to delete all odd numbers and negative numbers from a given
numeric list.

list1=[1,34,5,6,7,8,-1,-5,-7]
# print("the list",list1)
# list2 = list(filter(lambda
x:x>0,list1))
# print("remove after negative",list2)
for i in list(list1):
if(i%2!=0 or i<0):
list1.remove(i)
print("even number",(list1))
O/P :-

3. Python Program to Find Element Occurring Odd Number of Times in a List

list1=[1,2,3,4,5,6,7,3]

print(list1)

for i in list(list1):
if(i%2==0):
list1.remove(i)
print("odd_occurance number",len(list1))
O/P:-

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

Name = input("Enter the string \n")

rev = reversed(Name)

if list(Name) == list(rev):
print("paliendrome")
else:
print("Not paliendrome")
O/P:-

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

string = 'Hello , How are you'


sub_string = 'How'

sl= string.split()
if sub_string in sl:
print("yes")
else:
print("No")
O/P:-

6. Write a program to determine whether a given string has balanced parenthesis


or not.
def isbalanced(s):

c= 0
ans=False
for i in s:
if i == "(":
c += 1
elif i == ")":
c-= 1
if c < 0:
return ans
if c==0:
return not ans
return ans
s="{[]}"
print("Given string is balanced :",isbalanced(s))

O/P : -

7. Display Letters which are in the First String but not in the Second

s1= 'hello'
s2='howdoudo'

sets1=set(s1)
sets2=set(s2)

result=(sets2-sets1)

print(result)

O/P :-

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.
s = input("Enter a string: ")

ls = list(s)

for i in range(1, len(s),2):


ls[i] = ls[i].capitalize()

temp = ""

for i in ls:

temp = temp + i

print(temp)

O/P:-

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


s={1:2.3,2:4.5,3:9,4:8}
print("The Dictionary")
print(s)

key=input("Enter the value(1:4):")


if int(key) in s:
del s[int(key)]
else:
print("Key not found")
exit(0)

print("Update Dictionary")
print(s)

O/P:-
10. Python Program to Count the Frequency of Words Appearing in a String Using
a Dictionary
my_string = input("Enter The string : ")

a=[]

a=my_string.split()

count=[a.count(i) for i in a ]

print (dict(zip(a,count)))

O/P:-

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.
dicti_stu={101:{'Rollno':1, 'Name':'Sandeep',
'Marks':95},
102:{'Rollno':2, 'Name':'Siddhart',
'Marks':90},
103:{'Rollno':3, 'Name':'Deversh',
'Marks':92}}
for key,val in dicti_stu.items():
print(key,val)

O/P:-

12. Python Program to Find the Total Sum of a Nested List Using Recursion
def recursion_sum(my_list):
total = 0
for elem in my_list:
if (type(elem) == type([])):
total = total + recursion_sum(elem)
else:
total = total + elem
return total
my_list = [[2,3], [7,9], [11,45], [78,98]]
print("The list elements are :")
print(my_list)
print( "The sum is :")
print(recursion_sum(my_list))

O/P:-

13. Python Program to Read a String from the User and Append it into a File
fname = input("Enter file name: ")

file3=open(fname,"a")
c=input("Enter string to append: \n");
file3.write("\n")
file3.write(c)
file3.close()
print("Contents of appended file:");
file4=open(fname,'r')
line1=file4.readline()
while(line1!=""):
print(line1)
line1=file4.readline()
file4.close()

O/P:-

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


fname = input("Enter file name: ")
word=input("Enter word to be searched:")
k = 0

with open(fname, 'r') as f:


for line in f:
words = line.split()
for i in words:
if(i==word):
k=k+1
print("Occurrences of the word:")
print(k)

O/P :-

15. Write a program to compare two files and display total number of lines in a file.
from difflib import Differ

with open('file3.txt') as file_1, open('fname.txt')


as file_2:
differ = Differ()

for line in differ.compare(file_1.readlines(),


file_2.readlines()):
print(line)
O/P : -

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

list=[2,7,4,9,3,1]
temp=2

n=len(list)
index=linear(list,n,temp)

if(index==-1):
print("Element Not Found")

else:
{
print("Element found at ",index)
}

O/P :-

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

N=[10,51,2,18,4,31,5,23,64,29]
print("Before Sorting",N)
for i in range(0,len(N)):
for j in range(0,len(N)-1):
if N[i]<N[j]:
temp=N[i]
N[i]=N[j]
N[j]=temp
print("After Bubble sorting",N)

O/P :-

You might also like