Set A B C Answer.
Set A B C Answer.
1. Python Program: Write a Python program to remove all the lines that contain a
specific character from a file and write the remaining lines to another file.
Answer:
python
Copy code
def remove_lines_with_char(input_file, output_file,
char):
with open(input_file, 'r') as infile,
open(output_file, 'w') as outfile:
for line in infile:
if char not in line:
outfile.write(line)
# Example usage
remove_lines_with_char('input.txt', 'output.txt', 'a')
2.SQL Commands: Create a student table and insert data, then implement the following
SQL commands:
ALTER TABLE to add new attributes, modify data type, or drop attributes:
sql
Copy code
ALTER TABLE student ADD COLUMN age INT;
ALTER TABLE student MODIFY COLUMN name VARCHAR(100);
ALTER TABLE student DROP COLUMN age;
sql
Copy code
UPDATE student SET name = 'John Doe' WHERE roll_no = 1;
sql
Copy code
SELECT * FROM student ORDER BY name ASC; -- For ascending
order
SELECT * FROM student ORDER BY name DESC; -- For descending
order
SET B
Answer:
python
Copy code
class Stack:
def __init__(self):
self.stack = []
def pop(self):
if self.stack:
item = self.stack.pop()
print(f"Popped: {item}")
return item
else:
print("Stack is empty")
return None
def peek(self):
if self.stack:
print(f"Peek: {self.stack[-1]}")
return self.stack[-1]
else:
print("Stack is empty")
return None
def view(self):
print(f"Stack contents: {self.stack}")
# Example usage
s = Stack()
s.push(10)
s.push(20)
s.peek()
s.pop()
s.view()
2. Python Program with MySQL: Write a Python program to integrate SQL with
Python by importing the MySQL module to search a student using roll no. and delete
the record.
Answer:
python
Copy code
import mysql.connector
def search_and_delete_student(roll_no):
try:
conn = mysql.connector.connect(host='localhost',
user='your_username', password='your_password',
database='your_database')
cursor = conn.cursor()
if student:
print("Student found:", student)
# Delete the student record
delete_query = "DELETE FROM students WHERE roll_no
= %s"
cursor.execute(delete_query, (roll_no,))
conn.commit()
print("Student record deleted.")
else:
print("Student not found.")
# Example usage
search_and_delete_student(101)
SET C
SET-C (Python and SQL)
1. Python Program: Write a Python program to read characters one by one and store
lowercase letters in the file "lower", uppercase letters in the file "upper", and other
characters in the file "other".
Answer:
python
Copy code
with open('input.txt', 'r') as infile, \
open('lower.txt', 'w') as lowerfile, \
open('upper.txt', 'w') as upperfile, \
open('other.txt', 'w') as otherfile:
for char in infile.read():
if char.islower():
lowerfile.write(char)
elif char.isupper():
upperfile.write(char)
else:
otherfile.write(char)
2. SQL Commands: Based on the provided table structures, execute the following SQL
queries:
o Display details of all items in ascending order of item names:
sql
Copy code
SELECT * FROM ITEM ORDER BY Item_name ASC;
o Display item name and price of all items whose price is in the range of
10000 and 20000:
sql
Copy code
SELECT Item_name, Price FROM ITEM WHERE Price
BETWEEN 10000 AND 20000;
sql
Copy code
SELECT Trade_Code, COUNT(*) AS Number_of_Items FROM
ITEM GROUP BY Trade_Code;
o Display price, item name, and quantity of items with more than 150
quantity:
sql
Copy code
SELECT Price, Item_name, Quantity FROM ITEM WHERE
Quantity > 150;
sql
Copy code
SELECT TNAME FROM TRADES WHERE CITY IN ('Delhi',
'Mumbai');