INDEX:
PRGMN PROGRAM NAME DATE [Link]
O:
INTRODUCTION
PART A:
1 Write a python program using a function to print fibonacci series up to n
numbers.
2 Write a Menu driven program in python to find factorial, and sum of
natural Numbers using a function
3 Write a python program using user defined function to calculate interest
amount using simple interest method and compound interest method and
find the difference of interest amount between the two methods
4 Write a Python Program to read a text file and display the number of
vowels, consonants, uppercase and lowercase characters in the file
5 Write a python code to count the number of lines, number of words and
number of characters in a text file.
6 Write a python program to perform reading and writing operation in a
text file.
7 Write a python program using function to sort the elements of a list
using bubble sort method
8 Write a python program using function to sort the elements of a list
using selection sort method
9 Write a python program using function to sort the elements of a list
using insertion sort method
10 Write a python program using function to search an element in a list
using linear search method
11 Write a python program using function to search an element in a list
using binary search method
12 Write a python program to add and display elements from a stack using
list
PART B
13 Create a table with the following fields and enter 10 records into the
table.
Entity Name: marks
14 Create a table for house hold Electricity bill with the following fields
and enter 10 records.
Entity Name: BESCOM
15 Create a table with the following details and enter 10 records into the
table.
Entity Name: student
16 Create a table with following fields and enter 10 records into the table.
Entity Name: Library
PART A: PYTHON PROGRAMS
A1. Write a python program using a function to print fibonacci series up to n numbers
# Fibonacci numbers
def fibo(n):
a=0
b=1
c=b
count = 2
print(a," ", b, end=" ")
while count <n:
print(c, end=" ")
count += 1
a, b = b, c
c=a+b
n = int(input("enter the Limit:"))
fibo(n)
print()
(output should be written in unruled sheet)output:
enter the Limit: 5
0 1123
A2. Write a Menu driven program in python to find factorial, and sum of natural Numbers
using a function
# Python program to find factorial of given number and Sum of natural numbers
def fact(n):
return 1 if (n==1 or n==0) else n * fact(n - 1);
def sum(n):
return 0 if (n==0) else n+sum(n-1);
num = int(input("Enter any number : "))
print("1:To find the factorial \n 2:To find the sum \n 3:Exit")
opt=int(input("Enter the option 1-3 : "))
if (opt==1):
print("Factorial of ",num,"is :",fact(num))
elif(opt==2):
print("Sum of ",num,"is :",sum(num))
else:
print("Exit from program ")
output:
Enter any number : 6
1-To find the factorial
2-To find the sum
3-Exit
Enter the option 1-3 :1
Factorial of 6 is: 720
Enter the option 1-3 :2
Sum of 6 is: 21
Enter the option 1-3 :3
Exit from program
A3. Write a python program using user defined function to calculate interest amount using
simple interest method and compound interest method and find the difference of interest
amount between the two methods
# Simple and Compound Interest
# Reading principal amount, rate and time
def simpInt(principle,time,rate):
si = float(principle*time*rate/100)
return si
def compInt(principle,time,rate):
ci = float(principle * ((1+rate/100)**time - 1))
return ci
principle = float(input('Enter amount: '))
time = float(input('Enter time: '))
rate = float(input('Enter rate: '))
si=simpInt(principle,time,rate)
ci=compInt(principle,time,rate)
print('Simple interest is Rs. %8.2f' % si)
print('Compound interest is Rs. %8.2f' % ci)
diffint=ci-si
print("Difference is Rs. %8.2f " % diffint)
Output:
Enter amount: 2000
Enter time: 3
Enter rate: 2
Simple interest is Rs. 120.00
Compound interest is Rs. 122.42
Difference is Rs. 2.42
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"
vowel_count = 0
consonant_count = 0
uppercase_count = 0
lowercase_count = 0
with open(file_name, "r") as file:
text = [Link]()
for char in text:
if [Link]():
if char in vowels:
vowel_count += 1
else:
consonant_count += 1
if [Link]():
uppercase_count += 1
elif [Link]():
lowercase_count += 1
print("Vowels:" ,vowel_count)
print("Consonants: ", consonant_count)
print("Uppercase characters: ",uppercase_count)
print("Lowercase characters: ", lowercase_count)
def create_text_file(file_name, content):
with open(file_name, "w") as file:
[Link](content)
file_name = "[Link]"
content = input("enter few sentences to create a text file with content: ")
create_text_file(file_name, content)
count_characters(file_name)
output:
enter few sentences to create a text file with content: Hi hello this is sample
program to demonstrate
Vowels: 14
Consonants: 25
Uppercase characters: 1
Lowercase characters: 38
A5) Write a python code to count the number of lines, number of words and number of
characters in a text file.
# Python program to count number of lines,
# Words and character in textfile
def count_text_file(file_name):
line_count = 0
word_count = 0
char_count = 0
with open(file_name, "r") as file:
for line in file:
line_count += 1
word_count += len([Link]())
char_count += len(line)
print("Lines: ", line_count)
print("Words: ", word_count)
print("Characters: ", char_count)
def create_text_file(file_name, content):
with open(file_name, "w") as file:
[Link](content)
file_name = "[Link]"
content = """Hello Students
This is a sample text file.
It contains multiple lines."""
create_text_file(file_name, content)
count_text_file(file_name)
output:
Lines: 3
Words: 12
Characters: 70
[Link] a python program to perform reading and writing operation in a text file.
file= open("[Link]","w+")
print("Writing data in the file")
print()
while True:
line=input("Enter a sentence")
[Link](line)
[Link]('\n')
ch=input("Do you wish to enter more data (Y/N)")
if ch in ('n','N'):
break
print("The byte position of file is",[Link]())
[Link](0)
print()
print("Reading data from file")
str=[Link]()
print(str)
[Link]()
output:
Writing data in the file
Enter a sentence I am student of class XII
Do you wish to enter more data (Y/N)y
Enter a sentence Chetan college
Do you wish to enter more data (Y/N)n
The byte position of file is 45
Reading data from file
I am student of class XII
Chetan college
A7. 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_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] >arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
def input_list():
arr = []
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 = input_list()
print("Original list:", arr)
bubble_sort(arr)
print("Sorted list:", arr)
Output:
Enter the number of elements in the list: 5
Enter element 3
Enter element 4
Enter element 1
Enter element 6
Enter element 8
Original list: [3, 4, 1, 6, 8]
Sorted list: [1, 3, 4, 6, 8]
A8. Write a python program using function to sort the elements of a list using selection sort
method
# Selection sort in Python
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_index = i
for j in range(i+1, n):
if arr[j] <arr[min_index]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i]
def input_list():
arr = []
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 = input_list()
print("Original list:", arr)
selection_sort(arr)
print("Sorted list:", arr)
Output:
Enter the number of elements in the list: 5
Enter element 6
Enter element 9
Enter element 7
Enter element 2
Enter element 4
Original list: [6, 9, 7, 2, 4]
Sorted list: [4, 2, 6, 7, 9]
A9. Write a python program using function to sort the elements of a list using insertion sort
method
# python program using function to sort the elements of list using insertion sort method
def insertion_sort(arr):
for i in range(1, 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 input_list():
arr = []
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 = input_list()
print("Original list:", arr)
insertion_sort(arr)
print("Sorted list using insertion sort method:", arr)
Output:
Enter the number of elements in the list: 5
Enter element 25
Enter element 56
Enter element 23
Enter element 7
Enter element 8
Original list: [25, 56, 23, 7, 8]
Sorted list using insertion sort method: [7, 8, 23, 25, 56]
A10 .Write a python program using function to search an element in a list using linear search
method
# Search function with parameter list name and the value to be searched - Linear Search
def linear_search(arr, target):
for index, element in enumerate(arr):
if element == target:
return index
return -1
def input_list():
arr = []
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 = input_list()
target = int(input("Enter the element to search for: "))
result = linear_search(arr, target)
if result != -1:
print(target, " Element found at index ", result)
else:
print(target, " Element not found in the list.")
output:
Enter the number of elements in the list: 5
Enter element 4
Enter element 6
Enter element 7
Enter element 23
Enter element 8
Enter the element to search for: 7
7 Element found at index 2
Enter the number of elements in the list: 5
Enter element 3
Enter element 5
Enter element 67
Enter element 3
Enter element 45
Enter the element to search for: 8
8 Element not found in the list.
A11. Write a python program using function to search an element in a list using binary search
method.
def binary_search(arr, target):
low=0
high = len(arr) - 1
while low<= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elifarr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
def input_list():
arr = []
n = int(input("Enter the number of elements in the list: "))
for i in range(n):
element = int(input("Enter the elements in ascending order "))
[Link](element)
return arr
arr = input_list()
target = int(input("Enter the element to search for: "))
result = binary_search(arr, target)
if result != -1:
print(target, "Element found at index ", result, "and position is:",result+1)
else:
print(target, "Element not found in the list.")
Output:
Enter the number of elements in the list: 5
Enter the elements in ascending order 2
Enter the elements in ascending order 3
Enter the elements in ascending order 4
Enter the elements in ascending order 5
Enter the elements in ascending order 7
Enter the element to search for: 5
5 Element found at index 3 and position is: 4
[Link] a python program to add and display elements from a stack using list
# initial empty stack
stack = [ ]
#Pushing the elements into a stack
print("initially stack is empty :",stack)
[Link]('x')
[Link]('y')
[Link]('z')
print("After PUSHING stack is :")
print(stack)
#Poping the elements from a stack
print('After POPPED from stack: ')
print([Link]())
print([Link]())
print([Link]())
print('\nmy_stack after elements are poped:')
print(stack)
output:
initially stack is empty : []
After PUSHING stack is :
['x', 'y', 'z']
After POPped from stack:
z
y
x
my_stack after elements are poped:
[]
PART B :MYSQL
B1) Create a table with the following fields and enter 10 records into the table.
Entity Name: marks
#table creation
Mysql> create table marks(
rollno int,
snamevarchar(15),
lang_mks int CHECK (lang_mks BETWEEN 0 AND 100),
eng_mks int CHECK (eng_mks BETWEEN 0 AND 100),
sub1_mks int CHECK (sub1_mks BETWEEN 0 AND 100),
sub2_mks int CHECK (sub2_mks BETWEEN 0 AND 100),
sub3_mks int CHECK (sub3_mks BETWEEN 0 AND 100),
sub4_mks int CHECK (sub4_mks BETWEEN 0 AND 100)
);
#insertion of values
Mysql>INSERT INTO marks VALUES(1010, 'RAJ', 89, 97, 98, 99, 86, 95);
Mysql>INSERT INTO marks VALUES (1026, 'KIRAN', 67, 62, 72, 86, 72, 62);
Mysql>INSERT INTO marks VALUES (1042, 'ANAND', 78, 87, 92, 82, 72, 76);
Mysql>INSERT INTO marks VALUES (1250, 'RAM', 72, 86, 72, 62, 87, 68);
Mysql>INSERT INTO marks VALUES (5212, 'VIJAYA', 46, 58, 86, 92, 72, 62);
Mysql>INSERT INTO marks VALUES (3622, 'MANOJ', 86, 56, 62, 86, 52, 64);
Mysql>INSERT INTO marks VALUES (1948, 'REEHAN', 63, 68, 52, 56, 96, 76);
Mysql>INSERT INTO marks VALUES (1482, 'KAJOL', 49, 54, 48, 76, 62, 55);
Mysql>INSERT INTO marks VALUES (1947, 'KUMAR', 98, 98, 99, 100, 97, 99);
(i) List all the records
Mysql>select * from marks;
(ii) Display the description of the table
Mysql>desc marks;
(iii) Add the new attributes total and percent
Mysql>alter table marks add(total int(3), percent float(25,3));
Mysql>desc marks;
(iv) Calculate total and percentage of marks for all the students
Mysql>update marks set total = lang_mks+eng_mks+sub1_mks+sub2_mks+sub3_mks+sub4_mks;
Mysql> update marks set percent = total/600*100;
Mysql> select * from marks;
(v) List the students whose percentage of marks is more than 60%.
Mysql>select sname, percent from marks where percent >=60;
(vi) List the students whose percentage is between 60% and 85%.
Mysql>select sname, percent from marks where percent between 60 and 85;
(vii) Arrange the students based on percentage of marks from highest to lowest
Mysql>select * from marks order by percent desc;
[Link] a table for house hold Electricity bill with the following fields and enter 10 records.
Entity Name: BESCOM
Mysql>CREATE TABLE BESCOM (
RRNO VARCHAR(10) PRIMARY KEY,
CUSTNAME VARCHAR(25) NOT NULL,
BILLDATE DATE,
UNITS INT
);
i)View the structure of table.
Mysql>desc BESCOM;
Mysql>INSERT INTO BESCOM VALUES('E1120', 'RAJ', '2024-05-05', 250);
Mysql>INSERT INTO BESCOM VALUES ('E2210', 'KIRAN', '2024-03-26', 178);
Mysql>INSERT INTO BESCOM VALUES ('E1450', 'ANAND', '2024-04-15', 56);
Mysql>INSERT INTO BESCOM VALUES ('E2126', 'RAM', '2024-05-08', 782);
Mysql>INSERT INTO BESCOM VALUES ('E1562', 'MANJULA', '2024-05-02', 562);
Mysql>INSERT INTO BESCOM VALUES ('E6221', 'MANOJ', '2024-05-18', 72);
Mysql>INSERT INTO BESCOM VALUES ('E5822', 'REEHAN', '2024-02-19', 92);
Mysql>INSERT INTO BESCOM VALUES ('E1692', 'KAJOL', '2024-03-25', 73);
Mysql>INSERT INTO BESCOM VALUES ('E6721', 'KUMAR', '2024-07-14', 589);
Mysql>INSERT INTO BESCOM VALUES ('E2682', 'REEMA', '2024-05-11', 100);
ii)List all the records
Mysql>select * from BESCOM;
iii)Add a new field for bill amount in the name of billamt.
Mysql> ALTER TABLE BESCOM ADD BILLAMT FLOAT(27,2);
Mysql>desc BESCOM;
iv).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
mysql>UPDATE BESCOM SET BILLAMT = 100 + UNITS * 7.50 WHERE UNITS <= 100;
mysql>UPDATE BESCOM SET BILLAMT = 100 + (100 * 7.50) + (UNITS - 100) * 8.50 WHERE
UNITS > 100;
Mysql>select * from BESCOM;
v)Display the maximum, minimum and total bill amount.
Mysql>SELECT MAX(billamt), min(billamt), avg(billamt), sum(billamt) FROM BESCOM;
vi)List all the bills generated in a sorted order based on RRNO.
Mysql>select * from bescom order by rrno;
B3) Create a table with the following details and enter 10 records into the table.
Entity Name: student
Mysql>CREATE TABLE student (
Rollno int PRIMARY KEY,
SnameVARCHAR(15) NOT NULL,
dob date,
Gender CHAR(1),
CombnVARCHAR(5),
Class VARCHAR(6)
);
Mysql>desc student;
Mysql>insert into student values (3739, 'Uday', '2004-9-12', 'M', 'PCMC','2B');
Mysql>insert into student values (1001, 'Raj Kumar', '2005-5-21', 'M', 'BASC','2A');
Mysql>insert into student values (1005, 'Kiran', '2004-11-15', 'M', 'PCMC','2B');
Mysql>insert into student values (1042, 'Anand', '2005-12-22', 'M', 'CEBA','2C');
Mysql>insert into student values (1250, 'Ram', '2004-6-18', 'M', 'PCMC','2A');
Mysql>insert into student values (5212, 'Vijaya', '2007-7-28', 'F', 'PCMC','2A');
Mysql>insert into student values (1029, 'Bharath', '2005-1-12', 'M', 'BASC','2B');
Mysql>insert into student values (2152, 'Rekha', '2006-6-8', 'F', 'CEBA','2C');
Mysql>insert into student values (1948, 'Reehan', '2005-4-17', 'M', 'CEBA','2C');
Mysql>insert into student values (2443, 'Manjula', '2005-8-15', 'F', 'PCMC','2B');
(i) List all the students
Mysql>select * from student;
(ii) List only those students who are in BASC and CEBA combination.
Mysql>select * from student where combn='BASC' or combn='CEBA';
(iii) List only the combination by removing duplicate values.
Mysql>select distinct(combn) from student;
(iv) List the students alphabetically.
Mysql>select sname from student order by sname;
(v) List the students alphabeticallyclass-wise.
Mysql>select sname, class from student order by sname;
(vi) List the students who born in the month of June of any year.
Mysql> SELECT * FROM student WHERE MONTH(dob) = 6;
(vii) Count the number of students Gender-wise.
Mysql>SELECT gender, COUNT(*) FROM student GROUP BY gender;
B4) Create a table with following fields and enter 10 records into the table.
Entity Name: Library
Mysql>CREATE TABLE Library (
Title VARCHAR(75) NOT NULL,
Author VARCHAR(60),
Year INT,
Category VARCHAR(25),
Price FLOAT(7,2),
Qty INT
);
Mysql>desc Library;
Mysql>INSERT INTO Library VALUES('The Data Science Handbook', 'Darshan', 2019, 'Data science',750.00,
12);
Mysql>INSERT INTO Library VALUES ('Introduction to Computer Programs', 'Harshavardhan',NULL,
'Computer Science', 700.00, 6);
Mysql>INSERT INTO Library VALUES ('Computer Science Text book Class 12', 'Reeta Sahoo', 2019,
'Textbook', 450.00, 4);
Mysql>INSERT INTO Library VALUES ('A book on AI', 'Sagar', 2016, 'AI', 200.00, 7);
Mysql>INSERT INTO Library VALUES ('Robots and Automation', 'Dushyanth', 2018, 'Digital Technology',
750.00, 9);
Mysql>INSERT INTO Library VALUES ('AI 2041', 'Chen & Lee', 2021, 'AI', 1000.00, 2);
Mysql>INSERT INTO Library VALUES ('Computer Hardware and Software', 'Chethan', 2000,'Computer
science', 500.00, 7);
Mysql>INSERT INTO Library VALUES ('The Future of Work', 'Dev Kumar', 2018, 'DigitalTechnology',
750.00, 4);
Mysql>INSERT INTO Library VALUES ('Healthcare and AI', 'Eshwar', 2019, 'AI', 950.00, 9);
Mysql>INSERT INTO Library VALUES ('Introduction to Data Science', 'Dravid', 2014, 'Data Science',400.00,
5);
(i) List all the books
Mysql>select * from Library;
(ii) Calculate Amount by altering table by adding a new column ‘Amount’
(a) mysql>alter table library add(amount float(7,2));
mysql>desc library;
(b)mysql> update library set amount = price * qty;
Mysql>select * from library;
(iii) List the records of all those books price is between 400 and 900.
Mysql>select *from library where price between 400 and 900;
(iv) List those records with no value in the attribute year .
Mysql>select *from library where year is NULL;
(v) List the names of the authors whose name starts with letter ‘C’ or ‘D’.
Mysql>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’.
Mysql>SELECT Title, Year, Category FROM Library WHERE Category LIKE '%science%';
(vii) List all those records whose year of publication is 2010 onwards with book price is less than Rs.750.
mysql>SELECT *FROM Library WHERE Year >= 2010 AND Price < 750;
****************************************************