Computer Science Practical Output
Computer Science Practical Output
INTRODUCTION
CLASS: 12th B
ROLL.NO: 04
STUDENT.ID: 20180032187
SESSION: 2024-25
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:
words = line.split()
print("#".join(words))
# Python Code:
vowels = "aeiouAEIOU"
consonants = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"
def count_characters(file_path):
if char in vowels:
vowels_count += 1
consonants_count += 1
if char.isupper():
upper_case += 1
elif char.islower():
lower_case += 1
file_path = 'input.txt'
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:
lines = file.readlines()
new_file.write(line)
4. Create a binary file with name and roll number. Search for a given roll number and display
# Python Code:
import pickle
def create_file(file_name):
pickle.dump(data, file)
data = pickle.load(file)
if roll_number in data:
print(f"Name: {data[roll_number]}")
else:
create_file(file_name)
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):
data = {'04': {'name': 'Smriti Kumari Jha', 'marks': 95}, '05': {'name': 'Vinay Kumar', 'marks': 88}}
pickle.dump(data, file)
data = pickle.load(file)
if roll_number in data:
data[roll_number]['marks'] = new_marks
else:
file_name = 'marks.dat'
create_marks_file(file_name)
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)
# Python Code:
class Stack:
def __init__(self):
self.stack = []
self.stack.append(item)
def pop(self):
if not self.is_empty():
return self.stack.pop()
else:
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"])
reader = csv.reader(file)
if row[0] == user_id:
return
file_name = 'users.csv'
create_csv(file_name)
search_password(file_name, 'smriti04')