Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 49

Lovely Public Sr. Sec.

School
Computer Science -
Program file

Name- Shashank Tiwari


Class- XII
Roll No.-
Certificate
Name: Shashank Tiwari

Class: XIIth

Roll No.: ________

Institution: Lovely Public Sr. Sec. School

We certify this to be the meritorious work of the


student during the academic year 2022-23. In the
subject of Computer Science.
Index
S No. Page
Question No.
1 Program to file factorial of a number 1
2 Python code to compute nth Fibonacci number 2
3 Write a program to generate a random number between 1 and 6 3
4 To find the sum of elements in the list 4
5 To find if a string is palindrome or not 5
6 Sum of n natural numbers using loop statement 6
7 To illustrate read() by reading the entire data from a text file 7
8 To illustrate read(n) by reading only the first 10 characters from the text file 8-9
9 To read a file line by line 10
10 To read all lines from the file into a list 11
11 To read data from 2nd character into a list 12
12 Program to write data into a file 13
13 Design and code the program that asks the user to input their names along with 14
age and store data into a text file
14 Check whether given key already exists in a Python Dictionary 15
15 Python program to find second maximum value in a Dictionary 16
16 Write a program to write into a binary file, the details are available in the form 17
of Dictionary
17 Write a program to append students record in the same file 18
18 Write a program to display the records stored 19
19 Function to write user data in CSV file 20
20 Function to write user data in CSV file 21
21 Write a code to insert data in a csv file 22
22 Write a program to read a data from a csv file 23
23 Write a program to sort a list by bubble sort in ascending order 24
24 Write a program to input values in a list. Prompt user to input the number and 25-26
count the frequency of number in the list.
25 Write a program to sort a list by selection sort in ascending order 27
26 Write a python function to check whether the number is Armstrong or not 28
27 Wrute a program to display and count the number of world in the text file 29
28 Write PUSH,POP and SHOW operations to stack to add a new book and remove 30-31
a book from lists of stack
29 Write a program to create a table and insert data using mysql-python 32
connectivity
30 Write a program to update a database using mysql-python connectivity 33
31 Write a program to display data using mysql-python interface 34
32 Write a program to delete data using mysql-python connectivity 35
33 Write a python program to get the largest number from a list 36
34 Write a python program to count the number of elements in a list within a range 37
35 Write a function in Python PUSH(Arr),POP() and SHOW(), where Arr is a 38
list of numbers. From this list push all numbers divisible by 5 into a stack
implemented by using a list.
36 Enter a no. and display it is Armstrong or not? 39
37 Program to find max, min and mean values from the list 40
38 Program to count lowercase and uppercase letter in an inputted list 41
39 Program to illustrate a four function calculator 42
40 Program to check if a number is positive, negative or zero 43
41 Program for binary search 44
42 Program for linear search in a list 45
43 Program to create binary file with roll number, name and marks. Input a 46
roll number and update the marks
44 Create a binary file with name and roll number. Search for a given roll 47
number and display the name, if not found display appropriate message.
45 Remove all the lines that contain the character ‘a’ in a file and write it to another 48
file
46 Read a text file and display the number of vowels/consonants/ 49
uppercase/lowercase characters in the file
47 Read a text file line by line and display each word separated by a # 50
49 Write Addclient(Client) and Deleteclient(Client) methods in Python to add 51
a new client and delete client from a list of Client Number and Client
Names, consider them act as insert and delete operations of the queue
data structure.
50. A binary file “Emp.dat” has structure [Empno,Empname,Debt,Salary]. Write a 52
user defined fun
Q1. PROGRAM TO FIND THE FACTORIAL OF A NUMBER

Ans.

num=int(input("Enter the number= "))


for n in range (1,num):
if True:
num*=n
else:
None
print(num)
Q2. PROGRAM TO PRINT FIBONACCI SERIES

Ans.

n=int(input('Enter number of series= '))


a=0
b=1
print(a,b,end='')
i=1
while i<=n-2:
c=a+b
print(c,end='')
a,b=b,c
i+=1

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.

