0% found this document useful (0 votes)
4 views6 pages

Practical File

The document contains multiple Python programming tasks including reading and processing text files, generating random numbers, creating and searching binary files, and executing SQL queries to retrieve employee and teacher information. Each task is accompanied by code snippets and example outputs demonstrating the expected functionality. The tasks cover file handling, data manipulation, and basic database operations.

Uploaded by

sivakathiravan12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Practical File

The document contains multiple Python programming tasks including reading and processing text files, generating random numbers, creating and searching binary files, and executing SQL queries to retrieve employee and teacher information. Each task is accompanied by code snippets and example outputs demonstrating the expected functionality. The tasks cover file handling, data manipulation, and basic database operations.

Uploaded by

sivakathiravan12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Write a Python program to read a text file line by line and display each word
separated by a ‘#’.
f = open("[Link]", "r")
line = [Link]()
words = [Link]()
for word in words:
print(word, end="#")
[Link]()

[Link]
Welcome to Python

OUTPUT
Welcome#to#Python#

with open(file_name, "r") as file:


for line in file:
words = [Link]().split()
print("#".join(words))

Hello world
Python is fun

Hello#world
Python#is#fun

2. Write a python program for random number generation that generated


random numbers between 1 to 6.

import random
while True:
number = [Link](1, 6)
print ("Random number:", number)
choice = input("Do you want to generate another number? (y/n):").lower()
if choice != "y":
break

OUTPUT
Random number: 4
Do you want to generate another number? (yes/no): yes
Random number: 2
Do you want to generate another number? (yes/no): yes
Random number: 6
Do you want to generate another number? (yes/no): no

3. Write a Python program to create a binary file with name and roll number.
Search for a given roll number and display the name, if not found display the
appropriate message.

import pickle
def create_students():
f=open("[Link]", "wb")
n = int(input("How many students do you want to add? "))
for i in range(n):
name = input("Enter student name: ")
roll = int(input("Enter roll number: "))
stud=[name,roll]
[Link](stud, f)
[Link]()
print("\nStudent data saved successfully!\n")

def search_student():
search_roll = int(input("Enter roll number to search: "))
f=open([Link]", "rb")
data = [Link](f)
found = False
for student in data:
if student["roll"] == search_roll:
print("Name of the student:", student["name"])
found = True
break
if not found:
print("Roll number not found!")

create_students()
search_student()
OUTPUT
How many students do you want to add? 2
Enter student name: Alice
Enter roll number: 101
Enter student name: Bob
Enter roll number: 102

Student data saved successfully!

Enter roll number to search: 102


Name of the student: Bob

Enter roll number to search: 105


Roll number not found!
4. Write a Python program to read lines from a text file “[Link]” and copy
those lines into another file which are starting with an alphabet ‘a’, or ‘A’.
f1 = open("[Link]", "r")
f2 = open("[Link]", "w")
s=[Link]()
for i in s:
if i[0].lower() == 'a':
[Link](line + "\n")
[Link]()
[Link]()

OUTPUT
[Link]
Apple pie
banana cake
Avocado salad
Orange juice

[Link]
Apple pie
Avocado salad
5. Write a Python program to read lines from [Link] and display those words
which are less than 5 characters.

f = open("[Link]", "r")
s = [Link]()
w=[Link]()
for i in w:
if len(i) < 5:
print(i, end=" ")
[Link]()

[Link]

Roses are red


Violets are blue
Sugar is sweet

OUTPUT
are red
are blue
is

with open("[Link]", "r") as f:


for line in f:
words = [Link]()
for word in words:
if len(word) < 5:
print(word, end=" ")

6.
a) To display details of all employee in descending order of their
DOJ.
SELECT * FROM EMPLOYEE ORDER BY DOJ DESC;

b) To display Name and desig of those employee whose sgrade is


either ‘SO2’ or ‘SO3’
SELECT NAME, DESIG FROM EMPLOYEE WHERE SGRADE IN ('SO2',
'SO3');

c) To display number of employee working in each sgrade from the table.


SELECT SGRADE, COUNT(*) AS TOTAL_EMPLOYEES FROM
EMPLOYEE GROUP BY SGRADE;

d) To display name, desig, salary, hra from the tables employee


and sgrade where salary is less than 50000.
SELECT [Link], [Link], [Link], [Link] FROM EMPLOYEE E
JOIN SALGRADE S ON [Link] = [Link] WHERE [Link] <
50000;
OR
SELECT [Link], [Link], [Link], [Link] FROM EMPLOYEE E,
SALGRADE S WHERE [Link] = [Link] AND [Link] <
50000;
OR
SELECT NAME, DESIG, SALARY, HRA FROM EMPLOYEE,
SALGRADE WHERE [Link] = [Link]
AND [Link] < 50000;

7.
i)To show all information about the teacher of history department
SELECT * FROM TEACHER WHERE Department = 'History';

ii)To list the names of female teacher who are in History department
SELECT Name FROM TEACHER WHERE Department = 'History' AND
Sex = 'F';

iii)To list names of all teachers with their date of joining in ascending order.
SELECT Name, Date_of_join FROM TEACHER ORDER BY Date_of_join
ASC;

iv)To display teacher’s Name, Salary, Age for male teacher only
SELECT Name, Salary, Age FROM TEACHER WHERE Sex = 'M';

You might also like