Professional Documents
Culture Documents
List1: # List2 List (Set (List1) ) # List2.sort # Print ("The Second Higest in List",list1 (-1) )
List1: # List2 List (Set (List1) ) # List2.sort # Print ("The Second Higest in List",list1 (-1) )
Assignment – 2
Name – ABHINAY SHARMA
Enrollement No. - 04150404421
MCA
2. list1 =[2,4,5,6,5,9,8]
3.
4. # list2= list(set(list1))
5. # list2.sort()
6.
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 :-
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:-
rev = reversed(Name)
if list(Name) == list(rev):
print("paliendrome")
else:
print("Not paliendrome")
O/P:-
sl= string.split()
if sub_string in sl:
print("yes")
else:
print("No")
O/P:-
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)
temp = ""
for i in ls:
temp = temp + i
print(temp)
O/P:-
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:-
O/P :-
15. Write a program to compare two files and display total number of lines in a file.
from difflib import Differ
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 :-