Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Computer Science

The document outlines a Computer Science project for the academic year 2024-2025, detailing various Python programming tasks such as reading and processing text files, creating and manipulating binary files, and implementing basic data structures like stacks. It also includes SQL tasks related to database management, such as creating tables, modifying attributes, and executing queries. Each task is accompanied by example code snippets demonstrating the required functionality.

Uploaded by

Sriansh Shukla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Computer Science

The document outlines a Computer Science project for the academic year 2024-2025, detailing various Python programming tasks such as reading and processing text files, creating and manipulating binary files, and implementing basic data structures like stacks. It also includes SQL tasks related to database management, such as creating tables, modifying attributes, and executing queries. Each task is accompanied by example code snippets demonstrating the required functionality.

Uploaded by

Sriansh Shukla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Academic Year 2024 – 2025

Computer Science Project


Work

Submitted By Submitted To
Sriansh Shukla Tr. ___________________
XII A1
Python Programming
1. Read a text file line by line and display each word separated by a
`#`.

PYTHON
def display_words_with_hash(filename):
with open(filename, 'r') as file:
for line in file:
words = line.split()
print('#'.join(words))

display_words_with_hash('sample.txt')
2. Read a text file and display the number of
vowels/consonants/uppercase/lowercase characters.

PYTHON
def count_characters(filename):
vowels = "aeiouAEIOU"
vowel_count = consonant_count = uppercase_count =
lowercase_count = 0

with open(filename, 'r') as file:


for line in file:
for char in line:
if char.isalpha():
if char in vowels:
vowel_count += 1
else:
consonant_count += 1
if char.isupper():
uppercase_count += 1
elif char.islower():
lowercase_count += 1

print(f"Vowels: {vowel_count}, Consonants:


{consonant_count}, Uppercase: {uppercase_count}, Lowercase:
{lowercase_count}")

count_characters('sample.txt')
3. Remove all the lines that contain the character 'a' in a file and
write it to another file.

PYTHON
def remove_lines_with_a(input_file, output_file):
with open(input_file, 'r') as infile, open(output_file,
'w') as outfile:
for line in infile:
if 'a' not in line:
outfile.write(line)

remove_lines_with_a('input.txt', 'output.txt')
4. Create a binary file with name and roll number. Search for a given
roll number and display the name.

PYTHON
import pickle

def write_binary_file(filename, student_data):


with open(filename, 'wb') as file:
pickle.dump(student_data, file)

def search_by_roll_number(filename, roll_number):


with open(filename, 'rb') as file:
students = pickle.load(file)
for student in students:
if student['roll_number'] == roll_number:
print(f"Name: {student['name']}")
return
print("Roll number not found.")

students = [{'name': 'Alice', 'roll_number': 101}, {'name':


'Bob', 'roll_number': 102}]
write_binary_file('students.dat', students)
search_by_roll_number('students.dat', 101)
5. Create a binary file with roll number, name, and marks. Input a
roll number and update the marks.

PYTHON
def update_marks(filename, roll_number, new_marks):
with open(filename, 'rb') as file:
students = pickle.load(file)

for student in students:


if student['roll_number'] == roll_number:
student['marks'] = new_marks
break

with open(filename, 'wb') as file:


pickle.dump(students, file)

update_marks('students.dat', 101, 95)


6. Write a random number generator that generates random numbers
between 1 and 6 (simulates a dice).

PYTHON
import random

def roll_dice():
return random.randint(1, 6)

print(f"Dice roll: {roll_dice()}")


7. Write a Python program to implement a stack using a list.

PYTHON
class Stack:
def __init__(self):
self.stack = []

def push(self, item):


self.stack.append(item)

def pop(self):
return self.stack.pop() if not self.is_empty() else None

def peek(self):
return self.stack[-1] if not self.is_empty() else None

def is_empty(self):
return len(self.stack) == 0

def size(self):
return len(self.stack)

s = Stack()
s.push(1)
s.push(2)
print(s.pop()) # Outputs: 2
8. Create a CSV file by entering user-id and password, read, and
search the password for the given user-id.

PYTHON
import csv

def create_csv(filename):
with open(filename, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['User ID', 'Password'])
writer.writerow(['user1', 'pass1'])
writer.writerow(['user2', 'pass2'])

def search_password(filename, user_id):


with open(filename, mode='r') as file:
reader = csv.reader(file)
next(reader) # Skip header
for row in reader:
if row[0] == user_id:
print(f"Password for {user_id}: {row[1]}")
return
print("User ID not found.")

create_csv('users.csv')
search_password('users.csv', 'user1')
Database Management Tasks

1. Create a student table and insert data.

SQL
CREATE TABLE student (
roll_no INT PRIMARY KEY,
name VARCHAR(100),
marks INT
);

INSERT INTO student (roll_no, name, marks) VALUES (101, 'Alice',


85);
INSERT INTO student (roll_no, name, marks) VALUES (102, 'Bob',
90);

2. ALTER table to add new attributes / modify data type / drop an


attribute.
SQL

ALTER TABLE student ADD COLUMN age INT;


ALTER TABLE student MODIFY COLUMN marks FLOAT;
ALTER TABLE student DROP COLUMN age;

3. UPDATE table to modify data.


SQL

UPDATE student SET marks = 95 WHERE roll_no = 101;

4. ORDER BY to display data in ascending/descending order.

SQL
SELECT * FROM student ORDER BY marks ASC;
SELECT * FROM student ORDER BY marks DESC;
5. DELETE to remove tuple(s).

SQL
DELETE FROM student WHERE roll no = 102;

6. GROUP BY and find the min, max, sum, count, and average.

SQL
SELECT COUNT(*) FROM student;
SELECT AVG(marks) FROM student;
SELECT MAX(marks) FROM student;
SELECT MIN(marks) FROM student;
SELECT SUM(marks) FROM student;

7. Integrate SQL with Python by importing a suitable


module.
PYTHON
import sqlite3

conn = sqlite3.connect('students.db')
cursor = conn.cursor()

cursor.execute('''
CREATE TABLE IF NOT EXISTS student (
roll_no INTEGER PRIMARY KEY,
name TEXT NOT NULL,
marks INTEGER NOT NULL
)
''')

cursor.execute('INSERT INTO student (roll_no, name, marks)


VALUES (?, ?, ?)', (101, 'Alice', 85))
conn.commit()

conn.close()

You might also like