n=input('Enter the String:')


s=''
for i in range(len(n)-1,-1,-1):
s=s+n[i]
if n==s:
print(n,'is a Palindrome')
else:
print(n,'is not a Palindrome')

OUTPUT:
Q6. 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:
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****')

print('''1.Area & Perimeter of Circle

2.Area & Perimeter of Square

3.Area & Perimeter of Rectangle

4.Area & Perimeter of Triangle

5.Area & Perimeter of Parallelogram

''')

n=int(input('Select The Number of The Polygon:'))

if n==1:

r=int(input('Enter the Radius= '))

circumference=2*3.14*r

circlearea=3.14*r**2

print('Circumference=',circumference)

print('Area=',circlearea)

elif n==2:

s=int(input('Enter the Side= '))

squareperimeter=4*s

squarearea=s**2

print('Perimeter=',squareperimeter)

print('Area=',squarearea)

elif n==3:

l=int(input('Enter the Length= '))

b=int(input('Enter the Breadth= '))

rectangleperimeter=2*(l+b)

rectanglearea=l*b

print('Perimeter=',rectangleperimeter)

print('rectanglearea=',rectanglearea)

elif n==4:

triangleside1=int(input('Enter the First Side= '))


triangleside2=int(input('Enter the Second Side= '))

trianglebase=int(input('Enter the Base= '))

h=int(input('Enter the Height= '))

triangleperimeter=triangleside1+triangleside2+trianglebase

trianglearea=1/2*trianglebase*h

print('Perimeter=',triangleperimeter)

print('Area=',trianglearea)

elif n==5:

parallelogrambase=int(input('Enter the Base= '))

parallelogramside=int(input('Enter the Adjacent Side= '))

parallelogramheight=int(input('Enter the Height= '))

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.

size=int(input("Enter size of list= "))


l=[]
c=0
for i in range (size):
n=int (input("Enter element of list= "))
l.append(n)

j=int(input('Enter the Number to be counted:'))


for i in l:
if i==j:
c=c+1
print('Frequency of element',j,'is',c)
Q25. PROGRAM TO CONVERT TEMPERATURE IN CELCIUS TO FAHRENHEIT

Ans.

t=int(input('Enter Temeperature (in Celcius)= '))

t=(t*(9/5))+32

print('Temperature (in Fahrenheit)=',t)


Q26.PROGRAM TO CHECK WHETHER A NUMBER IS PRIME OR NOT
Ans.
num= int(input("Enter the number= "))
n=num
f=0
for n in range (2,n):
if num%n==0:
f+=1
print (num,"is not a prime number")
break
if f==0:
print(num,"is a prime number")
Q27. Program to calculate number of characters
Ans.
with open('text.txt') as f:
data=f.read()
c=0
al=0
au=0
v=0
for i in data:
if i.islower():
al=al+1
if i in 'aeiou':
v=v+1
else:
c=c+1
if i.isupper():
au=au+1
if i in 'AEIOU':
v=v+1
else:
c=c+1
print('Consonants:',c)
print('LowerCase:',al)
print('UpperCase:',au)
print('Vowels:',v)
Q28. 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")
Q29. PROGRAM TO CREATE TABLE AND INSERT DATA USING MYSQL-PYTHON CONNECTIVITY

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)

Q39. Program to illustrate a four-function calculator?


Ans.
A=int(input('Enter First Number='))
B=int(input('Enter Second Number='))
C=input('Enter the Operator=')
if(C=='+'):
print(A+B)
elif(C=='-'):
print(A-B)
elif(C=='/'):
print(A/B)
elif(C=='//'):
print(A//B)
elif(C=='%'):
print(A%B)
elif(C=='*'):
print(A*B)
elif(C=='**'):
print(A**B)
else:
pass
Q40. Program to check if a number is positive , negative or zero?
Ans.
A=int(input('Enter the number= '))
if(A<0):
print('Its Negative')
elif(A==0):
print('Its Zero')
else:
print('Its Positive')

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

You might also like