0% found this document useful (0 votes)
5 views39 pages

output (1)

Uploaded by

tanishmitra26
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)
5 views39 pages

output (1)

Uploaded by

tanishmitra26
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/ 39

REVISION TOUR

1. WAP to count the number of unique characters in a user-entered string and also
display the list of unique characters.

string = input("Enter a string: ")

unique_chars = set(string)

print("Number of unique characters:", len(unique_chars))

print("Unique characters:", unique_chars)

2. WAP to count the number of different types of characters used in a string.

string = input("Enter a string: ")

alphabets = sum(c.isalpha() for c in string)

digits = sum(c.isdigit() for c in string)

spaces = sum(c.isspace() for c in string)

specials = len(string) - (alphabets + digits + spaces)

print("Alphabets:", alphabets, "Digits:", digits, "Spaces:", spaces, "Special


Characters:", specials)

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.

n = int(input("Enter number of employees: "))

employees = {}
for i in range(n):

name = input("Enter employee name: ")

salary = int(input("Enter salary: "))

employees[name] = salary

high = max(employees, key=employees.get)

print("Employee with highest salary:", high, "Salary:", employees[high])

4. WAP to find the factorial of a number.

num = int(input("Enter a non-negative integer: "))

factorial = 1

for i in range(1, num + 1):

factorial *= i

print(f"The factorial of {num} is {factorial}.")

5. WAP to find if a number is a Palindrome or not.

num = input("Enter a number: ")

if num == num[::-1]:

print("The number is a palindrome.")

else:

print("The number is not a palindrome.")


6. WAP to create a triangle pattern with numbers.

rows = int(input("Enter number of rows: "))

for i in range(1, rows + 1):

row = ""

for j in range(1, i + 1):

row += str(j)

print(row)

7. WAP to write the HCF of two numbers.

def hcf(a, b):

while b:

a, b = b, a % b

return a

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

print("HCF:", hcf(num1, num2))


8. WAP to calculate the LCM of two numbers.

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

greater = max(num1, num2)

while True:

if greater % num1 == 0 and greater % num2 == 0:

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.

numbers = input("Enter numbers").split()

total = 0

for num in numbers:

total += int(num)

print("Sum of numbers:", total)


Binary Files:
10. WAP to add a book record to a binary file.

import pickle

def addRecord():

try:

with open("book.dat",'ab') as f:

while True:

a=input("enter book name")

b=input("enter author name")

c=int(input("enter year of publish"))

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:

print(f"an error occured:{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:

print(f"an error occured:{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:

with open("book.dat", 'rb') as f1, open("temp.dat", 'wb') as f2:

while True:

try:

record = pickle.load(f1)

if 500 <= record[3] <= 1500:

pickle.dump(record, f2)

print(record)

except EOFError:

break

except Exception as e:

print(f"An error occurred: {e}")


except FileNotFoundError:

print("The file 'book.dat' was not found.")

except Exception as e:

print(f"An error occurred: {e}")

copyRecord()

13. WAP to update records of the file books.dat.

import pickle

def updateRecord():

book_name = input("Enter book name to update: ")

new_author = input("Enter author: ")

new_year = int(input("Enter year of publication: "))

new_price = float(input("Enter price: "))

try:

with open("book.dat", 'rb') as f1, open("temp.dat", 'wb') as f2:

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:

print(f"An error occurred: {e}")

except FileNotFoundError:

print("The file 'book.dat' was not found.")

except Exception as e:

print(f"An error occurred: {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

fields = ["Book Name", "Author", "Publication"]

n = int(input("Enter number of books: "))

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

writer = csv.writer(file)
writer.writerow(fields)

for _ in range(n):

book = input("Enter book name: ")

author = input("Enter author: ")

pub = input("Enter publication: ")

writer.writerow([book, author, pub])

print("Books saved to CSV.")

15. WAP to read a CSV file and enlist the number of books in different categories.

categories = {}

with open("books.csv", "r") as f:

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

print("Number of books in each category:", categories)


16. WAP to read a CSV file and transfer books of type “fiction” from this file to another CSV
file.

import csv

with open("books.csv", "r") as f:

reader = csv.reader(f)

header = next(reader)

books = [row for row in reader if row[2] == "Fiction"]

with open("fiction_books.csv", "w", newline="") as f1:

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

fields = ["Book Name", "Author", "Publication"]

n = int(input("Enter number of books to add: "))

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

writer = csv.writer(file)

writer.writerow(fields)

for i in range(n):

book = input("Enter book name: ")


author = input("Enter author: ")

pub = input("Enter publication: ")

writer.writerow([book, author, pub])

search = input("Enter book name to search: ")

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

reader = csv.reader(file)

next(reader)

found = next((row for row in reader if row[0] == search), None)

if found:

print("Book found:", found)

else:

print("Book not found.")

Text Files:
18. WAP to take input from the user and write it in a text file.

filename = input("Enter file name")

text = input("Enter text to save: ")

with open(filename, "w") as file:

file.write(text)

print("Done")
19. WAP to read a line and store it in a text file.

f = input("Enter file ")

with open(f, "r") as f:

line = f.readline()

print("Line from file:", line)

20. WAP to input multiple lines from the user and store all of them in a file.

filename = input("Enter file")

lines = []

print("Enter lines of text")

while True:

line = input()

if line == ".":

break

lines.append(line)

with open(filename, "w") as f:

for line in lines:

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: ")

with open(filename, "r") as f:

content = f.read()

print("File content:\n", content)

22. WAP to read a text file and count the number of vowels in the text file.

filename = input("Enter file: ")

with open(filename, "r") as f:

text = f.read()

c=0

for char in text:

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.

filename = input("Enter file: ")

with open(filename, "r") as f:

text = f.read()

c=0

for char in text:

if char.isupper():

c += 1

print("Number of uppercase characters in file:", c)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy