Computer Science Programming File
Computer Science Programming File
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=line.split()
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:--"))
list1.append(number)
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 = record.keys()
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 i.islower():
a+=i
elif i.isupper():
b+=i
elif i.isdigit():
c+=i
str1=a+b+c
print('modified string',str1)
Output:
Program 7
Code:
filein = open("rishi.txt",'r')
for line in filein:
word= line .split()
for w in word:
print(w + '#',end ='')
print()
filein.close()
Output:
Program 8
Code:
filein = open("rishi.txt",'r')
line = filein.read()
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 ch.isupper():
count_up +=1
if ch.islower():
count_low += 1
if ch in 'aeiouAEIOU':
count_vow += 1
if ch.isalpha():
count_con += 1
if ch.isdigit():
count_digit += 1
if not ch.isalnum() 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)
filein.close()
Output:
Program 9
Output:
str1=input('enter string')
print('string;',str1)
a=str()
b=str()
c=str()
for i in str1:
if i.islower():
a+=i
elif i.isupper():
b+=i
elif i.isdigit():
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
return large_line
Output:
Program 11
Code:
import pickle
def write():
f=open("studentdetails.dat","wb")
while True:
r=int(input("enter roll no. : "))
n=input("enter name: ")
data=[r,n]
pickle.dump(data,f)
ch=input("more? (Y/N)")
if ch in 'Nn':
break
f.close()
def search():
rollno=int(input("enter roll no to display the name: "))
f=open("studentdetails.dat","rb")
try:
while True:
rec=pickle.load(f)
if rec[0]==rollno:
print(rec[1])
except EOFError:
f.close()
write()
search()
Output
Program 12
Code:
import pickle
def Writerecord(sroll,sname,sperc,sremark):
with open ('StudentRecord.dat','ab') as Myfile:
srecord={"SROLL":sroll,"SNAME":sname,"SPERC":sperc,
"SREMARKS":sremark}
pickle.dump(srecord,Myfile)
def Readrecord():
with open ('StudentRecord.dat','rb') as Myfile:
print("\n-------DISPALY STUDENTS DETAILS--------")
print("\nRoll No.",' ','Name','\t',end='')
print('Percetage',' ','Remarks')
while True:
try:
rec=pickle.load(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 ('StudentRecord.dat','rb') as Myfile:
newRecord=[]
while True:
try:
rec=pickle.load(Myfile)
newRecord.append(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 ('StudentRecord.dat','wb') as Myfile:
for j in newRecord:
pickle.dump(j,Myfile)
def main():
while True:
print('\nYour Choices are: ')
print('1.Insert Records')
print('2.Dispaly Records')
print('3.Update Records')
print('0.Exit (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("rishi.txt")
f2 = open("copyrishi.txt","w")
for line in f1:
if 'a' not in line:
f2.write(line)
print('## File Copied Successfully! ##')
f1.close()
f2.close()
f2 = open("copyrishi.txt","r")
print(f2.read())
Output:
Program 14
Code:
import csv
with open('Student_Details.csv','w',newline='') as csvf:
writecsv=csv.writer(csvf,delimiter=',')
choice='y'
while choice.lower()=='y':
rl=int(input("Enter Roll No.: "))
n=input("Enter Name: ")
p=float(input("Enter Percentage: "))
r=input("Enter Remarks: ")
writecsv.writerow([rl,n,p,r])
print(" Data saved in Student Details file..")
choice=input("Want add more record(y/n).....")
Output:
Program 15
Code:
import csv
number = input('Enter number to find: ')
found=0
with open('Student_Details.csv') as f:
csv_file = csv.reader(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]
host.append(temp)
def pop(host):
if (host==1):
print("No record")
else:
print("Deleted record is", host.pop())
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 :