Xii Final Practicals Xii b
Xii Final Practicals Xii b
1
CREATING A MENU DRIVEN PROGRAM TO PERFORM
ARITHMETIC OPERATIONS
2
CREATING A PYTHON PROGRAM TO DISPLAY FIBONACCI
SERIES
3
CREATING A MENU DRIVEN PROGRAM TO FIND FACTORIAL
AND SUM OF LIST OF NUMBERS USING FUNCTION
4
CREATING A PYTHON PROGRAM TO IMPLEMENT
MATHEMATICAL FUNCTIONS
5
USER DEFINED FUNCTION WITH PARAMETERS
7
COUNTING NUMBER OF WORDS IN DATA FILE
8
CREATING A PYTHON PROGRAM TO GENERATE RANDOM
NUMBER BETWEEN 1 TO 6
9
CREATING A PYTHON PROGRAM TO READ A TEXT FILE LINE
BY LINE AND DISPLAY EACH WORD SEPARATED BY '#'
11
CREATING A PYTHON PROGRAM TO COPY PARTICULAR LINES OF
A TEXT FILE INTO AN ANOTHER TEXT FILE
1
14
CREATING A PYTHON PROGRAM TO CREATE AND SEARCHI
EMPLOYEE’S RECORD IN CSV FILE
15
CREATING A PYTHON PROGRAM TO IMPLEMENT STACK
OPERATIONS
2
EX.NO: 1
DATE:
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is verified.
|”:
3
SAMPLE OUTPUT:
RUN- 2
***************************************************************************************
4
EX.NO: 2
DATE:
AIM:
To write a Python Program to display Fibonacci Series up to ‘n’ numbers.
SOURCE CODE:
5
SAMPLE OUTPUT:
RUN -1:
RUN -2 :
*****************************************************************************************
6
EX.NO: 3
DATE:
AIM:
To write a menu driven Python Program to find Factorial and sum of list of numbers
using function.
SOURCE CODE:
7
SAMPLE OUTPUT:
RUN – 2:
**************************************************************************************
8
EX.NO: 4
DATE:
AIM:
To write a Python program to implement python mathematical functions to find:
(i) To find Square of a Number.
(ii) To find Log of a Number(i.e. Log10)
SOURCE CODE:
SAMPLE OUTPUT:
**************************************************************************************
9
EX.NO: 5
DATE:
AIM:
Program to find simple interest using a user defined function with parameters and with
return value.
SOURCE CODE:
def interest(p1,r1,t1 ):
i=p*r*t/100
return(i)
p=int(input("Enter principle="))
r=int(input("Enter rate="))
t=int(input("Enter year="))
in1=interest(p,r,t)
print("Interest=",in1)
10
SAMPLE OUTPUT:
11
EX.NO: 6
DATE:
SOURCE CODE:
12
Given Input:
Emerging technologies are technologies whose development, practical applications, or both are still largely unrealized.
These technologies are generally new but also include older technologies finding new applications.
Emerging technologies are often perceived as capable of changing the status quo.
SAMPLE OUTPUT:
13
EX.NO: 7
To create a text file and write data in it
# program to create a text file and add data
with open('data.txt', 'w') as file:
file.write("Hello, world!\n")
file.write("This is a text file.\n")
with open('data.txt', 'r') as file:
content = file.read()
print(content)
-------------------------------------------------------------------
#read date from file
#with open('data.txt', 'r')as file:
# content = file.read()
# print(content)
14
Program :8
AIM:To write a Python program to generate random number between 1 to 6 to simulate the dice.
import random
while True:
choice = input("Do you want to roll the dice (y/n): ").strip().lower()
if choice == 'y':
no = random.randint(1, 6)
# Corrected function name and indentation
print(f"\nYou rolled a: {no}")
elif choice == 'n':
break
else:
print("Please enter 'y' for yes or 'n' for no.")
15
Pgm 9: To CREATING A PYTHON PROGRAM TO READ A TEXT FILE LINE BY LINE
AND DISPLAY EACH WORD SEPARATED BY '#'
f=open("data.txt",'r')
contents = f.readlines()
# The line contents = f.readlines() reads all the lines from the file object f and stores them as a list of strings in the
variable contents.
for line in contents:
words=line.split()
# The line words = line.split() splits the string line into a list of words.
for i in words:
print(i+'#',end=' ')
# print(i+'#',end=' ')The line print(i+'#', end=' ') is a part of a loop, and it's used to print each word from a list followed
by the # symbol, all on the same line with spaces in between.
print(" ")
f.close()
16
EX.NO: 10
CREATING A PYTHON PROGRAM TO READ A TEXT FILE AND DISPLAY THE NUMBER OF
VOWELS/CONSONANTS/LOWER CASE/ UPPER CASE CHARACTERS.
AIM:
f=open("data.txt",'r')
contents=f.read()
# The line contents = f.read() reads the entire content of the file f and stores it in the variable contents.
#intialize counters
vowels=0
consonants = 0
lowercase=0
uppercase=0
for ch in contents:
if ch in 'aeiouAEIOU':
vowels += 1
# vowels += 1 increments the vowels count by 1 each time a vowel is found.
if ch in 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ':
consonants += 1
if ch.islower():
lowercase += 1
if ch.isupper():
uppercase += 1
f.close()
print("the total number of vowels in the list",vowels)
print("the total number of consonts in the list",consonants)
print("the total number of uppercase in the list",lowercase)
print("the total number of lowercase in the list",uppercase)
*****************************************************************************************
17
11. To CREATING A PYTHON PROGRAM TO COPY PARTICULAR LINES OF A TEXT FILE
INTO AN ANOTHER TEXT FILE
F1=open("sample.txt",'r')
F2=open("new.txt",'w')
while True:
line=F1.readline()
if line=='':
break
if line[0]=='a' or line[0]=='A':
F2.write(line)
print("all lines are starting")
F1.close()
F2.close()
18
12. To write a Python Program to Create a binary file with roll number and name. Search for a given roll
number and display the name, if not found display appropriate message.
import pickle
def Create():
F = open("student.dat", 'ab') # Use append binary mode
opt = 'y'
while opt == 'y':
Roll_No = int(input('Enter roll number: '))
Name = input("Enter Name: ")
L = [Roll_No, Name]
pickle.dump(L, F) # Use pickle to dump the list into the file
opt = input("Do you want to add another student detail(y/n): ")
F.close()
def Search():
F = open("student.dat", 'rb') # Use read binary mode
no = int(input("Enter the roll number of student to search: "))
found = 0
try:
while True:
S = pickle.load(F) # Use pickle to load the object from the file
if S[0] == no:
print("The searched Roll No found and Details are:", S)
found = 1
except EOFError:
F.close()
if found == 0:
print("The searched roll number is not found.")
# Main Program
Create()
Search()
19
20
EX.NO: 13
DATE:
To write a Python Program to Create a binary file with roll number, name, mark
and update/modify the mark for a given roll number.
SOURCE CODE:
21
SAMPLE OUTPUT:
**********************************************************************************
22
EX.NO: 14
DATE:
AIM:
To write a Python program Create a CSV file to store Empno, Name, Salary and search
any Empno and display Name, Salary and if not found display appropriate message.
SOURCE CODE:
23
SAMPLE OUTPUT:
PYTHON PROGRAM EXECUTED OUTPUT:
******************************************************************************
24
EX.NO: 15
DATE:
25
SAMPLE OUTPUT:
Python Program Executed Output:
31
****************************************************************************************
32
EX.NO: 16
DATE:
AIM:
Write a program to implement a stack for the employee details (empno, name).
SOURCE CODE:
stk=[]
top=-1
def line():
print('~'*100)
def isEmpty():
global stk
if stk==[]:
print("Stack is empty!!!")
else:
None
def push():
global stk
global top
empno=int(input("Enter the employee number to push:"))
ename=input("Enter the employee name to push:")
stk.append([empno,ename])
top=len(stk)-1
def display():
global stk
global top
if top==-1:
isEmpty()
else:
top=len(stk)-1
print(stk[top],"<-top")
for i in range(top-1,-1,-1):
print(stk[i])
def pop_ele():
global stk
global top
if top==-1:
isEmpty()
else:
stk.pop()
top=top-1
33
def main():
while True:
line()
print("1. Push")
print("2. Pop")
print("3. Display")
print("4. Exit")
ch=int(input("Enter your choice:"))
if ch==1:nm
push()
print("Element Pushed")
elif ch==2:
pop_ele()
elif ch==3:
display()
else:
print("Invalid Choice")
34
SAMPLE OUTPUT:
35
EX.NO: 17
DATE:
CREATING A PYTHON PROGRAM TO INTEGRATE MYSQL WITH PYTHON
AIM:
To write a Python Program to integrate MYSQL with Python.
SOURCE CODE:
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect(host='localhost',
database='schooldb',
user='Student',
password=’Student@123’)
if connection.is_connected():
db_Info = connection.get_server_info()
print("Connected to MySQL Server version ", db_Info)
cursor = connection.cursor()
cursor.execute("select database();")
record = cursor.fetchone()
print("You're connected to database: ", record)
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
36
OUTPUT:
37
EX.NO: 18
DATE:
SOURCE CODE:
38
OUTPUT:
SQL OUTPUT:
39
EX.NO: 19
DATE:
CREATING A PYTHON PROGRAM TO INTEGRATE MYSQL WITH PYTHON
(SEARCHING AND DISPLAYING RECORDS)
AIM:
To write a Python Program to integrate MYSQL with Python to search an Employee using
EMPID and display the record if present in already existing table EMP, if not display the
appropriate message.
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is verified.
40
SAMPLE OUTPUT:
RUN -1:
Run – 2:
SQL OUTPUT:
OUTPUT -1:
OUTPUT -2:
41
EX.NO: 20
DATE:
CREATING A PYTHON PROGRAM TO INTEGRATE MYSQL WITH PYTHON
(UPDATING RECORDS)
AIM:
To write a Python Program to integrate MYSQL with Python to search an Employee using
EMPID and update the Salary of an employee if present in already existing table EMP,
if not display the appropriate message.
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is verified.
42
SAMPLE OUTPUT:
RUN -1:
Run -2:
SQL OUTPUT:
43
SQL COMMANDS EXERCISE - 1
Ex.No: 21
DATE:
AIM:
To write Queries for the following Questions based on the given table:
Sol:
44
(e) Write a Query to List all the tables that exists in the current database.
Sol:
mysql> SHOW TABLES;
Output:
(f) Write a Query to insert all the rows of above table into Info table.
Sol:
INSERT INTO STU VALUES (1,'Arun','M', 24,'COMPUTER','1997-01-10', 120);
(g) Write a Query to display all the details of the Employees from the above table 'STU'.
Sol:
Output:
(h) Write a query to Rollno, Name and Department of the students from STU table.
Sol:
mysql> SELECT ROLLNO,NAME,DEPT FROM STU;
45
Ex.No: 22 SQL COMMANDS EXERCISE – 2
DATE:
AIM:
To write Queries for the following Questions based on the given table:
Output:
Output:
Output:
46
(d) Write a Query to list name of the students whose ages are between 18 to 20.
Sol:
mysql> SELECT NAME FROM STU WHERE AGE BETWEEN 18 AND 20;
Output:
(e) Write a Query to display the name of the students whose name is starting with 'A'.
Sol:
Output:
(f) Write a query to list the names of those students whose name have second alphabet 'n' in their
names.
Sol:
********************************************************************************************************
47
Ex.No: 23 SQL COMMANDS EXERCISE - 3
DATE:
AIM:
To write Queries for the following Questions based on the given table:
(b) Write a Query to change the fess of Student to 170 whose Roll number is 1, if the existing fess
is less than 130.
Sol:
Output(After Update):
48
(c) Write a Query to add a new column Area of type varchar in table STU.
Sol:
mysql> ALTER TABLE STU ADD AREA VARCHAR(20);
Output:
(d) Write a Query to Display Name of all students whose Area Contains NULL.
Sol:
mysql> SELECT NAME FROM STU WHERE AREA IS NULL;
Output:
(e) Write a Query to delete Area Column from the table STU.
Sol:
mysql> ALTER TABLE STU DROP AREA;
Output:
Sol:
mysql> DROP TABLE STU;
Output:
*******************************************************************************************
49
Ex.No: 24 SQL COMMANDS EXERCISE - 4
DATE:
AIM:
To write Queries for the following Questions based on the given table:
TABLE: STOCK
Pno Pname Dcode Qty UnitPrice StockDate
5005 Ball point pen 102 100 10 2021-03-31
5003 Gel pen premium 102 150 15 2021-01-01
5002 Pencil 101 125 4 2021-02-18
5006 Scale 101 200 6 2020-01-01
5001 Eraser 102 210 3 2020-03-19
5004 Sharpner 102 60 5 2020-12-09
5009 Gel pen classic 103 160 8 2022-01-19
TABLE: DEALERS
Dcode Dname
101 Sakthi Stationeries
103 Classic Stationeries
102 Indian Book House
(a) To display the total Unit price of all the products whose Dcode as 102.
Sol:
mysql> SELECT SUM(UNITPRICE) FROM STOCK GROUP BY DCODE HAVING
DCODE=102;
Output:
(b) To display details of all products in the stock table in descending order of Stock date.
Sol:
Output:
50
(c) To display maximum unit price of products for each dealer individually as per dcode
from the table Stock.
Sol:
Output:
(d) To display the Pname and Dname from table stock and dealers.
Sol:
mysql> SELECT PNAME,DNAME FROM STOCK S,DEALERS D WHERE S.DCODE=D.DCODE;
Output:
51
Ex.No: 25 SQL COMMANDS EXERCISE - 5
DATE:
AIM:
To write Queries for the following Questions :
a. Write a query to display cube of 5.
b. Write a query to display the number 563.854741 rounding off to the next hundred.
c. Write a query to display "put" from the word "Computer".
d. Write a query to display today's date into DD.MM.YYYY format.
e. Write a query to display 'DIA' from the word "MEDIA".
Sol:
a. select pow(5,3);
b. select round(563.854741,-2);
c. select mid(“Computer”,4,3);
52
e. select right("Media",3);
53