1.
Read a text file line by line and display each word separated by #
with open("[Link]") as f:
for line in f:
words = [Link]().split()
print("#".join(words))
OUTPUT:
2. Read a text file and display vowels, consonants, uppercase, lowercase
count
vowels = consonants = upper = lower = 0
with open("[Link]") as f:
for line in f:
for char in line:
if [Link]():
if [Link]() in "aeiou":
vowels += 1
else:
consonants += 1
if [Link]():
upper += 1
else:
lower += 1
print("Vowels:", vowels, "Consonants:", consonants)
print("Uppercase:", upper, "Lowercase:", lower)
OUTPUT:
3. Remove lines containing 'a' and write to another file
input_file = "[Link]"
output_file = "[Link]"
with open(input_file, 'r') as infile, open(output_file, 'w') as outfile:
for line in infile:
if 'a' not in line:
[Link](line)
print(f"Lines without 'a' have been written to '{output_file}'.")
OUTPUT:
4. Binary file: Store roll and name, search by roll number
import pickle
data = [(101, "Amit"), (102, "Neha")] with open("[Link]", "wb") as f:
[Link](data, f)
roll = int(input("Enter roll to search: "))
found = False
with open("[Link]", "rb") as f:
records = [Link](f)
for r in records:
if r[0] == roll:
print("Name:", r[1])
found = True
if not found:
print("Roll not found")
OUTPUT:
5. Simulate a dice (1 to 6)
import random
print("Dice rolled:", [Link](1, 6))
OUTPUT:
6. Implement a stack using list
stack = []
def push():
val = input("Enter value: ")
[Link](val)
def pop():
if stack:
print("Popped:", [Link]())
else:
print("Empty stack")
push(); push(); pop(); print(stack)
OUTPUT:
7. Create CSV file with user ID and password, search password by user
ID
import csv
with open("[Link]", "w", newline="") as f:
w = [Link](f)
[Link](["user1", "pass1"])
[Link](["user2", "pass2"])
uid = input("Enter user ID: ")
with open("[Link]") as f:
r = [Link](f)
for row in r:
if row[0] == uid:
print("Password:", row[1])
OUTPUT:
8. Input numbers in tuple and count even, odd
input_str = input("Enter numbers separated by spaces: ")
num_list = [ ]
for x in input_str.split():
num_list.append(int(x))
num_tuple = tuple(num_list)
even = 0
odd = 0
for n in num_tuple:
if n % 2 == 0:
even += 1
else:
odd += 1
print("Tuple:", num_tuple)
print("Even:", even)
print("Odd:", odd)
OUTPUT:
9. Count vowels in a string using function
def count_vowels(s):
return sum(1 for c in [Link]() if c in "aeiou")
print("Vowels:", count_vowels("Hello World"))
OUTPUT:
10. Implement math functions
import math
print("Square root of 25:", [Link](25))
print("Cos(0):", [Link](0))
OUTPUT:
11. Implement string functions
s = "hello world"
print([Link](), [Link](), [Link]("world", "Python"))
OUTPUT:
12. Define module, import in another
# file1: [Link]
def greet(name):
return f”Hello, {name}!”
def add(a,b):
return a+b
# file2: [Link]
import mymodule
print([Link]("Alice"))
print(“Sum:”[Link](10,5))
OUTPUT:
13. Check if string is palindrome using loop
s = input("Enter string: ")
rev = ""
for c in s:
rev = c + rev
print("Palindrome" if s == rev else "Not Palindrome")
OUTPUT:
14. Search element in list, display count & location
def search_element(lst, ele):
count = [Link](ele) locs = [i for i, x in enumerate(lst) if x == ele]
return count, locs
lst = [2, 4, 2, 5, 2]; ele = 2
c, l = search_element(lst, ele)
print("Count:", c, "Locations:", l)
OUTPUT:
15. Update dictionary key value via function
def update_dict(d, key, val):
if key in d:
d[key] = val
d = {"a": 1, "b": 2}
update_dict(d, "a", 10)
print(d)
OUTPUT:
15. Update dictionary key value via function
def update_dict(d, key, val):
if key in d:
d[key] = val
d = {"a": 1, "b": 2}
update_dict(d, "a", 10)
print(d)
OUTPUT:
16. MySQL Program: Create DB and table, insert & show
import [Link]
con = [Link](host="localhost", user="root", passwd="yourpass-
word")
cur = [Link]()
[Link]("CREATE DATABASE IF NOT EXISTS school")
[Link]("USE school")
[Link]("CREATE TABLE IF NOT EXISTS student (roll INT, name VARCHAR(30))")
[Link]("INSERT INTO student VALUES (101, 'Amit'), (102, 'Neha')")
[Link]()
[Link]("SELECT * FROM student")
for row in cur: print(row)
[Link]()
OUTPUT: