Shuvcs (Updated) New
Shuvcs (Updated) New
1. Read a text file line by line and display each word separated
by a #.
Source Code: -
Output: -
Hello#python#I’m#Shuvhamkar#and#I’m#an#CS#student
5321
shuvhamkar
Source Code: -
print(f"Vowels: {vowels}")
print(f"Consonants: {consonants}")
print(f"Uppercase characters: {uppercase}")
print(f"Lowercase characters: {lowercase}")
5321
shuvhamkar
Output:-
Vowels: 11
Consonants: 23
Uppercase Characters: 3
Lowercase Characters: 31
5321
shuvhamkar
3. Remove all the lines that contain the character ‘a’ in a file
and write it to another file.
Source Code: -
Output: -
5321
shuvhamkar
4. Create a Binary file with name and roll number Search for a
given roll number and display the name, if not found display
appropriate message.
Source Code: -
import pickle
students = [
('Shuvhamkar', 28),
('Sparsh', 29),
('Suhani', 30),
('Shreya', 27)
]
pickle.dump(students, file)
print("Binary file created successfully.")
if roll_number == search_roll:
print(f"Roll number {roll_number} found. Name:
{name}")
break
5321
shuvhamkar
else:
print(f"Roll number {search_roll} not found.")
Output:-
5321
shuvhamkar
Source Code: -
import pickle
students = [
(28, 'Shuvhamkar', 89),
(29, 'Abhishek', 90),
(30, 'Shourya', 93),
(27, 'Shreya', 92)
]
pickle.dump(students, file)
print("Binary file created successfully.")
try:
search_roll = int(input("Enter the roll number to update
marks: "))
new_marks = int(input("Enter the new marks: "))
except ValueError:
print("Please enter valid integer values for roll number
and marks.")
exit()
try:
with open('students.bin', 'rb') as file:
students = pickle.load(file)
5321
shuvhamkar
updated_students = []
updated = False
for roll_number, name, marks in students:
if roll_number == search_roll:
updated_students.append((roll_number, name,
new_marks))
print(f"Marks updated for {name}. New marks:
{new_marks}")
updated = True
else:
updated_students.append((roll_number, name, marks))
if not updated:
print(f"Roll number {search_roll} not found.")
Output: -
5321
shuvhamkar
Source Code: -
import random
while True:
input("Press Enter to roll the dice...")
dice_roll = random.randint(1, 6)
print(f"You rolled a {dice_roll}!")
Output: -
5321
shuvhamkar
Source Code:-
stack = []
stack.append(10)
print(f"Pushed 10 onto the stack.")
stack.append(20)
print(f"Pushed 20 onto the stack.")
stack.append(30)
print(f"Pushed 30 onto the stack.")
if stack:
top_element = stack[-1]
print("Top element:", top_element)
else:
print("Stack is empty! No top element.")
if stack:
popped_element = stack.pop()
print(f"Popped element: {popped_element}")
else:
print("Stack is empty! Cannot pop.")
if stack:
popped_element = stack.pop()
print(f"Popped element: {popped_element}")
5321
shuvhamkar
if stack:
popped_element = stack.pop()
print(f"Popped element: {popped_element}")
else:
print("Stack is empty! Cannot pop.")
if stack:
popped_element = stack.pop()
print(f"Popped element: {popped_element}")
else:
print("Stack is empty! Cannot pop.")
Output:-
5321
shuvhamkar
5321
shuvhamkar
Source Code: -
import csv
filename = 'user_data.csv'
while True:
user_id = input("Enter user ID (or type 'exit' to finish): ")
if user_id.lower() == 'exit':
break
password = input("Enter password: ")
with open(filename, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([user_id, password])
found = False
with open(filename, mode='r') as file:
reader = csv.reader(file)
for row in reader:
if row[0] == search_id:
print(f"Password for user ID '{search_id}': {row[1]}")
found = True
break
5321
shuvhamkar
if not found:
print(f"No password found for user ID '{search_id}'.")
Output: -
5321
shuvhamkar
Source Code:-
Import pickle
print(my_list)
Output: -
[10,20,30,40,50]
5321
shuvhamkar
10. How would you write a python function to create a binary file.
Source code: -
def create_binary_file(filename):
with open(filename,’wb’) as file:
print(“Binary file ‘{filename}’ created successfully.”)
create_binary_file(“student.bin”)
Output: -
5321
shuvhamkar
Source Code:-
5321
shuvhamkar
Source Code:-
5321
shuvhamkar
Source Code:-
5321
shuvhamkar
Source Code: -
5321
shuvhamkar
Source Code:-
5321
shuvhamkar
Python Code:
from collections import Counter
def word_count(fname):
with open(fname) as f:
return Counter(f.read().split())
5321
shuvhamkar
Python Code:
def file_lengthy(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
OUTPUT:
5321
shuvhamkar
Python Code:
import glob
char_list = []
files_list = glob.glob("*.txt")
char_list.append(f.read())
print(char_list)
5321
shuvhamkar
Python Code:
def remove_newlines(fname):
flist = open(fname).readlines()
print(remove_newlines("test.txt"))
5321
shuvhamkar
20. Write a Python program to create a file where all letters of English
alphabet are listed by specified number of letters on each line.
Python Code:
import string
def letters_file_line(n):
alphabet = string.ascii_uppercase
f.writelines(letters)
letters_file_line(3)
Output:
words1.txt
ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
YZ
5321
shuvhamkar
SOURCE CODE:
st=[]
def push(st):
sn=input("Enter name of student")
st.append(sn)
def pop(st):
if(st== []):
print("Stack is empty")
else:
print("Deleted student name:",st.pop())
5321
shuvhamkar
SOURCE CODE:
st=[]
def push(st):
sn=input("Enter any Number")
st.append(sn)
def pop(st):
if(st= = []):
print("Stack is empty")
else:
print("Deleted Number is:",st.pop())
5321
shuvhamkar
23. Write a menu based program to add, delete and display the
record of hostel using list as stack data structure in python.
Record of hostel contains the fields: Hostel number, Total
Students and Total Rooms
SOURCE CODE:
hostel_records = []
def add_record():
hostel_number = input("Enter the hostel number: ")
total_students = int(input("Enter the total number of students: "))
total_rooms = int(input("Enter the total number of rooms: "))
hostel_records.append([hostel_number, total_students, total_rooms])
print("Record added successfully.")
def delete_record():
hostel_number = input("Enter the hostel number to delete the record:
")
for record in hostel_records:
if record[0] == hostel_number:
hostel_records.remove(record)
print("Record deleted successfully.")
return
print("Record not found.")
def display_records():
if not hostel_records:
print("No records to display.")
5321
shuvhamkar
return
print("Hostel Number\tTotal Students\tTotal Rooms")
for record in hostel_records:
print(record[0], "\t\t", record[1], "\t\t", record[2])
while True:
print("1. Add Record")
print("2. Delete Record")
print("3. Display Records")
print("4. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
add_record()
elif choice == 2:
delete_record()
elif choice == 3:
display_records()
elif choice == 4:
break
else:
print("Invalid choice. Try again.")
OUTPUT:
5321
shuvhamkar
24. Write a program to show the detail of the student who scored
the highest marks. Data stored in "Data.csv" is given below:
Rollno,Name,Marks
1,Shreya,35
2,Shuvhamkar,36
3,Sparsh,32
4,Suhani,35
SOURCE CODE:
import csv f=open("data.csv","r")
d=csv.reader(f)
next(f)
max=0
for i in d:
if int(i[2])>max:
max=int([2])
f.close() f=open("data.csv","r") d=csv.reader(f)
next(f)
5321
shuvhamkar
for i in d:
if int(i[2])==max
print(i)
f.close()
OUTPUT:
['2', 'Shuvhamkar', '37']
5321
shuvhamkar
5321
shuvhamkar
f.close()
OUTPUT:
5321
shuvhamkar
5321
shuvhamkar
5321
shuvhamkar
5321
shuvhamkar
5321
shuvhamkar
5321
shuvhamkar
5321