0% found this document useful (0 votes)
13 views6 pages

Board Practical Solutions

Uploaded by

manishydv9845
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Board Practical Solutions

Uploaded by

manishydv9845
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Board Practical Solutions

Python Programming Solutions

1. Read a text file line by line and display each word separated by a #:

with open("sample.txt", "r") as file:

for line in file:

words = line.split()

print("#".join(words))

2. Count vowels, consonants, uppercase, and lowercase characters in a text file:

vowels = "aeiouAEIOU"

with open("sample.txt", "r") as file:

text = file.read()

vowel_count = sum(1 for char in text if char in vowels)

consonant_count = sum(1 for char in text if char.isalpha() and char not in vowels)

uppercase_count = sum(1 for char in text if char.isupper())

lowercase_count = sum(1 for char in text if char.islower())

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

{uppercase_count}, Lowercase: {lowercase_count}")

3. Remove lines containing the character 'a' and write to another file:

with open("input.txt", "r") as infile, open("output.txt", "w") as outfile:

for line in infile:

if "a" not in line:

outfile.write(line)
Board Practical Solutions

4. Binary file with name and roll number:

import pickle

data = {"101": "John", "102": "Alice"}

with open("students.dat", "wb") as file:

pickle.dump(data, file)

roll_number = input("Enter roll number: ")

with open("students.dat", "rb") as file:

data = pickle.load(file)

print(data.get(roll_number, "Roll number not found"))

5. Binary file with roll number, name, and marks:

roll_number = input("Enter roll number to update marks: ")

new_marks = int(input("Enter new marks: "))

with open("students.dat", "rb") as file:

data = pickle.load(file)

data[roll_number] = new_marks

with open("students.dat", "wb") as file:

pickle.dump(data, file)

print("Marks updated successfully.")


Board Practical Solutions

6. Random number generator (dice simulation):

import random

print(f"Random dice roll: {random.randint(1, 6)}")

7. Implement a stack using a list:

stack = []

stack.append(10)

stack.append(20)

stack.pop() # Removes 20

print(stack) # Output: [10]

8. CSV file: User ID and Password:

import csv

with open("users.csv", "w", newline="") as file:

writer = csv.writer(file)

writer.writerow(["User ID", "Password"])

writer.writerow(["user1", "pass1"])

writer.writerow(["user2", "pass2"])

search_user = input("Enter user ID: ")

with open("users.csv", "r") as file:


Board Practical Solutions

reader = csv.reader(file)

for row in reader:

if row[0] == search_user:

print(f"Password: {row[1]}")

SQL Solutions

1. Create a student table and insert data:

CREATE TABLE students (

roll_number INT PRIMARY KEY,

name VARCHAR(50),

marks INT

);

INSERT INTO students VALUES (101, 'John', 85), (102, 'Alice', 90);

2. ALTER table:

ALTER TABLE students ADD age INT;

ALTER TABLE students MODIFY marks FLOAT;

ALTER TABLE students DROP COLUMN age;

3. UPDATE table:

UPDATE students SET marks = 95 WHERE roll_number = 101;

4. ORDER BY clause:
Board Practical Solutions

SELECT * FROM students ORDER BY marks ASC;

SELECT * FROM students ORDER BY marks DESC;

5. DELETE rows:

DELETE FROM students WHERE marks < 80;

6. GROUP BY and aggregate functions:

SELECT name, MAX(marks) AS highest_marks FROM students GROUP BY name;

SELECT AVG(marks) AS average_marks FROM students;

7. Integrate SQL with Python:

import sqlite3

connection = sqlite3.connect("school.db")

cursor = connection.cursor()

cursor.execute("CREATE TABLE IF NOT EXISTS students (roll_number INT, name TEXT, marks

INT)")

cursor.execute("INSERT INTO students VALUES (101, 'John', 85), (102, 'Alice', 90)")

cursor.execute("SELECT * FROM students")

print(cursor.fetchall())

connection.commit()
Board Practical Solutions

connection.close()

You might also like