def add_student(): roll = input("Enter Roll No: ") name = input("Enter Name: ") marks = input("Enter Marks: ") student = {"roll": roll, "name": name, "marks": marks} students.append(student) print("Student added")
def display_students(): if not students: print("No records") return for s in students: print("Roll:", s["roll"], "Name:", s["name"], "Marks:", s["marks"])
def search_student(): roll = input("Enter Roll No to search: ") for s in students: if s["roll"] == roll: print("Roll:", s["roll"], "Name:", s["name"], "Marks:", s["marks"]) return print("Student not found")
def update_student(): roll = input("Enter Roll No to update: ") for s in students: if s["roll"] == roll: s["name"] = input("Enter new name: ") s["marks"] = input("Enter new marks: ") print("Student updated") return print("Student not found")
def delete_student(): roll = input("Enter Roll No to delete: ") for s in students: if s["roll"] == roll: students.remove(s) print("Student deleted") return print("Student not found")
client = MongoClient("mongodb://localhost:27017/") db = client["SPPU"] collection = db["student"]
print("Connected to MongoDB")
def add_student(): roll = input("Enter Roll No: ") name = input("Enter Name: ") marks = input("Enter Marks: ") collection.insert_one({"id": roll, "name": name, "marks": marks}) print("Student added")
def display_students(): data = collection.find({}, {"_id": 0}) found = False for s in data: print("Roll:", s["id"], "Name:", s["name"], "Marks:", s["marks"]) found = True if not found: print("No records found")
def search_student(): roll = input("Enter Roll No to search: ") s = collection.find_one({"id": roll}, {"_id": 0}) if s: print("Roll:", s["id"], "Name:", s["name"], "Marks:", s["marks"]) else: print("Student not found")
def update_student(): roll = input("Enter Roll No to update: ") name = input("Enter new name: ") marks = input("Enter new marks: ") result = collection.update_one( {"id": roll}, {"$set": {"name": name, "marks": marks}} ) if result.matched_count: print("Student updated") else: print("Student not found")
def delete_student(): roll = input("Enter Roll No to delete: ") result = collection.delete_one({"id": roll}) if result.deleted_count: print("Student deleted") else: print("Student not found")
10 Comments
1:
ReplyDeletenumbers = [1,2,3,4,5,6,7,8]
def is_prime(n):
if n <= 1:
return False
for i in range(2,n):
if n % i == 0:
return False
return True
for n in numbers:
if is_prime(n):
print(f"{n} is Prime")
elif n % 2 == 0:
print(f"{n} is Even")
else:
print(f"{n} is Odd")
2:
ReplyDeletesentence = input("Enter the Sentence: ")
words = sentence.lower().split()
word_count = {}
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
file = open("word_count.txt","w")
for word,count in word_count.items():
file.write(word + " : " + str(count) + "\n")
file.close()
print("Done")
3:
ReplyDeletestudents = []
def add_student():
roll = input("Enter Roll No: ")
name = input("Enter Name: ")
marks = input("Enter Marks: ")
student = {"roll": roll, "name": name,
"marks": marks}
students.append(student)
print("Student added")
def display_students():
if not students:
print("No records")
return
for s in students:
print("Roll:", s["roll"], "Name:",
s["name"], "Marks:", s["marks"])
def search_student():
roll = input("Enter Roll No to search: ")
for s in students:
if s["roll"] == roll:
print("Roll:", s["roll"], "Name:",
s["name"], "Marks:", s["marks"])
return
print("Student not found")
def update_student():
roll = input("Enter Roll No to update: ")
for s in students:
if s["roll"] == roll:
s["name"] = input("Enter new name: ")
s["marks"] = input("Enter new marks: ")
print("Student updated")
return
print("Student not found")
def delete_student():
roll = input("Enter Roll No to delete: ")
for s in students:
if s["roll"] == roll:
students.remove(s)
print("Student deleted")
return
print("Student not found")
while True:
print("1.Add 2.Display 3.Search 4.Update " \
"5.Delete 6.Exit")
choice = input("Enter choice: ")
if choice == "1":
add_student()
elif choice == "2":
display_students()
elif choice == "3":
search_student()
elif choice == "4":
update_student()
elif choice == "5":
delete_student()
elif choice == "6":
break
else:
print("Invalid choice")
4:
ReplyDeletedef flatten(lst):
flat = []
for item in lst:
if type(item) == list:
flat = flat + flatten(item)
else:
flat.append(item)
return flat
print(flatten([1, [2, [3, 4], 5]]))
6:
ReplyDeletetry:
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
op = input("Enter + - * / : ")
if op == '+':
print(a + b)
elif op == '-':
print(a - b)
elif op == '*':
print(a * b)
elif op == '/':
print(a / b)
else:
print("Invalid operator")
except ZeroDivisionError:
print("Error: Division by zero is not allowed")
except ValueError:
print("Error: Please enter valid numeric values")
except Exception as e:
print("Unexpected Error:", e)
7:
ReplyDeletefile = open("marks.csv", "r")
lines = file.readlines()
file.close()
total = 0
count = 0
for i in range(1, len(lines)): # skip header
row = lines[i].strip().split(",")
marks = int(row[1])
total = total + marks
count = count + 1
average = total / count
out = open("average.txt", "w")
out.write("Average Marks = " + str(average))
out.close()
print("Average calculated successfully")
8:
ReplyDeleteclass BankAccount():
def _init_(self,balance=0):
self.balance = balance
def deposit(self, amount):
self.balance = self.balance + amount
print("Deposited: ",amount)
def withdraw(self, amount):
if amount <= self.balance:
self.balance = self.balance - amount
print("Withdraw:",amount)
else:
print("Insufficient funds")
def check_balance(self):
print("Remaining Balance:",self.balance)
account = BankAccount(1000)
account.deposit(200)
account.withdraw(300)
account.check_balance()
9:
ReplyDeleteclass Vehicle:
def display_name(self):
print("I am Vehicle")
class car(Vehicle):
def display_name(self):
print("I am Car")
class motorcycle(Vehicle):
def display_name(self):
print("I am bike")
v = Vehicle()
c = car()
m = motorcycle()
v.display_name()
m.display_name()
c.display_name()
11:
ReplyDeleteimport re
s = "Email list: abc@inmiet.in, xyz@onimiet.org," \
"shardul123@gmail.com, jadhav99@yahoo.com"
pattern = r"[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-z]{2,}"
result = re.findall(pattern, s)
print(result)
????
import re
text = "Contact us at test@gmail.com or info@yahoo.com"
emails = re.findall(r'\S+@\S+', text)
print(emails)
12:
ReplyDeletefrom pymongo import MongoClient
client = MongoClient("mongodb://localhost:27017/")
db = client["SPPU"]
collection = db["student"]
print("Connected to MongoDB")
def add_student():
roll = input("Enter Roll No: ")
name = input("Enter Name: ")
marks = input("Enter Marks: ")
collection.insert_one({"id": roll, "name": name, "marks": marks})
print("Student added")
def display_students():
data = collection.find({}, {"_id": 0})
found = False
for s in data:
print("Roll:", s["id"], "Name:", s["name"], "Marks:", s["marks"])
found = True
if not found:
print("No records found")
def search_student():
roll = input("Enter Roll No to search: ")
s = collection.find_one({"id": roll}, {"_id": 0})
if s:
print("Roll:", s["id"], "Name:", s["name"], "Marks:", s["marks"])
else:
print("Student not found")
def update_student():
roll = input("Enter Roll No to update: ")
name = input("Enter new name: ")
marks = input("Enter new marks: ")
result = collection.update_one(
{"id": roll},
{"$set": {"name": name, "marks": marks}}
)
if result.matched_count:
print("Student updated")
else:
print("Student not found")
def delete_student():
roll = input("Enter Roll No to delete: ")
result = collection.delete_one({"id": roll})
if result.deleted_count:
print("Student deleted")
else:
print("Student not found")
while True:
print("1.Add 2.Display 3.Search 4.Update 5.Delete 6.Exit")
choice = input("Enter choice: ")
if choice == "1":
add_student()
elif choice == "2":
display_students()
elif choice == "3":
search_student()
elif choice == "4":
update_student()
elif choice == "5":
delete_student()
elif choice == "6":
break
else:
print("Invalid choice")