Computer Practical Programs
Computer Practical Programs
vowels = "aeiouAEIOU"
with open("file.txt", "r") as file:
text = file.read()
Output:
3. Create a binary file with name and roll no. Search for a roll no and display the name:
import pickle
def write_binary_file():
students = {101: 'Alice', 102: 'Bob', 103: 'Charlie'}
with open('students.dat', 'wb') as file:
pickle.dump(students, file)
def search_roll_no(roll_no):
with open('students.dat', 'rb') as file:
students = pickle.load(file)
name = students.get(roll_no, "Not Found")
print(f"Roll No: {roll_no}, Name: {name}")
write_binary_file()
search_roll_no(102)
stack = []
def push(item)
stack.append(item)
print(f"Pushed {item} onto the stack.")
def pop():
if is_empty():
return "Stack is empty, cannot pop."
return stack.pop()
def is_empty():
return len(stack) == 0
def peek():
if is_empty():
return "Stack is empty."
return stack[-1]
def display_stack():
print("Stack:", stack)
display_stack()
push(10)
push(20)
push(30)
display_stack()
Popped element: 30
Stack: [10, 20]
Top element: 20
Stack: [10, 20]
def SUM3(L):
total = 0
for num in L:
if str(num).endswith("3"):
total += num
print(total)
6. Sort a list using bubble sort and display in ascending and descending order:
def bubble_sort(arr, reverse=False):
n = len(arr)
for i in range(n-1):
for j in range(n-i-1):
if (arr[j] > arr[j+1] and not reverse) or (arr[j] < arr[j+1] and reverse):
arr[j], arr[j+1] = arr[j+1], arr[j]
def display_sorted_list():
L = [64, 25, 12, 22, 11]
bubble_sort(L)
print("Ascending:", L)
bubble_sort(L, reverse=True)
print("Descending:", L)
display_sorted_list()
def find_max_length_word(text):
words = text.split()
max_word = ""
for word in words:
if len(word) > len(max_word):
max_word = word
print(f"Word with max length: {max_word}")
def is_palindrome(text):
text = text.lower()
return text == text[::-1]
import csv
def write_csv():
with open('stu.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Roll', 'Name', 'Stream', 'Marks'])
writer.writerow([1, 'Alice', 'CS', 90])
writer.writerow([2, 'Bob', 'Math', 85])
def read_csv():
with open('stu.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
write_csv()
read_csv()
10. Store names and phone numbers in a dictionary and search for a name:
def phone_directory():
directory = {}
for _ in range(int(input("Enter number of entries: "))):
name = input("Enter name: ")
phone = input("Enter phone number: ")
directory[name] = phone
phone_directory()
SQL Questions
1. Employee and Department Queries:
/* a.> */ SELECT deptid, MAX(salary), MIN(salary) FROM Employee GROUP BY deptid;
/* b.> */ SELECT name FROM Employee WHERE name LIKE 'a%';
/* c.> */ SELECT Employee.name, Department.deptname FROM Employee JOIN Department ON Employee.deptid =
Department.deptid;
/* d.> */ SELECT name, empid FROM Employee ORDER BY dob;
/* e.> */ UPDATE Employee SET salary = 95000 WHERE name = 'Ramesh';
2. Student Queries:
Python-MySQL Connectivity
# A.
import mysql.connector
cursor.execute('''
CREATE TABLE student (
name VARCHAR(255),
class INT,
roll INT PRIMARY KEY,
stipend DECIMAL(10, 2)
)
''')
conn.commit()
cursor.close()
conn.close()
# B.
conn = mysql.connector.connect(host='localhost', user='root', password='password', database='school')
cursor = conn.cursor()
cursor.close()
conn.close()
# C.
conn = mysql.connector.connect(host='localhost', user='root', password='password', database='school')
cursor = conn.cursor()
cursor.execute("INSERT INTO student (name, class, roll, stipend) VALUES ('Alice', 12, 101, 8000)")
cursor.execute("INSERT INTO student (name, class, roll, stipend) VALUES ('Bob', 10, 102, 9000)")
conn.commit()
cursor.close()
conn.close()
# D.
conn = mysql.connector.connect(host='localhost', user='root', password='password', database='school')
cursor = conn.cursor()
conn.commit()
cursor.close()
conn.close()