1. Read a text file line by line and display each word separated by #.
------------------------------------------------------------------
def task1(filename):
with open(filename, 'r') as file:
for line in file:
words = [Link]().split()
print('#'.join(words))
# Example:
task1('[Link]')
Output:
Hello#world
Python#programming#is#fun
2. Count vowels, consonants, uppercase, and lowercase characters.
------------------------------------------------------------------
def task2(filename):
vowels = "aeiouAEIOU"
with open(filename, 'r') as file:
data = [Link]()
vowel_count = sum(1 for char in data if char in vowels)
consonant_count = sum(1 for char in data if [Link]() and char not in
vowels)
uppercase_count = sum(1 for char in data if [Link]())
lowercase_count = sum(1 for char in data if [Link]())
print(f"Vowels: {vowel_count}, Consonants: {consonant_count}, Uppercase:
{uppercase_count}, Lowercase: {lowercase_count})
# Example:
task2('[Link]')
Output:
Vowels: 7, Consonants: 15, Uppercase: 4, Lowercase: 18
3. Remove all lines containing the character 'a' and write them to another file.
------------------------------------------------------------------
def task3(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:
[Link](line)
# Example:
task3('[Link]', '[Link]')
Output ([Link]):
Python is fun
Life is great
4. Create a binary file with name and roll number. Search for a roll number.
------------------------------------------------------------------
import pickle
def task4_create(file_name, data):
with open(file_name, 'wb') as file:
[Link](data, file)
def task4_search(file_name, roll_number):
with open(file_name, 'rb') as file:
data = [Link](file)
result = next((item['name'] for item in data if item['roll'] ==
roll_number), None)
print(f"Name: {result}" if result else "Roll number not found")
# Example:
data = [{'roll': 1, 'name': 'John'}, {'roll': 2, 'name': 'Jane'}]
task4_create('[Link]', data)
task4_search('[Link]', 1)
Output:
Name: John
5. Update marks for a roll number in a binary file.
------------------------------------------------------------------
def task5_update(file_name, roll_number, new_marks):
with open(file_name, 'rb') as file:
data = [Link](file)
for item in data:
if item['roll'] == roll_number:
item['marks'] = new_marks
with open(file_name, 'wb') as file:
[Link](data, file)
print("Marks updated")
# Example:
task5_update('[Link]', 1, 95)
Output:
Marks updated
6. Simulate a dice (random number generator 1 to 6).
------------------------------------------------------------------
import random
def task6():
print([Link](1, 6))
# Example:
task6()
Output:
4
7. Implement a stack using a list.
------------------------------------------------------------------
def task7():
stack = []
def push(item):
[Link](item)
def pop():
if stack:
return [Link]()
else:
print("Stack is empty")
def display():
print(stack)
# Example operations
push(10)
push(20)
display()
pop()
display()
task7()
Output:
[10, 20]
[10]
8. Create a CSV file and search for a password by user ID.
------------------------------------------------------------------
import csv
def task8():
# Create CSV file
with open('[Link]', 'w', newline='') as file:
writer = [Link](file)
[Link](['user_id', 'password'])
[Link](['user1', 'pass123'])
[Link](['user2', 'pass456'])
# Search password
user_id_to_search = 'user1'
with open('[Link]', 'r') as file:
reader = [Link](file)
for row in reader:
if row['user_id'] == user_id_to_search:
print(f"Password for {user_id_to_search}: {row['password']}")
# Example:
task8()
Output:
Password for user1: pass123
9. Database management: Create a table, insert data, and perform SQL operations.
------------------------------------------------------------------
import sqlite3
def database_management():
conn = [Link]('[Link]')
cursor = [Link]()
# Create table
[Link]('''CREATE TABLE IF NOT EXISTS student (
roll INTEGER PRIMARY KEY,
name TEXT,
marks REAL)''')
# Insert data
[Link]("INSERT INTO student (roll, name, marks) VALUES (1, 'John',
85.5)")
[Link]("INSERT INTO student (roll, name, marks) VALUES (2, 'Jane',
90.0)")
[Link]()
# Update marks
[Link]("UPDATE student SET marks = 95.0 WHERE roll = 1")
# Order by marks
[Link]("SELECT * FROM student ORDER BY marks DESC")
print([Link]())
# Group by and aggregate functions
[Link]("SELECT AVG(marks) FROM student")
print(f"Average marks: {[Link]()[0]}")
[Link]()
# Example:
database_management()
Output:
[(1, 'John', 95.0), (2, 'Jane', 90.0)]
Average marks: 92.5