Computer Science Practical
INTRODUCTION
NAME: Smriti Kumari Jha
CLASS: 12th B
ROLL.NO: 04
STUDENT.ID: 20180032187
SESSION: 2024-25
SUB.TEACHER NAME: Mrs. Madhulika Pandey
1. Read a text file line by line and display each word separated by a #
# input.txt contains:
# Hello world
# This is a test
# Python Code:
with open("input.txt", "r") as file:
for line in file:
words = line.split()
print("#".join(words))
2. Read a text file and display the number of vowels/consonants/uppercase/lowercase
characters in the file
# Python Code:
vowels = "aeiouAEIOU"
consonants = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"
def count_characters(file_path):
vowels_count = consonants_count = upper_case = lower_case = 0
with open(file_path, 'r') as file:
for line in file:
for char in line:
if char in vowels:
vowels_count += 1
elif char in consonants:
consonants_count += 1
if char.isupper():
upper_case += 1
elif char.islower():
lower_case += 1
return vowels_count, consonants_count, upper_case, lower_case
file_path = 'input.txt'
vowels_count, consonants_count, upper_case, lower_case = count_characters(file_path)
print(f"Vowels: {vowels_count}, Consonants: {consonants_count}, Uppercase: {upper_case}, Lowercase: {lower_case}")
3. Remove all the lines that contain the character 'a' in a file and write it to another file
# input.txt contains:
# Hello world
# This is a test
# Another line
# Python Code:
with open('input.txt', 'r') as file:
lines = file.readlines()
with open('output.txt', 'w') as new_file:
for line in lines:
if 'a' not in line:
new_file.write(line)
4. Create a binary file with name and roll number. Search for a given roll number and display
the name if not found display appropriate message
# Python Code:
import pickle
def create_file(file_name):
with open(file_name, 'wb') as file:
data = {'04': 'Smriti Kumari Jha', '05': 'Vinay Kumar'}
pickle.dump(data, file)
def search_roll(file_name, roll_number):
with open(file_name, 'rb') as file:
data = pickle.load(file)
if roll_number in data:
print(f"Name: {data[roll_number]}")
else:
print("Roll number not found.")
file_name = 'student.dat'
create_file(file_name)
search_roll(file_name, '04') # example roll number
5. Create a binary file with roll number name and marks. Input a roll number and update the
marks
# Python Code:
import pickle
def create_marks_file(file_name):
with open(file_name, 'wb') as file:
data = {'04': {'name': 'Smriti Kumari Jha', 'marks': 95}, '05': {'name': 'Vinay Kumar', 'marks': 88}}
pickle.dump(data, file)
def update_marks(file_name, roll_number, new_marks):
with open(file_name, 'rb') as file:
data = pickle.load(file)
if roll_number in data:
data[roll_number]['marks'] = new_marks
print(f"Updated marks for {data[roll_number]['name']} to {new_marks}.")
else:
print("Roll number not found.")
with open(file_name, 'wb') as file:
pickle.dump(data, file)
file_name = 'marks.dat'
create_marks_file(file_name)
update_marks(file_name, '04', 98) # example to update marks
6. Write a random number generator that generates random numbers between 1 and 6
(simulates a dice)
# Python Code:
import random
def roll_dice():
return random.randint(1, 6)
print(f"Dice rolled: {roll_dice()}")
7. Write a Python program to implement a stack using a list
# Python Code:
class Stack:
def __init__(self):
self.stack = []
def push(self, item):
self.stack.append(item)
def pop(self):
if not self.is_empty():
return self.stack.pop()
else:
return "Stack is empty"
def is_empty(self):
return len(self.stack) == 0
def display(self):
print(self.stack)
stack = Stack()
stack.push(10)
stack.push(20)
stack.push(30)
stack.display()
stack.pop()
stack.display()
8. Create a CSV file by entering user-id and password read and search the password for a
given user-id
# Python Code:
import csv
def create_csv(file_name):
with open(file_name, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["user_id", "password"])
writer.writerow(["smriti04", "mypassword123"])
writer.writerow(["vinay05", "secretpass"])
def search_password(file_name, user_id):
with open(file_name, mode='r') as file:
reader = csv.reader(file)
for row in reader:
if row[0] == user_id:
print(f"Password for {user_id} is {row[1]}")
return
print("User ID not found")
file_name = 'users.csv'
create_csv(file_name)
search_password(file_name, 'smriti04')