output (1)
output (1)
1. WAP to count the number of unique characters in a user-entered string and also
display the list of unique characters.
unique_chars = set(string)
3. WAP to create a key-value pair of an employee and his salary and find out which
employee is being offered the highest salary.
employees = {}
for i in range(n):
employees[name] = salary
factorial = 1
factorial *= i
if num == num[::-1]:
else:
row = ""
row += str(j)
print(row)
while b:
a, b = b, a % b
return a
while True:
lcm = greater
break
greater += 1
print("LCM:", lcm)
9. WAP to create a list of numbers and also sum up all the numbers in the list.
total = 0
total += int(num)
import pickle
def addRecord():
try:
with open("book.dat",'ab') as f:
while True:
d=int(input("enter price"))
L=[a,b,c,d]
pickle.dump(L,f)
ch=input("enter y or n")
if ch=="n":
break
except Exception as e:
addRecord()
11. WAP to read the contents from a binary file and print details of the book.
import pickle
def readRecord():
try:
with open("book.dat",'rb') as f:
while True:
s=pickle.load(f)
print(s)
except Exception as e:
readRecord()
12. WAP to copy the books from books.dat to temp.dat if their price is in between 500 and
1200.
import pickle
def copyRecord():
try:
while True:
try:
record = pickle.load(f1)
pickle.dump(record, f2)
print(record)
except EOFError:
break
except Exception as e:
except Exception as e:
copyRecord()
import pickle
def updateRecord():
try:
while True:
try:
s = pickle.load(f1)
if s[0] == book_name:
s[1] = new_author
s[2] = new_year
s[3] = new_price
print(f"Updated record: {s}")
pickle.dump(s, f2)
except EOFError:
break
except Exception as e:
except FileNotFoundError:
except Exception as e:
updateRecord()
CSV Files:
14. WAP to take the input in the format [book no., name, author & publication] and add it in
the database in terms of CSV file.
import csv
writer = csv.writer(file)
writer.writerow(fields)
for _ in range(n):
15. WAP to read a CSV file and enlist the number of books in different categories.
categories = {}
first_line = True
for line in f:
row = line.strip().split(",")
category = row[2]
if category in categories:
categories[category] += 1
else:
categories[category] = 1
import csv
reader = csv.reader(f)
header = next(reader)
writer = csv.writer(f1)
writer.writerow(header)
writer.writerows(books)
print("done")
17. WAP to take the inputs about a book in a format ([book no., name, author, publication]),
put it in a CSV file, and take another input to search a specific book from this CSV file.
import csv
writer = csv.writer(file)
writer.writerow(fields)
for i in range(n):
reader = csv.reader(file)
next(reader)
if found:
else:
Text Files:
18. WAP to take input from the user and write it in a text file.
file.write(text)
print("Done")
19. WAP to read a line and store it in a text file.
line = f.readline()
20. WAP to input multiple lines from the user and store all of them in a file.
lines = []
while True:
line = input()
if line == ".":
break
lines.append(line)
f.write(line)
print("Done")
21. WAP to read from a text file & display it on the screen.filename = input("Enter file name
to read from: ")
content = f.read()
22. WAP to read a text file and count the number of vowels in the text file.
text = f.read()
c=0
if char.lower() in "aeiou":
c += 1
print("Number of vowels", c)
23. WAP to read a text file and count the number of uppercase characters in the text file &
display this uppercase count.
text = f.read()
c=0
if char.isupper():
c += 1