Program 1
Code:
list1=[]
if not list1:
print("list is empty")
flag=0
list1=[1,2,3,4,5]
list2=[5,4,7,8,9]
for x in list1:
for y in list2:
if x==y:
flag=1
print(x)
if (flag==0):
print("no common")
else:
print("common")
Output:
Program 2
Code:
line=input("enter line----->")
x=[Link]()
count=0
for i in x:
if len(i) >=5and i[1]=='b':
print(i)
count=count+1
print(count)
output:
Program 3
Code:
list1=[]
value=int(input("Enter a List Size:--"))
for i in range (0,value):
number=int(input("Enter a number:--"))
[Link](number)
print("The maximum value is", max(list1))
print("The minimum value is", min(list1))
Output:
Program 4
Code:
t1 = tuple()
n = int (input("Total no of values in First tuple: "))
for i in range(n):
a = input("Enter Elements : ")
t1 = t1 + (a,)
t2 = tuple()
m = int (input("Total no of values in Second tuple: "))
for i in range(m):
a = input("Enter Elements : ")
t2 = t2 + (a,)
print("First Tuple : ")
print(t1)
print("Second Tuple : ")
print(t2)
t1,t2 = t2, t1
print("After Swapping: ")
print("First Tuple : ")
print(t1)
print("Second Tuple : ")
print(t2)
Output:
Program 5
Code:
record = dict ()
i=1
n= int (input ("How many records u want to enter: "))
while(i<=n):
Adm = input("Enter Admission number: ")
roll = input("Enter Roll Number: ")
name = input("Enter Name :")
perc = float(input("Enter Percentage : "))
t = (roll,name, perc)
record[Adm] = t
i=i+1
Nkey = [Link]()
for i in Nkey:
print("\nAdmno- ", i, " :")
r = record[i]
print("Roll No\t", "Name\t", "Percentage\t")
for j in r:
print(j, end = "\t")
Output:
Program 6
str1=input('enter string')
print('string;',str1)
a=str()
b=str()
c=str()
for i in str1:
if [Link]():
a+=i
elif [Link]():
b+=i
elif [Link]():
c+=i
str1=a+b+c
print('modified string',str1)
Output:
Program 7
Code:
filein = open("[Link]",'r')
for line in filein:
word= line .split()
for w in word:
print(w + '#',end ='')
print()
[Link]()
Output:
Program 8
Code:
filein = open("[Link]",'r')
line = [Link]()
count_vow = 0
count_con = 0
count_low = 0
count_up = 0
count_digit = 0
count_other = 0
print(line)
for ch in line:
if [Link]():
count_up +=1
if [Link]():
count_low += 1
if ch in 'aeiouAEIOU':
count_vow += 1
if [Link]():
count_con += 1
if [Link]():
count_digit += 1
if not [Link]() and ch !=' ' and ch !='\n':
count_other += 1
print("Digits",count_digit)
print("Vowels: ",count_vow)
print("Consonants: ",count_con-count_vow)
print("Upper Case: ",count_up)
print("Lower Case: ",count_low)
print("other than letters digit: ",count_other)
[Link]()
Output:
Program 9
Output:
str1=input('enter string')
print('string;',str1)
a=str()
b=str()
c=str()
for i in str1:
if [Link]():
a+=i
elif [Link]():
b+=i
elif [Link]():
c+=i
str1=a+b+c
print('modified string',str1)
Output:
Program 10
Code:
def get_longest_line(filename):
large_line = ''
large_line_len = 0
with open(filename, 'r') as f:
for line in f:
if len(line) > large_line_len:
large_line_len = len(line)
large_line = line
return large_line
filename = input('Enter text file Name: ')
print (get_longest_line(filename+".txt"))
Output:
Program 11
Code:
import pickle
def write():
f=open("[Link]","wb")
while True:
r=int(input("enter roll no. : "))
n=input("enter name: ")
data=[r,n]
[Link](data,f)
ch=input("more? (Y/N)")
if ch in 'Nn':
break
[Link]()
def search():
rollno=int(input("enter roll no to display the name: "))
f=open("[Link]","rb")
try:
while True:
rec=[Link](f)
if rec[0]==rollno:
print(rec[1])
except EOFError:
[Link]()
write()
search()
Output
Program 12
Code:
import pickle
def Writerecord(sroll,sname,sperc,sremark):
with open ('[Link]','ab') as Myfile:
srecord={"SROLL":sroll,"SNAME":sname,"SPERC":sperc,
"SREMARKS":sremark}
[Link](srecord,Myfile)
def Readrecord():
with open ('[Link]','rb') as Myfile:
print("\n-------DISPALY STUDENTS DETAILS--------")
print("\nRoll No.",' ','Name','\t',end='')
print('Percetage',' ','Remarks')
while True:
try:
rec=[Link](Myfile)
print(' ',rec['SROLL'],'\t ' ,rec['SNAME'],'\t ',end='')
print(rec['SPERC'],'\t ',rec['SREMARKS'])
except EOFError:
break
def Input():
n=int(input("How many records you want to create :"))
for ctr in range(n):
sroll=int(input("Enter Roll No: "))
sname=input("Enter Name: ")
sperc=float(input("Enter Percentage: "))
sremark=input("Enter Remark: ")
Writerecord(sroll,sname,sperc,sremark)
def Modify(roll):
with open ('[Link]','rb') as Myfile:
newRecord=[]
while True:
try:
rec=[Link](Myfile)
[Link](rec)
except EOFError:
break
found=1
for i in range(len(newRecord)):
if newRecord[i]['SROLL']==roll:
name=input("Enter Name: ")
perc=float(input("Enter Percentage: "))
remark=input("Enter Remark: ")
newRecord[i]['SNAME']=name
newRecord[i]['SPERC']=perc
newRecord[i]['SREMARKS']=remark
found =1
else:
found=0
if found==0:
print("Record not found")
with open ('[Link]','wb') as Myfile:
for j in newRecord:
[Link](j,Myfile)
def main():
while True:
print('\nYour Choices are: ')
print('[Link] Records')
print('[Link] Records')
print('[Link] Records')
print('[Link] (Enter 0 to exit)')
ch=int(input('Enter Your Choice: '))
if ch==1:
Input()
elif ch==2:
Readrecord()
elif ch==3:
r =int(input("Enter a Rollno to be update: "))
Modify(r)
else:
break
main()
Output:
Program 13
Code:
f1 = open("[Link]")
f2 = open("[Link]","w")
for line in f1:
if 'a' not in line:
[Link](line)
print('## File Copied Successfully! ##')
[Link]()
[Link]()
f2 = open("[Link]","r")
print([Link]())
Output:
Program 14
Code:
import csv
with open('Student_Details.csv','w',newline='') as csvf:
writecsv=[Link](csvf,delimiter=',')
choice='y'
while [Link]()=='y':
rl=int(input("Enter Roll No.: "))
n=input("Enter Name: ")
p=float(input("Enter Percentage: "))
r=input("Enter Remarks: ")
[Link]([rl,n,p,r])
print(" Data saved in Student Details file..")
choice=input("Want add more record(y/n).....")
with open('Student_Details.csv','r',newline='') as fileobject:
readcsv=[Link](fileobject)
for i in readcsv:
print(i)
Output:
Program 15
Code:
import csv
number = input('Enter number to find: ')
found=0
with open('Student_Details.csv') as f:
csv_file = [Link](f, delimiter=",")
#loop through csv list
for row in csv_file:
if number ==row[0]:
print (row)
found=1
else:
found=0
if found==1:
pass
else:
print("Record Not found")
Output:
Program 16
Code:
host = []
ch = 'y'
def push(host):
hn = int(input("enter hostel number:"))
ts = int(input("enter total student number:"))
tr = int(input("enter total rooms:"))
temp = [hn,ts,tr]
[Link](temp)
def pop(host):
if (host==1):
print("No record")
else:
print("Deleted record is", [Link]())
def display(host):
print("Hostel Number\tTotal Students\tTotal Rooms")
for i in reversed (host):
print(ifO], " Elt", i[1], "It|", i(2])
while ch in 'yY':
print("1. Add record")
print("2. Delete record")
print("3. Display record")
print("4. Exit")
op = int(input("enter your choice:"))
if op==1:
push(host)
elif op==2:
pop(host)
elif op==3:
display(host)
elif op==4:
break
ch = input("Do you want to enter more?(y/n):")
Output:
Program 17
Code:
L = [1, 2, 'amit', 5, 'list']
def double(L):
for i in range(len (L)):
if str(L[il).isdigit0:
L[]=L[]*3
else:
LO]=L[]*2
print(L)
double(L)
OUTPUT :