Gr12 - Prac Record Programs - 24-25

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

EX.

NO: 1

DATE:

CREATING A MENU DRIVEN PROGRAM TO PERFORM ARITHMETIC OPERATIONS


AIM:

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:

CREATING A PYTHON PROGRAM TO IMPLEMENT RETURNING


VALUE(S) FROM FUNCTION

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:

CREATING A PYTHON PROGRAM TO IMPLEMENT MATHEMATICAL FUNCTIONS 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)
(iii) To find Quad of a Number

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:

CREATING A PYTHON PROGRAM TO GENERATE RANDOM NUMBER


BETWEEN 1 TO 6
AIM:

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:

Python Executed Output Program:

**********************************************************************************

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 :-

Write a program to perform read and write operations


with csv file.
Program:-
import csv
f = open("students.csv","w",newline='')
dt = csv.writer(f)
dt.writerow(['Student_ID','StudentName','Score'])
f.close()
#Insert Data
f = open("students.csv","a",newline='\n')
ch='y'
while ch=='y':
st_id= int(input("Enter Student ID:"))
st_name = input("Enter Student name:")
st_score = int(input("Enter score:"))
dt = csv.writer(f)
dt.writerow([st_id,st_name,st_score])
ch=input("Want to insert More records?(y or n):")
ch=ch.lower()
print("Record has been added.")
f.close()
f = open("students.csv",'r')
rt=csv.reader(f)
for i in rt:
print(i)
f.close()

RESULT:-

The program for performing read and write operation


in csv file was run successfully, and the related output
was obtained.
Exp. No: 15
STACK MENU DRIVEN PROGRAMMING
Aim :-
Write a menu driven program to implement Stack operations.
Program:-
s=[]
while True:
print("1. Push")
print("2. Pop")
print("3. Display")
print("4. Size of Stack")
print("5. Value at Top")
choice=int(input("Enter your choice(1-5): "))
if choice == 1:
val=input("Enter the element: ")
s.append(val)
elif choice==2:
if s==[]:
print("Stack is empty")
else:
print("Deleted element is ", s.pop())
elif choice==3:
print(s)
elif choice==4:
print("Size of the stack is ", len(s))
elif choice==5:
print("Value of top element is ", s[len(s)-1])
else:
print("You entered a wrong choice")
option=input("Do you want to continue? Y/N: ")
if option in 'Nn':
break

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(

3) List the name and department of those employees whose


department is production.
Ans:-
SELECT Ename, Dept FROM EMPLOYEE
WHERE Dept = ‘production’;

4) Display the name and salary of those employees whose


salary is more than 20000.
Ans :-

SELECT Ename, Salary FROM EMPLOYEE


where Salary > 20000;

5) Display the names of those employees who live in Dubai.


Ans:-

SELECT Ename FROM EMPLOYEE WHERE City ='Dubai’;


6) Display the name and department of those employees
who live in Ajman and whose salary is greater than 25000.

Ans:-

SELECT Ename, Dept FROM EMPLOYEE


WHERE city='Ajman' and salary >25000;

RESULT:-

The SQL queries were run successfully, and the related


outputs were obtained.

Exp. No: 17
SQL EXERCISE – 2
Aim :-

Write SQL Queries using order by and aggregate functions.


QUERIES:-

1) Display the details of employees in descending order


of employee code.
Ans:-
SELECT *FROM EMPLOYEE ORDER BY Ecode DESC;

2) Find the average salary for each department.


Ans:-
SELECT Dept, Avg(Salary) FROM EMPLOYEE group by Dept;

3) Find the total salary of those employees who work


in Ajman city.
Ans:-
SELECT sum(salary) FROM EMPLOYEE WHERE City =
'Ajman';
4) Find the number of tuples in the EMPLOYEE
relation. Ans:-
SELECT count(*) FROM EMPLOYEE;

5) Find the maximum salary of a male employee


in EMPLOYEE table.

Ans:-
SELECT Ename, max(salary) FROM EMPLOYEE WHERE Gender
='M'

6) Display the employee names whose second alphabet is 'o' .


Ans:-
SELECT Ename FROM EMPLOYEE WHERE Ename LIKE '_o%';

RESULT:-

The SQL queries were run successfully, and the


related outputs were obtained.
Exp. No: 18
SQL EXERCISE – 3
Aim :-
To execute SQL Queries for the following tasks.
QUERIES:-

1) Delete the details of the employee whose Ecode is 2314.

Ans:
DELETE FROM EMPLOYEE WHERE Ecode=2314;

2) Change the salary of employee to 13000 whose Ecode is 1356,


if the existing salary is less than 12000.

Ans: UPDATE EMPLOYEE SET salary=13000 WHERE


Ecode=1356 AND Salary < 12000;

3) Add a new column Bonus of type float to table Employee.


Ans: ALTER TABLE EMPLOYEE ADD Bonus float (12,2);

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:-

The SQL queries were run successfully, and the


related outputs were obtained.
Exp. No: 19
PYTHON MYSQL CONNECTIVITY
EXERCISE – 1
Aim :-
To write a Python program to integrate MYSQL with Python, to create
Database and Table to store the details of students.

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( )

a=int(input("Enter student id to search: "))


q="select * from Student where ID={}".format(a)
cur.execute(q)
data=cur.fetchone()
if data!=None:
print(data)
else:
print("Record not found.")
conn.close()

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( )

a=int(input("Enter student id to update: "))


q="select * from Student where ID={}".format(a)
cur.execute(q)
data=cur.fetchone()
if data!=None:
print("Record found. The details are: ")
print(data)
b=input("Enter the new address of the student: ")
r="update Student set Address='{}' where Id={}".format(b,a)
cur.execute(r)
conn.commit()
print("Address updated.")
cur.execute("select * from Student")
studata=cur.fetchall()
for i in studata:
print(studata)
else:
print("Record not found")
conn.close()
Result:-
The Python program to update a student detail and display the data
was run successfully and the related output was obtained

You might also like