Computer Study Material Programs
Computer Study Material Programs
PYTHO N
a
A 1. Write a python program using function to print fibonacci series up to n numbers
#Fibonacci numbers
def fibo(n):
nl =O
n2= 1
next_number = n2
count=2
while count<n:
print(next_number,end=" ")
count +=1
n 1, n2 = n2, next_number
next_number = n 1 + n2
n = int(input("enter the Limit"))
fibo(n)
print()
OUTPUT:
enter the Limit 10
0 I I 2 3 5 8 13 21 34 55 89
I At• Write a Menu driven program
in python to find factorial, and sum of
natural NumbefS using a function
#python program to find factorial of giv
en number and Sum of natural number
s
def fact(n):
return 1 if (n 1 or n =0 ) else n• fact (n-1 );
defsutn (n):
return Oif (n= O) else n+sum(n-
1);
nUOl == int(input("Enter any nu mb er: "))
OUTPUT:
Enter any number : 5
.
1-To find the factorial, 2-To fiin d the sum 3-Exit
) LC:_)
A3. Write a python progratn usin d fi d .
and com ared . g user e ne function to calculate interest amount using simple interest method
P mterest method and find the difference of interest amount between the two methods.
for
si = float(p*t*r/100)
return si
def complnt(principal,time,rate):
p = float(input('Enter amount:'))
f
t = float(input('Enter time:')) I
r = float(input('Enter rate:')) I
si=simplnt(p,t,r)
ci=complnt(p,t,r)
diffint=ci-si;
OUTPUT:
Enter time: 3
A4. Write a Python Program to read a text file and display the number of vowels, consonants,
uppercase and lowercase characters in the file
# Python Program to create a text file and to read a text file and display the number of
# vowels, consonants, uppercase and lowercase characters in the file
def count_characters(file_name):
vowels = "aeiouAEIOU"
v count =0
c count =0
upper_count = 0
lower_count = 0
if [Link] ( ):
if char in vowels:
v count + =l
else:
c count + =I
if [Link]:
upper _count + = I
elif [Link]():
lower count + = I
print("Vowels:" , v_count)
print("Consonants: ", c_count)
print("Uppercase characters: ",upper_count)
print("Lowercase charac ters:", lower_count)
count_characters(file_name)
OUTPUT:
enter few sentences to create a text file with content: This progr
am is TO Create ·a FILE
Vowels: 11
Consonants: 15
Uppercase characters: 8
Lowercase characters: 18
linecount =O
wordcount =O
charcount =O
with open(file_name, "r") as file:
linecount + =I
wordcount + = len([Link]())
charcount + = len(line)
print("Lines: ", Iinecount)
print("Words: ", wordcount)
print("Characters: ", charcount)
file. write(content)
file_name = "[Link]"
content = """Hello Students
This is a sample text file.
create_text_file(file_name, content)
count_text(file_name)
OUTPUT:
Lines: 3
Words: 12
Characters: 70
A6) Write a python program to create and to read records in binary file with student name and
marks of six subjects.
import pickle
while True:
print("' I. Create Binary File.
2. Display the File.
3. Exit"')
a = int(input('choose a command (1-2,3-exit): '))
if a== I:
f open('[Link]','wb')
X int(input('How many student: '))
for i in range(x):
name = input('Name: ')
english = int(input('English Mark: '))
Ian = int(input("Language Marks"))
phy = int(input('Physics Mark: '))
chem = int(input('Chemistry Mark: '))
maths = int(input('Maths Mark: '))
[Link]{)
elif a== 2: open('studenldat','rb')
f =
try:
while True:
p = [Link](t)
print(p)
except
[Link]()
ifa> 2:
brea k
OUTPUT:
1. Crea te Binary File.
2. Display the File .
3.E m
choose a com man d (1-2, 3-exit): 2
('Bharath', 77, 88, 99, 77, 88, 99)
('Gu ru', 77, 88, 99, 77, 88, 99)
('Ma njul a', 88, 99, 88, 99, 88, 88)
['Re kha' , -99, 88, 99, 99, 88, 88)
['Od ay', 66, 77, 66, 77, 66, 77)
1. Crea te Bina ry File.
2. Disp lay the File. . . ..~·
3. Exit
choose a com man d (1-2,9-exit):3
above
of the students having percentage 90 and
Write a python program to copy the records
from the binary me into another file.
into another file.
# program to create and copy records to
copy records with percentage 90 and above
import pickle
while True:
print('" I. Create Biruuy File.
2. Display the main File.
3. Create new file wirh >90
4. Exit'")
·
a = int(input('choose a command (1-2,9-exit): '))
ifa= =l:
f = open('studentdat','wb')
0 = open('[Link]','wb')
X = int(input('How many student:'))
for i in range(x):
name = input('Name: ')
english = int(input('English Mark:'))
Ian = int(input("Language Marks"))
phy = int(input('Physics Mark:'))
chem = int{input('Chemistry Mark:'))
maths= int(input('Maths Mark: '))
cs = int(input('CS Mark:'))
total = phy+chem+cs+maths+english+lan
139
PU BOARD lAB MANUAL
-
,• •P"lF"""CS.,......_ _ _ __ _ _ _
_,_,_ ,.,.. ITIIIP'Y ..~ .~ - - -- -- - - -- - - - - - --
I g =
[Link](t,f)
if per>= 90:
[name, english , Ian, phy, chem, maths, cs, total, per]
[Link](g,o)
[Link]()
[Link]()
elif a== 2:
f = open('[Link]','rb')
try:
I except:
while True:
p =-[Link](f)
print(p)
[Link]()
elif a== 3:
print("Studets with> 90 Marks")
f = open('[Link]','rb')
try:
while True:
o = [Link](f)
print(o)
except:
[Link]()
else:
break
OUTPUT:
1. Create Binary File.
--- - rilliplay the main File.
·3. Create new file wirh >90
. 4. Exit
choose a command (1-2,9-exit): 2
['Bharath', 77, 88, 99, 88, 77, 77,506, 84.33333333333334]
['Guru', 88, 77, 88, 77, 88, 77,495, 82.5)
['Manjula', 88, 77, 88, 88, 77, 77,495, 82.5)
['Rekha', 99, 88, 99, 88, 99, 88, 561, 93.5)
['Uday', 99, 99, 99, 99, 99, 99, 594, 99.0]
1. Create Binary File.
2. Display the main File.
3. Create new file wirh >90
4. Exit
choose a command (1-2,9-exit): 3
Studets with > 90 Marks
('Rekba', 99, 88, 99, 88, 99, 88, 561, 93.5]
('Uday', 99, 99, 99, 99, 99, 99,594, 99.0)
1. Create Binary File.
AB. Write a python program using function to sort the elements of a list using bubble sort
method
# program using function to sort the elements of list using bubble sort method
def bubble(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1 ):
if arr[j] > arr[j+ I]:
arr[j], arr[j+ I] = arr[j+ 1], arr[j]
def bubble_inO:
arr=O
n = int(input("Enter the number of elements in the list: "))
for i in range(n):
element= int(input("Enter element"))
[Link](element)
return arr
arr = bubble_inO
print("Original list:", arr)
bubble(arr)
print("Sorted list:", arr)
OUTPVf;
Enter the number of elements in the list: 5
Enter element 34
Enter element 56
Enter element 23
Enter element 12
Enter element 34
Original list: [34, 56, 23, 12, 34]
Sorted list: [12, 23, 34, 34, 56]
A9. Write a python program using function to sort the elements of a list using selection sort
Method
def selectioninQ:
arr= D
n = int(input("Enter the number of elements in the list: "))
for i in range(n):
ele = int(input("Enter element"))
[Link]( ele)
return arr
arr = selectionin0
selection(arr)
print("Sorted list:", arr)
OUTPUT:
Enter the number of elements in the list: S
Enter element 32
Enter element 45
Enter element 67
Enter element 34
Enter element 23
Original list: [32, 45, 67, 34, 23)
Sorted list using insertion sort method: [23, 32, 34, 45, 67)
l
,d 0. Write a python program using functi on to sort the eleme
nts of a list using Insertion sort
method
# python program using function to sort the elements of list using inserti
on sort method
definsertion(arr):
for i in range (l, len(arr)):
key= arr[i]
j =i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j-= 1
arr[j + 1] = key
def insertioninO:
am= D
n = int(inp ut("En ter the number of elements in the list: "))
for i in range (n):
eleme nt= int(input("Enter element"))
[Link] pend(element)
return arr
arr = insertioninO
print("Original list:", arr)
insertion(arr)
print("Sorted list using insertion sort meth0d:", arr)
OUTPUT:
method
deflinear(arr, target):
for inde~ ele in enumerate(arr):
if ele = target:
return index return
-I
deflinearinO:
arr= D
n = int(input("Enter the number of elements ="))
for i in range(n):
ele = int(input("Enter element"))
[Link](ele)
return arr
arr= linearinO
target= int(input("Enter the element to search for: "))
OUTPUT:
All. Write • python program using function to search an element in a list using binary search
method.
defbinaI)'(arr, target):
beg, end= 0, len(arr)- 1 : ;
arr[mid] = target:
return mid
elifarr[mid] < target:
beg=mid+ 1
else:
end=mid-1
return-I
defbinaiyinQ: arr
=□
n = int(input("Enter the number of elements="))
for i in range(n):
ele= int(input("Enterthe elements in as~ending order.")) '·:,:·· i •- -,,: ::'. j 1 ;
[Link](ele)
return arr
· ·
'
'
~ ~ .
.,... .· . ·tr "7' ••••
.
~ •. ' '... .,
I .
OUTPUT: ·
[Link]('x')
[Link]('y')
[Link]('z')
ck)
print("After PUSHING stack is:") print(sta
ed:') prin~stack)
print('\n my_stack after elements are pop
OUTPUT:
# python program to add and display elements from a queue using list#
ereate Queue and perform insert and delete ·
unPort queue
• '1 •• : j \ • • ;, • • •
q == [Link] . • • ..., f
,..
. • 'l .
after REMOVE is O • I
' ;
I •
f .t :
MYSQL
Bl) Create a table with the following fields and enter IO records into the table.
Entity Name: marks jS
(iiJ 0
Attribute name Type Size Constraints
Rollno Int 5
Sname Varchar 15 Not null
Lang mks Int 3 Between 0 and 100
Eng mks Int 3 Between 0 and 100
Subl mks Int 3 Between 0 and I 00
Sub2 mks Int 3 Between 0 and I 00
Sub3 mks Int 3 Between 0 and I 00
Sub4 mks Int 3 Between 0 and I 00
l
Data to be entered: (Values are indicative)
Rollno Sname Lane mks Ene: mks Subl mks Sub2 mks Sub3 mks Sub4 mks
1010 RAJ 89 97 98 99 86 95
1026 KIRAN 67 62 72 86 72 62
1042 ANAND 78 87 92 82 72 76
1250 RAM 72 86 72 62 87 68
5212 VIJAYA 46 58 86 92 72 62
3622 MANOJ 86 56 62 86 52 64
1948 REEHAN 63 68 52 56 96 76 CRE
1482 KAJOL 49 54 48 76 62 55 R1
1947 KUMAR 98 98 99 100 97 99 Cl
1951 REEMA 82 72 62 98 73 64 BJ
INSERT INTO marks VALUES(toto, 'RAJ', 89, 97, 98, 99, 86, 95) lJJ
);
INSERT INTO marks VALUES (1026, 'KIRAN', 67, 62, 72, 86, 72, 62)
INSERT INTO marks VALUES (1042, 'ANAND', 78, 87, 92, 82, 72, 76)
INSERT INTO marks VALUES (1250, 'RAM', 72, 86, 72, 62, 87, 68)
INSERT INTO marks VALUES (5212, 'VIJAYA', 46, 58, 86, 92, 72, 62)
INSERT INTO marks VALUES (3622, 'MAN OJ', 86, 56, 62, 86, 52, 64)
INSERT INTO marks VALUES (1948, 'REEHAN', 63, 68, 52, 56, 96, 76)
INSERT INTO marks VALUES (1482, 'KAJOL', 49, 54, 48, 76, 62, 55)
INSERT INTO marks VALUES (1947, 'KUMAR', 98, 98, 99, 100, 97, 99)
(iv) Calcula te total and percent age of marks for all the students
update marks set total= lang_m ks+eng _mks+s ubl_mks+sub2_mks+sub3_mks+sub4
_mks;
update marks set percen t= total/600*100;
(v) List the students whose percenta ge _o f marks is more than 60%.
select sname, percent from marks where percent >=60;
(vi) List the students whose percenta ge is between 60% and 85% . .
select sname, percent from marks where .percent between 60 and 85;
(vii) Arrange the students based on percentage of marks from highest to lowest.
select sname, percent from marks order by percent desc;
B2) Create a table for house hold Electricity bill with the· following fields and enter
10 records.
Entity Name: BESCOM
I0. Compute the bill amount for each consumer as per the following rules: _
· a. MINIMUM Amount Rs. 100 ·
b. For first 100 units Rs 7.50/Unit
c. For the above 100 units Rs. 8.50/Unit .··: , . ·, .: ·
(i) UPDATE BESCOM SET BILLAMT = 100 + UNITS * 7.50 WHERE UNITS<= 100;
• • t ... - -.... : ~ / .- '
(ii) UPDATE BESCOM SET BILLAMT = 100 + (100 * 7.50) + (UNITS - 100) * 8.50 WHERE
UNITS> 100;
insert into student vaiues (5212,· 'Vijay~•: '2007-7- ; ·: __ ~-. _·. _· _.;· ,. .
rt into .studen t valu es (1~29, 'Bha ra~h ', '200 5-1-12', 'M', 'BASC','2B'); .
inse
8', 'F', 'CEBA','2C'); _·_ ,_· ·
insert inio student values (2152; 'Rekha', '2006-6-
5-4-17', 'M', 'CEBA','2C');
insert into student values (1948, 'Reehan', '200
·· ' .. l t • • •
INSERT INTO Library VALUES('The Data Science Handbo~k', '~arshan', 2019, 'Da~ scienc~',
750.00, 12) . .. . .
~SERT INTO Library VALVES ('Introduction to Computer Programs', 'Harshavar~an', NULL,
'CoJPPuterScience', 700.00, 6) . . · ·. ..
INSERT INTO Library VALUE~ ('Computer Science Text book Class 12', 'Reeta Sahoo', 2019,
Textbook', 450.00, 4) . · , . . , . _ ·
INSERTJNTO Library VALVES ('A book on AI', 'Sagar', 2016, 'AI', 200.00, 7)
INSERT INTO Library VALVES ('Robots and Automation', .'Dushyanth', 2018, 'Digital ·
Technology', 750.00, 9) · . · : . :.
INSERT INTO Library VALUES ('AI 2041', 'Chen & Lee', 2021, 'AI', 1000.00, 2) . . ..
INSERT INTO Library VALVES ('Computer Hardware and Software', 'Chethan', 2000, 'Computer
science', 500.00, 7) : ··· ·· i •; .
INSERT INTO Library VALVES ('The Future ofWork~, 'Dev Kumar', 2018, 'Digital Technology',
750.00, 4) · · . ·· · · , . . . .. .
BOARD IAB MANUAL . . . : . . 152
INSERT INTO Library VALUES ('Healthcare d AI' ·,
INSERT INTO Library VALUES ('lntroductio a~ D , ESs~war\ 2019, 'Al', 950.00 9)
400.00 ' S)·' - n ° . ,
ata cience, 'Dravid' ' 2014, •u'ata Science'
(iii) List the records of all those books pric~ is between 400 and 900
select price from library where price between 400 and 900;
(v) List the names of the authors whose name starts with letter 'C' or 'D'.
select Author from library where author like 'C%' or author like 'D%';
(vi) List Title, year and category from the table library with category field has word 'science'.
SELECT Title, Year, Category FROM Library WHERE Category LIKE '%science%';
(vii) List all those records whose year of publication is 20 IO onwards with book price is iess than
Rs.750.
SELECT year, price FROM Library WHERE Year>= 2010 AND Price< 750;