Gr12 - Prac Record Programs - 24-25
Gr12 - Prac Record Programs - 24-25
Gr12 - Prac Record Programs - 24-25
NO: 1
DATE:
To write a menu driven Python Program to perform Arithmetic operations (+,-*,/) based on
the user’s choice.
SOURCE CODE:
Result:
Thus, the above Python program has been executed and the output is verified
successfully.
3
EX.NO: 2
DATE:
CREATING A PYTHON PROGRAM TO DISPLAY FIBONACCI SERIES
AIM:
To write a Python Program to display Fibonacci Series up to ‘n’ numbers.
SOURCE CODE:
Result:
Thus, the above Python program has been executed and the output is verified
successfully.
5
EX.NO: 3
DATE:
CREATING A MENU DRIVEN PROGRAM TO FIND FACTORIAL AND SUM OF LIST OF NUMBERS
USING FUNCTION.
AIM:
To write a menu driven Python Program to find Factorial and sum of list of numbers using
function.
SOURCE CODE:
Result:
Thus, the above Python program has been executed and the output is verified
successfully.
7
EX.NO: 4
DATE:
AIM:
To Write a Python program to define the function Check(no1,no2) that take two numbers and Returns
the number that has minimum ones digit.
Source Code:
Result:
Thus, the above Python program has been executed and the output is verified
successfully.
Sample Output:
**********************************************************************************
9
EX.NO: 5
DATE:
SOURCE CODE:
Result:
Thus, the above Python program has been executed and the output is verified
successfully.
SAMPLE OUTPUT:
Python Executed Program Output:
***************************************************************************
10
EX.NO: 6
DATE:
To write a Python program to generate random number between 1 to 6 to simulate the dice.
SOURCE CODE:
Result:
Thus, the above Python program has been executed and the output is verified
successfully.
SAMPLE OUTPUT:
**********************************************************************************
11
Exp. No: 7
WRITING AND READING A TEXT FILE
Aim :-
Write a program to write and read text in a file named text.txt.
PROGRAM :-
f = open("text.txt", "w+")
f.write("Welcome to File Handling in python.\n")
f.write("Python file handling is very interesting and useful.\n")
f.write("File refers to the collection of bytes stored in
computer storage.\n")
f.write("File handling refers to the process of handling data
using software for IO operations.\n") f.seek(0)
content=f.read()
print (content)
f.close()
RESULT:-
The program for writing and reading the text file was
run successfully, and the related output was obtained.
Exp. No: 8
DISPLAY THE NUMBER OF LINES AND SIZE OF A TEXT FILE IN BYTES
Aim :-
Write a program to display the number of lines and size of
a text file in bytes.
PROGRAM :-
myfile=open("text.txt",'r')
cont1=myfile.read( )
size=len(cont1)
print("Size of the file is", size,"bytes")
myfile.seek(0)
cont2=myfile.readlines()
linecount=len(cont2)
print("Number of lines in the file is",linecount)
myfile.close()
RESULT:-
The program to display the number of lines and size of
a text file in bytes was run successfully, and the related
output was obtained.
Exp. No: 9
COUNTING NUMBER OF VOWELS IN A TEXT FILE
Aim :-
Write a program to count the number of vowels present in
a text file.
PROGRAM :-
fin=open("text.txt",'r')
str=fin.read( )
count=0
for i in str:
if i=='a' or i=='e' or i=='i' or i=='o' or i=='u':
count=count+1
print(“The number of vowels in the file is ”, count)
RESULT:-
The program to count the number of vowels was run
successfully, and the related output was obtained.
Exp. No: 10
COUNTING NUMBER OF WORDS IN A TEXT FILE
Aim :-
Write a program to count number of words in a text file.
PROGRAM :-
fin=open("text.txt ",'r')
str=fin.read( )
L=str.split()
count_words=0
for i in L:
count_words=count_words+1
print(“The number of words in the file is “, count_words)
RESULT:-
The program for counting number of words was run
successfully, and the related output also obtained.
Exp. No: 11
READ A TEXT FILE LINE BY LINE AND DISPLAY
EACH WORD SEPARATED BY #
Aim :-
Write a program to read a text file line by line and display
each word separated by '#'.
PROGRAM :-
fin=open("text.txt",'r')
cont=fin.read()
L=cont.split()
s=' '
for j in L:
s=s+j
s=s+'#'
print(s)
fin.close( )
RESULT:-
The program to read a text file line by line and display each
word separated by # was run successfully, and the related
output was obtained.
Exp. No: 12
CREATE A BINARY FILE WITH NAME, ROLL AND
MARKS OF STUDENTS AND SEARCH FOR A RECORD
Aim :-
To create a binary file with name, roll number and marks and
search a record using its roll number and display the data.
Program:-
import pickle
def create():
stufile = open("stu1.dat","ab")
while True:
roll = int(input("Enter student Roll No: "))
name = input("Enter student Name: ")
marks=float(input("Enter marks: "))
stu= [roll,name,marks]
pickle.dump(stu,stufile)
ans= input("Want to add more record(y/n): ")
if ans not in 'Yy':
break
stufile.close()
def search():
stufile = open("stu1.dat","rb")
no=int(input("Enter roll no. of student to be searched: "))
found=0
try:
while True:
data = pickle.load(stufile)
if data[0]==no:
print("The details are: ",data)
found=1
break
except:
stufile.close()
if found==0:
print("Searched record not found!")
create()
search()
RESULT:-
The program for creating a binary file with name, roll
number and marks of students and searching a record
was run successfully, and the related output was obtained.
Exp. No: 13
CREATE A BINARY FILE WITH NAME, ROLL AND
MARKS OF STUDENTS AND UPDATE A RECORD
Aim :-
To create a binary file with name, roll number and marks and
update a record using its roll number and display the data.
Program:-
import pickle
def create():
stufile = open("studentup.dat","ab")
while True:
roll = int(input("Enter student Roll No: "))
name = input("Enter student Name: ")
marks=float(input("Enter marks: "))
stu= [roll,name,marks]
pickle.dump(stu,stufile)
ans= input("Want to add more record(y/n): ")
if ans not in 'Yy':
break
stufile.close()
def update():
stufile = open("studentup.dat","rb+")
no=int(input("Enter roll no. of student to be updated: "))
found=0
try:
while True:
data = pickle.load(stufile)
if data[0]==no:
print("The details are: ",data)
newmark=int(input("Enter the new mark: "))
data[2]=newmark
print("Record updated. The details are: ",data)
found=1
break
except:
stufile.close()
if found==0:
print("Searched record not found!")
create()
update()
RESULT:-
The program for creating a binary file with name, roll
number and marks of students and updating a record was run
successfully, and the related output was obtained.
Exp. No: 14
PERFORMING READ AND WRITE OPERATIONS IN CSV FILE
Aim :-
RESULT:-
RESULT:-
The program for implementing stack operations was
run successfully, and the related output was obtained.
Exp. No: 16
SQL EXERCISE – 1
Aim :-
Write a SQL Program to execute the following Queries for the
table mentioned below.
Ecode Ename Dept City Gender DOJ Salary
1048 Abdul Production Ajman M 1997-01-10 15000
1356 John Marketing Sharjah M 1998-03-24 11500
2541 Sonia Accounts Dubai F 1996-12-12 8000
2314 Samar Production Sharjah M 1999-07-01 16500
1572 Arun Accounts Dubai M 1997-09-05 12000
1189 Deepa Production Ajman F 1997-06-27 15500
QUERIES:-
1) To Create a table EMPLOYEE with the following fields: Ecode,
Ename, Dept, City, Gender, DOJ and Salary. Set Ecode as the Primary key
of EMPLOYEE.
Ans:-
CREATE TABLE EMPLOYEE(Ecode int(6) primary key,
Ename varchar(25), Dept varchar(15), City(15),
Gender(1), DOJ date, Salary float (12,2);
2) Insert 5 records into the table named Employee.
Ans:-
● INSERT INTO EMPLOYEE
VALUES(1048,’Abdul’,’Production’,’Ajman’,’M’,’1997-01-
10’,15000)
● INSERT INTO EMPLOYEE VALUES(
● INSERT INTO EMPLOYEE VALUES(
● INSERT INTO EMPLOYEE VALUES(
● INSERT INTO EMPLOYEE VALUES(
Ans:-
RESULT:-
Exp. No: 17
SQL EXERCISE – 2
Aim :-
Ans:-
SELECT Ename, max(salary) FROM EMPLOYEE WHERE Gender
='M'
RESULT:-
Ans:
DELETE FROM EMPLOYEE WHERE Ecode=2314;
4)Delete the column Bonus from the table Employee. Ans: ALTER
TABLE EMPLOYEE DROP Bonus;
5)Delete the table Employee from the database. Ans: DROP TABLE
EMPLOYEE;
RESULT:-
Program:-
import mysql.connector
conn= mysql.connector.connect(host="localhost", user="root",
passwd="12345")
cur=conn.cursor( )
q="create database Education"
cur.execute(q)
print("Database created")
cur.execute("use Education")
r="create table Student (ID int primary key, Name varchar(25), Age
int(3), Phone int,Address varchar(15))" cur.execute(r)
print("Table created")
conn.close()
Result:-
The Python program to create a Database and Table was run
successfully, and the related output was obtained.
Exp. No: 20
PYTHON MYSQL CONNECTIVITY
EXERCISE – 2
Aim :-
To write a Python program to integrate MYSQL with Python, insert
students’ details and display the data.
Program:-
import mysql.connector
conn= mysql.connector.connect(host="localhost", user="root",
passwd="12345", database="Education")
cur=conn.cursor( )
opt='y'
while opt=='y':
v=int(input("Enter student id: "))
w=input("Enter student name: ")
x=int(input("Enter student age: "))
y=int(input("Enter student contact no.: "))
z=input("Enter student address: ")
q="insert into Student values ({},'{}',{},{},'{}')".format(v,w,x,y,z)
cur.execute(q)
conn.commit()
print("Record added")
opt=input("Do you want to add more?(y/n) ")
cur.execute("select * from Student")
data=cur.fetchall()
for i in data:
print(i)
conn.close()
Result:-
The Python program to insert student details to the table and display
the data was run successfully and the related output was obtained.
Exp. No: 21
PYTHON MYSQL CONNECTIVITY
EXERCISE – 3
Aim :-
To write a Python program to integrate MYSQL with Python, search
for a student detail and display the data.
Program:-
import mysql.connector
conn= mysql.connector.connect(host="localhost", user="root",
passwd="12345", database="Education") cur=conn.cursor( )
Result:-
The Python program to search a student detail and display the data
was run successfully and the related output was obtained.
Exp. No: 22
PYTHON MYSQL CONNECTIVITY
EXERCISE – 4
Aim :-
To write a Python program to integrate MYSQL with Python, update a
student detail and display the data.
Program:-
import mysql.connector
conn= mysql.connector.connect(host="localhost", user="root",
passwd="12345", database="Education") cur=conn.cursor( )