Professional Documents
Culture Documents
Shashank Program File
Shashank Program File
School
Computer Science -
Program file
Class: XIIth
Ans.
Ans.
OUTPUT:
Q3. PROGRAM TO GENERATE RANDOM NUMBERS BETWEEN 1 AND 6
Ans.
import random
while True:
print(random.randint(1,6))
ch=input('Generate more numbers(Y/N):')
if ch in 'Nn':
break
Q4. PROGRAM TO FIND THE SUM OF ELEMENTS IN A LIST
Ans.
l=[]
for i in range(n):
j=int(input('Enter the element:'))
l.append(j)n=int(input('Enter the length of list:'))
s=0
for i in l:
s
s=s+i
print('Sum of the Elements of a List:',s)
OUTPUT:
Q5. PROGRAM TO FIND IF A STRING IS A PALINDROME
Ans.
OUTPUT:
Q6. PROGRAM TO PRINT THE SUM OF N NATURAL NUMBERS
Ans.
OUTPUT:
Q7. WAP TO CALCULATE MEAN
Ans.
l=[]
n=int(input('Enter size of List= '))
for i in range(n):
j=int(input('Enter Element of list= '))
l.append(j)
sum1=0
x=len(l)
OUTPUT:
Q8. PROGRAM TO PRINT THE AREA AND PERIMETER OF POLYGONS BY USER CHOICEAns.
print('****MAIN MENU****')
''')
if n==1:
circumference=2*3.14*r
circlearea=3.14*r**2
print('Circumference=',circumference)
print('Area=',circlearea)
elif n==2:
squareperimeter=4*s
squarearea=s**2
print('Perimeter=',squareperimeter)
print('Area=',squarearea)
elif n==3:
rectangleperimeter=2*(l+b)
rectanglearea=l*b
print('Perimeter=',rectangleperimeter)
print('rectanglearea=',rectanglearea)
elif n==4:
triangleperimeter=triangleside1+triangleside2+trianglebase
trianglearea=1/2*trianglebase*h
print('Perimeter=',triangleperimeter)
print('Area=',trianglearea)
elif n==5:
parallelogramperimeter=2*(parallelogramside+parallelogrambase)
parallelogramarea=parallelogramheight*parallelogrambase
print('Perimeter=',parallelogramperimeter)
print('Area=',parallelogramarea)
else:
print('Invalid Choice')
Q9. Program to read a file line by line.
Ans.
with open('text.txt') as f:
while True:
data=f.readline()
print(data)
ch=input('Want to read next line(Y/N):')
if ch in 'nN':
break
Q10. PROGRAM TO READ ALL LINES FROM THE FILE INTO THE LIST
Ans.
with open('text.txt') as f:
data=f.readlines()
print(data)
Q11. PROGRAM TO READ DATA FROM THE SECOND CHARACTER INTO A LIST
Ans.
with open('text.txt') as f:
data=f.read(1)
data=f.read()
print(list(data))
Q12. Program to write data into a file.
Ans.
with open('text.txt','w') as f:
while True:
n=input('Enter content:')
f.write(n)
ch=input('Want to enter more(Y/N):')
if ch in 'nN':
break
Q13. Program for user to input details into a text file.
Ans.
with open('text.txt','w') as f:
while True:
n=input('Enter name:')
a=input('Enter age:')
f.writelines([n,' ',a])
ch=input('Want to enter more(Y/N):')
if ch in 'nN':
break
Q14. PROGRAM TO CHECK IF THE GIVEN KEY EXISTS IN DICTIONARY
Ans.
d={}
while True:
n=input('Enter key:')
a=input('Enter value:')
d[n]=a
ch=input('Want to enter more(Y/N):')
if ch in 'nN':
break
j=d.keys()
h=input('Enter the Key to check:')
if h in j:
print(h,'key exists in dictionary')
else:
print(h,'key doesn\'t exists in dictionary')
Q15. PROGRAM TO FIND THE SECOND MAXIMUM VALUE IN A DICTIONARY
Ans.
d={}
while True:
n=input('Enter key:')
a=input('Enter value:')
d[n]=a
ch=input('Want to enter more(Y/N):')
if ch in 'nN':
break
j=d.values()
j=sorted(j,reverse=True)
print('second maximum value',j[1])
Q16. PROGRAM TO WRITE INTO A BINARY FILE FROM DICTIONARY
Ans.
import pickle
d={}
while True:
n=input('Enter key:')
a=input('Enter value:')
d[n]=a
ch=input('Want to enter more(Y/N):')
if ch in 'nN':
break
with open('text.dat','wb') as f:
pickle.dump(d,f)
Q17. PROGRAM TO APPEND STUDENT RECORD INTO THE SAME FILE
Ans.
with open('text.txt','a') as f:
while True:
n=input('Enter name:')
a=input('Enter age:')
f.writelines([n,' ',a])
ch=input('Want to enter more(Y/N):')
if ch in 'nN':
break
Q18. PROGRAM TO DISPLAY THE RECORDS STORED
Ans.
with open('text.txt') as f:
data=f.read()
print(data)
OUTPUT:
Q23. PROGRAM TO SORT A LIST BY BUBBLE SORT IN ASCENDING ORDER
Ans.
size=int(input("Enter size of list= "))
l=[]
for i in range (size):
n=int (input("Enter element of list= "))
l.append(n)
print ("Original list:",l)
for n in range (size-1,0,-1):
for i in range(n):
if l[i]>l[i+1]:
l[i],l[i+1]=l[i+1],l[i]
print("Sorted List is:",l)
Q24. PROGRAM TO COUNT THE FREQUENCY OF NUMBER IN A LIST
Ans.
Ans.
t=(t*(9/5))+32
Ans.
Ans.
import mysql.connector as sql
db=sql.connect(host='localhost',user='root',password='paras3105',database='parass')
cur=db.cursor()
while True:
n=input('Enter serial no.:')
na=input('Enter name:')
marks=input('Enter marks:')
cur.execute('insert into parasnew values("{}","{}","{}")'.format(n,na,marks))
db.commit()
ch=input('Want to enter more(Y/N):')
if ch in 'nN':
break
Q30. PROGRAM TO DELETE DATA USING MYSQL-PYTHON INTERFACE
Ans.
import mysql.connector as sql
db=sql.connect(host='localhost',user='root',password='paras3105',database='parass')
cur=db.cursor()
print('MAIN MENU')
print('1.Delete all records')
print('2.Delete all records and Structure')
print('3.Delete the entire database and tables within')
ch=int(input('Enter your choice:'))
if ch==1:
cur.execute('Delete from parasnew')
db.commit()
if ch==2:
cur.execute('drop table new')
db.commit()
if ch==3:
cur.execute('drop database parass')
db.commit()
Output:
Q31. PROGRAM TO DISPLAY DATA USING MYSQL-PYTHON INTERFACE
Ans.
import mysql.connector as sql
db=sql.connect(host='localhost',user='root',password='paras3105',database='parass')
cur=db.cursor()
cur.execute('select * from parasnew')
data=cur.fetchall()
print(data)
OUTPUT:
Q32.Program to write, add client and delete client from a list
Ans.
def ADDCLIENT(CLIENT):
h=input('Enter client name:')
CLIENT.append(h)
print('New client list:',CLIENT)
def DELETECLIENT(CLIENT):
h=input('Enter client name:')
CLIENT.remove(h)
print('NEW client list:',CLIENT)
size=int(input("Enter size of list= "))
CLIENT=[
for i in range (size):
n=input("Enter Clients= ")
CLIENT.append(n)
while True:
print('***MAIN MENU***')
print('1.Add a client')
print('2.Remove a client')
print('3.exit')
ch=int(input('Enter your choice:'))
if ch==1:
ADDCLIENT(CLIENT)
if ch==2:
DELETECLIENT(CLIENT)
if ch==3:
break
Q33. WAP to sort a list by bubble sort in ascending order
Ans.
size=int(input("Enter size of list= "))
l=[]
for i in range (size):
n=int (input("Enter element of list= "))
l.append(n)
print ("Original list:",l)
for n in range (size-1,0,-1):
for i in range(n):
if l[i]>l[i+1]:
l[i],l[i+1]=l[i+1],l[i]
print("Sorted List is:",l)
Q36. Program to check whether a number is Armstrong or not
Ans.
num=int(input("Enter the number= "))
org=num
count=0
while num!=0:
rem=num%10
count+=1
num//=10
num=org
suma=0
while num!=0:
rem=num%10
exp=rem**count
suma+=exp
num//=10
if(suma==org):
print(org,"is an Armstrong")
else:
print(org,"is not an Armstrong")
Q37. Program to print the sum of N natural numbers.
Ans.
num=int(input("Enter the number= "))
s=0
for n in range (1,num+1):
s=s+n
print('Sum of',num,'Natural Numbers is',s)
OUTPUT:
Q38. Program to count lowercase and uppercase letter in an inputted string?
Ans.
n=input('Enter the String= ')
a=0
d=0
s=0
al=0
au=0
sp=0
v=0
for i in n:
if i.isalpha():
a=a+1
if i.islower():
al=al+1
if i in 'aeiou':
v=v+1
if i.isupper():
au=au+1
if i in 'AEIOU':
v=v+1
elif i.isdigit():
d=d+1
elif i.isspace():
s=s+1
else:
sp=sp+1
print('Alphabets:',a)
print('Digits:',d)
print('Spaces:',s)
print('LowerCase:',al)
print('UpperCase:',au)
print('Vowels:',v)
OUTPUT:
Q41. Program for binary search in a list.
Ans.
size=int(input("Enter size of list= "))
l=[]
for i in range (size):
n=int (input("Enter element of list= "))
l.append(n)
snum=int(input('Enter no. to be Searched:'))
start=0
last=len(l)-1
while start<=last:
mid=(start+last)//2
if snum==l[mid]:
print('Element found at position',mid)
found=1
break
elif snum>l[mid]:
start=mid+1
else:
last=mid-1
found=0
if found==0:
print('Element not found')
Q42. Program for linear search in a list.
Ans.
size=int(input("Enter size of list= "))
l=[]
for i in range (size):
n=int (input("Enter elements of List= "))
l.append(n)
sn=int(input("Enter number to Search= "))
for i in range (size):
if sn==l[i]:
print ("Number Found at Index", i)
break
else:
print("Number not found")
Q43. Program to create binary file with roll number, name and marks. Input a roll number
and update the marks
Ans.
import pickle
with open('text.dat','wb') as f:
l=[]
while True:
rno=int(input('Enter roll no.:'))
name=input('Enter name:')
marks=int(input('Enter marks:'))
rec=[rno,name,marks]
l.append(rec)
ch=input('Want to enter more(Y/N):')
if ch in 'nN':
break
pickle.dump(l,f)
Q44.Remove all the lines that contain the character ‘a’ in a file and write it to another file.
Ans.
with open('text.txt') as f:
data=f.readlines()
with open('text2.txt','w') as f:
l=[]
for i in data:
if 'A' in i:
pass
else:
l.append(i)
f.writelines(l)
Q45. Create a binary file with name and roll number. Search for a given roll number and display the
name, if not found display appropriate messages.
Ans.
import pickle
with open('text.dat') as f:
found=0
st=int(input('Etner roll no. to be searched:'))
while True:
l=pickle.load(f)
print(l)
for i in l:
if i[0]==st:
print('roll no.:',i[0],'name:',i[1],'marks:',i[2])
found=1
if found==0:
print('roll no. not found')
break
OUTPUT:
Q46. Write PUSH and POP operations of stack to add a new book and remove a book from a list of
stacks.
Ans.
Q48. Read a text file and display the number of vowels/consonants/uppercase/lowercase characters
in the file.
Ans.
n=input('Enter the String= ')
a=0
d=0
s=0
al=0
au=0
sp=0
v=0
for i in n:
if i.isalpha():
a=a+1
if i.islower():
al=al+1
if i in 'aeiou':
v=v+1
if i.isupper():
au=au+1
if i in 'AEIOU':
v=v+1
elif i.isdigit():
d=d+1
elif i.isspace():
s=s+1
else:
sp=sp+1
print('Alphabets:',a)
print('Digits:',d)
print('Spaces:',s)
print('LowerCase:',al)
print('UpperCase:',au)
print('Vowels:',v)
Q49. PROGRAM TO WRITE ADD CLIENT AND DELETE CLIENT FROM A LIST
Ans.
def ADDCLIENT(CLIENT):
h=input('Enter client name:')
CLIENT.append(h)
print('New client list:',CLIENT)
def DELETECLIENT(CLIENT):
h=input('Enter client name:')
CLIENT.remove(h)
print('NEW client list:',CLIENT)
size=int(input("Enter size of list= "))
CLIENT=[]
for i in range (size):
n=input("Enter Clients= ")
CLIENT.append(n)
while True:
print('***MAIN MENU***')
print('1.Add a client')
print('2.Remove a client')
print('3.exit')
ch=int(input('Enter your choice:'))
if ch==1:
ADDCLIENT(CLIENT)
if ch==2:
DELETECLIENT(CLIENT)
if ch==3:
break
Q50. A binary file “Emp.dat” has structure [Empno,Empname,Debt,Salary]. Write a user defined fun
Ans.
import pickle
def CreateEmp():
f1=open("emp.dat",'wb')
eid=input("Enter E. Id")
ename=input("Enter Name")
designation=input("Enter Designation")
salary=int(input("Enter Salary"))
l=[eid,ename,designation,salary]
pickle.dump(l,f1)
f1.close()
OUTPUT:
THANK YOU