0% found this document useful (0 votes)
25 views5 pages

Set A B C Answer.

Uploaded by

demon121869
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)
25 views5 pages

Set A B C Answer.

Uploaded by

demon121869
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
You are on page 1/ 5

SET-A

SET-A (Python and SQL)

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;

 UPDATE TABLE to modify data:

sql
Copy code
UPDATE student SET name = 'John Doe' WHERE roll_no = 1;

 ORDER BY to display data in ascending/descending order:

sql
Copy code
SELECT * FROM student ORDER BY name ASC; -- For ascending
order
SELECT * FROM student ORDER BY name DESC; -- For descending
order

 Find the min, max, sum, count, and average:


sql
Copy code
SELECT MIN(marks) AS MinMarks, MAX(marks) AS MaxMarks,
SUM(marks) AS TotalMarks, COUNT(*) AS StudentCount, AVG(marks)
AS AverageMarks FROM student;

SET B

SET-B (Python and SQL)

1. Python Program: Write a Python program to implement stack operations (Perform


Push, Pull, View, and Peek).

Answer:

python
Copy code
class Stack:
def __init__(self):
self.stack = []

def push(self, item):


self.stack.append(item)
print(f"Pushed: {item}")

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

# Search for the student


search_query = "SELECT * FROM students WHERE roll_no =
%s"
cursor.execute(search_query, (roll_no,))
student = cursor.fetchone()

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.")

except mysql.connector.Error as err:


print(f"Error: {err}")
finally:
if conn.is_connected():
cursor.close()
conn.close()

# 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;

o Display the number of items traded by each trader:

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;

o Display the name of traders from Delhi or Mumbai:

sql
Copy code
SELECT TNAME FROM TRADES WHERE CITY IN ('Delhi',
'Mumbai');

You might also like